# Printing inside test cases

**URL:** <https://forum.modular.com/t/printing-inside-test-cases/1292>\
**Category:** Mojo\
**Created:** [April 24, 2025, 2:05pm UTC](https://forum.modular.com/t/printing-inside-test-cases/1292 "2025-04-24T14:05:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratulb](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@ratulb](https://forum.modular.com/u/ratulb)\
**Post date:** [April 24, 2025, 2:05pm UTC](https://forum.modular.com/t/printing-inside-test-cases/1292/1 "2025-04-24T14:05:44Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![duck\_tape](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/duck_tape/32/432_2.png) [@duck\_tape](https://forum.modular.com/u/duck_tape)\
**Post date:** [April 24, 2025, 2:10pm UTC](https://forum.modular.com/t/printing-inside-test-cases/1292/2 "2025-04-24T14:10:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ratulb](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@ratulb](https://forum.modular.com/u/ratulb)\
**Post date:** [April 24, 2025, 4:58pm UTC](https://forum.modular.com/t/printing-inside-test-cases/1292/3 "2025-04-24T16:58:30Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![duck\_tape](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/duck_tape/32/432_2.png) [@duck\_tape](https://forum.modular.com/u/duck_tape)\
**Post date:** [April 24, 2025, 5:12pm UTC](https://forum.modular.com/t/printing-inside-test-cases/1292/4 "2025-04-24T17:12:18Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![ratulb](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@ratulb](https://forum.modular.com/u/ratulb)\
**Post date:** [April 24, 2025, 5:33pm UTC](https://forum.modular.com/t/printing-inside-test-cases/1292/5 "2025-04-24T17:33:38Z")

</div>

> [@duck\_tape](#):
>
> `magic run mojo run -I . ./tests/`

That worked. Thank you.

From within the magic shell, following also work:

```mojo
mojo run -I . ./test/test_seeding.mojo
mojo -I . ./test/test_seeding.mojo # Without 'run'

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [May 1, 2025, 5:34pm UTC](https://forum.modular.com/t/printing-inside-test-cases/1292/6 "2025-05-01T17:34:01Z")

</div>

Closed on May 1, 2025, 5:34pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
