Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance MojoTest class in util.mojo to print the call_location. #15

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ $ pytest
============================= test session starts ==============================
platform darwin -- Python 3.11.9, pytest-7.4.3, pluggy-1.3.0
rootdir: /Users/guidorice/mojo/mojo-pytest
plugins: mojo-24.3.0
plugins: mojo-24.3.1
collected 18 items

example/tests/suffix_test.mojo . [ 5%]
Expand All @@ -96,10 +96,10 @@ example/tests/mod_b/test_greet.mojo . [100%]

=================================== FAILURES ===================================
_______________________________ maths more: 42 ________________________________
(<MojoTestItem maths more: 42>, 'At /.../mojo-pytest/example/tests/util.mojo:21:32: AssertionError: bad maths: 42')
(<MojoTestItem maths more: 42>, '/.../mojo-pytest/example/tests/mod_a/test_maths.mojo:30:29: AssertionError: bad maths: 42')
=========================== short test summary info ============================
FAILED example/tests/mod_a/test_maths.mojo:: maths more: 42
========================= 1 failed, 17 passed in 1.82s =========================
========================= 1 failed, 17 passed in 1.90s =========================
```

## Links
Expand Down
12 changes: 6 additions & 6 deletions example/tests/util.mojo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import testing
from builtin._location import __call_location


@value
Expand All @@ -13,11 +13,11 @@ struct MojoTest:
self.test_name = test_name
print("# " + test_name)

@always_inline("nodebug")
fn assert_true(self, cond: Bool, message: String):
"""
Wraps testing.assert_true.
If the condition is false, prints MojoPytestError and call location.
"""
try:
testing.assert_true(cond, message)
except e:
print(e)
if not cond:
var call_loc = __call_location()
print(call_loc.file_name, ":", str(call_loc.line), ":", str(call_loc.col), ": ", "AssertionError: " , message, sep="")
6 changes: 3 additions & 3 deletions pytest_mojo/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
By convention, a comment line (hashtag) signals the test item name.
"""

TEST_FAILED_PREFIX = "AssertionError: "
TEST_FAILED_TAG = "AssertionError: "
"""
This is the prefix used in Mojo assertions in the testing module
This is the tag used in Mojo assertions in the util.mojo module.
"""


Expand Down Expand Up @@ -115,7 +115,7 @@ def __init__(self, *, name: str, parent, spec: dict[str, Any], **kwargs):

def runtest(self):
if self.spec.get("code") or any(
TEST_FAILED_PREFIX in item for item in self.spec["stdout"]
TEST_FAILED_TAG in item for item in self.spec["stdout"]
):
raise MojoTestException(self, self.spec["stdout"][-1])

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="24.3.0",
version="24.3.1",
packages=find_packages(),
entry_points={"pytest11": ["mojo = pytest_mojo.plugin"]},
install_requires=["pytest"],
Expand Down
Loading