Skip to content

Commit

Permalink
Add support for _test suffix test discovery. (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
guidorice authored Nov 19, 2023
1 parent c03ede7 commit 6a70d50
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
export MODULAR_HOME="/home/runner/.modular"
export PATH="/home/runner/.modular/pkg/packages.modular.com_mojo/bin:$PATH"
# check test collection (this count needs to be updated manually when tests are updated)
pytest | grep "collected 17 items"
# Tests that do not fail
pytest example/tests/mod_b
Expand All @@ -29,14 +32,13 @@ jobs:
exit 1
fi
# Test should fail becuase I am passing options for checking warnings
# Test should fail because I am passing options for checking warnings
rm -rf ~/.modular/.mojo_cache
if pytest -W error example/tests/test_warning.mojo ; then
echo "This tests should fail"
exit 1
fi
# Test should not fail becuase I am not checking for warning
# Test should not fail because I am not checking for warning
rm -rf ~/.modular/.mojo_cache
pytest example/tests/test_warning.mojo
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ In the `example/` directory is a Mojo package with a couple of modules. Note: in
`__init__.mojo` file, not shown here:

```shell
example/
example
├── mod_a
│ └── impl.mojo
├── mod_b
Expand All @@ -67,30 +67,32 @@ example/
│ └── test_maths.mojo
├── mod_b
│ └── test_greet.mojo
├── suffix_test.mojo
├── test_warning.mojo
└── util.mojo
```

```text
$ pytest
============================= test session starts =============================
platform darwin -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0
================================ test session starts ================================
platform darwin -- Python 3.11.5, pytest-7.4.0, pluggy-1.0.0
rootdir: /Users/guidorice/mojo/mojo-pytest
plugins: mojo-0.1.1
collected 16 items
example/tests/test_warning.mojo . [ 6%]
example/tests/mod_a/test_convert.mojo . [ 12%]
example/tests/mod_a/test_convert_different.mojo . [ 18%]
example/tests/mod_a/test_maths.mojo ....F....... [ 93%]
example/tests/mod_b/test_greet.mojo . [100%]
================================== FAILURES ===================================
_______________________________ maths more: 42 _______________________________
plugins: mojo-0.2.0
collected 17 items
example/tests/suffix_test.mojo . [ 5%]
example/tests/test_warning.mojo . [ 11%]
example/tests/mod_a/test_convert.mojo . [ 17%]
example/tests/mod_a/test_convert_different.mojo . [ 23%]
example/tests/mod_a/test_maths.mojo ....F....... [ 94%]
example/tests/mod_b/test_greet.mojo . [100%]
===================================== FAILURES ======================================
__________________________________ maths more: 42 __________________________________
(<MojoTestItem maths more: 42>, 'ASSERT ERROR: bad maths: 42')
=========================== short test summary info ===========================
============================== short test summary info ==============================
FAILED example/tests/mod_a/test_maths.mojo:: maths more: 42
======================== 1 failed, 15 passed in 0.33s =========================
=========================== 1 failed, 16 passed in 0.38s ============================
```

## Links
Expand Down
13 changes: 13 additions & 0 deletions example/tests/suffix_test.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Tests the test suffix form of discovery ex: something_test.mojo
"""
from example.tests.util import MojoTest


fn main() raises:
test()


fn test() raises:
let test = MojoTest("test suffix discovery")
test.assert_true(True, "")
9 changes: 8 additions & 1 deletion pytest_mojo/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
Examples of test prefix: `test_something.mojo` or `test_xyz.🔥`
"""

TEST_SUFFIX = "_test"
"""
Examples of test suffix: `something_test.mojo` or `xyz_test.🔥`
"""

TEST_ITEM_PREFIX = "#"
"""
By convention, a comment line (hashtag) signals the test item name.
Expand All @@ -26,7 +31,9 @@


def pytest_collect_file(parent: Package, file_path: Path) -> File | None:
if file_path.suffix in (".mojo", ".🔥") and file_path.name.startswith(TEST_PREFIX):
if file_path.suffix in (".mojo", ".🔥") and (
file_path.stem.startswith(TEST_PREFIX) or file_path.stem.endswith(TEST_SUFFIX)
):
return MojoTestFile.from_parent(parent, path=file_path)
return None

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="pytest-mojo",
version="0.1.1",
version="0.2.0",
packages=find_packages(),
entry_points={"pytest11": ["mojo = pytest_mojo.plugin"]},
install_requires=["pytest"],
Expand Down

0 comments on commit 6a70d50

Please sign in to comment.