M
Modular7mo ago
Hylke

Mojo test

Did anyone get unit tests working? I run mojo test on the following folder structure:
├── my_package
│   ├── __init__.:fire:
│   └── tensor.:fire:
└── test
├── __init__.:fire:
└── test_tensor.:fire:
├── my_package
│   ├── __init__.:fire:
│   └── tensor.:fire:
└── test
├── __init__.:fire:
└── test_tensor.:fire:
But it doesn't pick up my tests Total Discovered Tests: 0. The test_tensor.🔥 contains a function:
def test_transformation():
...
def test_transformation():
...
As described in the docs: https://docs.modular.com/mojo/tools/testing Any suggestions?
Testing | Modular Docs
Testing Mojo programs.
3 Replies
bunny
bunny7mo ago
Same issue. Not sure what I'm doing wrong. So I've taken to including a main() in each test file and adding each test function there:
# file: test_stuff.mojo

def test_A():
print("test_A: ", end="")
# do stuff
print("PASS")

def test_B():
print("test_B: ", end="")
# do stuff
print("PASS")

def main():
print("##### test_stuff.mojo, start")
test_A()
test_B()
print("all tests passed")
# file: test_stuff.mojo

def test_A():
print("test_A: ", end="")
# do stuff
print("PASS")

def test_B():
print("test_B: ", end="")
# do stuff
print("PASS")

def main():
print("##### test_stuff.mojo, start")
test_A()
test_B()
print("all tests passed")
Then, I can do a shell command to run them all: find ./ -type f -name "test_*.mojo" -execdir mojo "{}" \; Output:
##### test_stuff.mojo, start
test_A: PASS
test_B: PASS
all tests passed
##### test_some_other_file_that_is_not_shown_above.mojo, start
test_ok: PASS
test_some_failure: AssertionError: `left == right` comparison failed:
left: 1
right: 2
mojo: error: execution exited with a non-zero result: 1
##### test_stuff.mojo, start
test_A: PASS
test_B: PASS
all tests passed
##### test_some_other_file_that_is_not_shown_above.mojo, start
test_ok: PASS
test_some_failure: AssertionError: `left == right` comparison failed:
left: 1
right: 2
mojo: error: execution exited with a non-zero result: 1
Not pretty, but is a workaround until I figure out what I'm doing wrong that prevents mojo test from picking up my test files & functions. 😊 oh wow. I was just goofing around a bit and removed the __init__.mojo file; now mojo test works. Although I need to rework my test code, as removing the init file messed with my imports. Hmmm.... mojo test -I . worked for me. Still need to tidy test cases a bit, but at least this is a bit of a win. I forgot to include a pic of the output from mojo test -I . -- keep in mind that the -I . tells it that it should add the current directory (.) to Mojo's import path for the purpose of the test run.
bunny
bunny7mo ago
No description
Hylke
HylkeOP7mo ago
Solved! Thanks @bunny , removing the __init__.mojo helped fixed the problem.
Want results from more Discord servers?
Add your server