Bugs: Mojo Test - Crashes when non-test file contains a struct with a parameter

I think I found a couple of bugs in Mojo’s test command.

  1. Running $ mojo test in a bash terminal seg faults when the file crash_mojo_test.mojois in another folder and contains struct Matrix[rows: Int]():
  2. Running $ mojo test ./tests/*finds only the last file in the /tests folder.

See the file PROJECT_STRUCTURE.md in Github project mojo_test for the folder structure.

In this repro, the two test files contain a total of three identical tests which execute a simple function that returns a string value. Nothing exotic in either the function or tests. For some reason, a non-test file in another folder blows up the mojo test command when a struct is defined using a parameter. It makes no sense why Mojo’s test command is even looking inside that file because its name doesn’t start with test_, the file doesn’t contain any tests, nor is the file crash_mojo_test.mojoused anywhere.

While trying to understand the cause of bug #1, I discovered that pointing to the test folder while using a wildcard executes only the last test file found (bug #2). That was also unexpected. Using mojo test ./tests runs all the tests in each test file.
Should I open a bug report for either or both of these? If yes, two separate bug reports or combined into one? Thanks!

Ubuntu 25.04 (boot OS)

Mojo 0.25.7.0.dev2025092905

Pixi 0.55.0

I think a bug report is appropriate. To me it looks as if the issue is with identifying the test files - not all required, yet some undesired files are executed. The struct issue is already known for files that are executed by test. So I would suggest opening a single issue and linking the test issue with the structs.

1 Like

Here’s a link to the struct issue, #5050, that @samufi referred to in his reply.

Created bug report #5379, `[BUG] mojo test opens files that don’t start with test_`.

Now as I have had a second look, I do not think that prefixing a file with test_ has ever been a requirement for mojo test. Only the functions in there need the prefix. As #5050 is independent on whether there is any test function, the behaviour you see with the segfault is a direct consequence of #5050 if the file matches the wildcard pattern.

With the “not all files are seen”, this might be a second issue.

The standard naming convention for Python test files is to prefix files with `test_` or use the suffix`_test.py`.This allows test discovery tools like pytest and unittest to automatically find and run test cases in those files.

My expectation was for Mojo test to follow the same file naming convention as Python, but it sounds like this is not a valid assumption. This should raise the priority for fixing #5050 to avoid crashing a functioning test suite while emitting an opaque error message. I’ve spent well over a day tracking down the cause of this crash which you documented over two months ago in #5050.:sad_but_relieved_face:

I placed a symlink to src in my tests folder so I can run individual test files in the VS Code IDE during development. The command mojo test tests follows the symlink and still finds the file `crash_mojo_test.mojo` which causes the seg fault due to #5050.

I see no way to run all of the tests at one go using a single Mojo command.

Why do you need a sym link?

The short answer is imports from the src folder of modules to be tested. See the file PROJECT_STRUCTURE.md in Github project mojo_test for the folder structure.
Below is a sample test file from the repro.

test_hello

from src.funcs import hello, MESSAGE

from testing import assert_true

fn main() raises:

“”“Run all ApiMessage tests.”“”

test_hello1()

fn test_hello1() raises:

print(“# test_hello1”)

assert_true(hello() == MESSAGE)

You can do this via the -I path/to/src argument. You can tell VSCode about this as well. See here, e.g.: Installation – Larecs🌲

Thanks! I am away from my computer right now. Will look at it later this evening. Cheers!