Printing inside test cases

It appears print messages don’t get displayed - by default. What do we need to do to display print messages within test cases? Currently - I am having make tests fail intentionally to get the assertion message!

I don’t think there’s a way to make print messages show when running with mojo test. What I do is stick a def main() in my test file, and then call the test functions from that so I can run the file with mojo run. They you can see the stdout.

How do execute the test cases with main function? When I fire the command “mojo test” - they get executed. These test files have imports from parent folder. No issues when I run them with “mojo test”. But when I change my directory to the test folder itself - run a particular test file with main function by executing “mojo test_something.mojo” - the imports from parent folder is not recognized!

From the root of your project you should be able to run something like this:

magic run mojo run -I . ./tests/test_searcher.mojo

Where test_searcher.mojo is the file that you have the def main in.

That worked. Thank you.

From within the magic shell, following also work:

mojo run -I . ./test/test_seeding.mojo
mojo  -I . ./test/test_seeding.mojo # Without 'run'
1 Like