Skip to content

Commit 7b72cec

Browse files
authored
Enhance MojoTest class in util.mojo to print the call_location. (#15)
1 parent 7171669 commit 7b72cec

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ $ pytest
8383
============================= test session starts ==============================
8484
platform darwin -- Python 3.11.9, pytest-7.4.3, pluggy-1.3.0
8585
rootdir: /Users/guidorice/mojo/mojo-pytest
86-
plugins: mojo-24.3.0
86+
plugins: mojo-24.3.1
8787
collected 18 items
8888
8989
example/tests/suffix_test.mojo . [ 5%]
@@ -96,10 +96,10 @@ example/tests/mod_b/test_greet.mojo . [100%]
9696
9797
=================================== FAILURES ===================================
9898
_______________________________ maths more: 42 ________________________________
99-
(<MojoTestItem maths more: 42>, 'At /.../mojo-pytest/example/tests/util.mojo:21:32: AssertionError: bad maths: 42')
99+
(<MojoTestItem maths more: 42>, '/.../mojo-pytest/example/tests/mod_a/test_maths.mojo:30:29: AssertionError: bad maths: 42')
100100
=========================== short test summary info ============================
101101
FAILED example/tests/mod_a/test_maths.mojo:: maths more: 42
102-
========================= 1 failed, 17 passed in 1.82s =========================
102+
========================= 1 failed, 17 passed in 1.90s =========================
103103
```
104104

105105
## Links

example/tests/util.mojo

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import testing
1+
from builtin._location import __call_location
22

33

44
@value
@@ -13,11 +13,11 @@ struct MojoTest:
1313
self.test_name = test_name
1414
print("# " + test_name)
1515

16+
@always_inline("nodebug")
1617
fn assert_true(self, cond: Bool, message: String):
1718
"""
18-
Wraps testing.assert_true.
19+
If the condition is false, prints MojoPytestError and call location.
1920
"""
20-
try:
21-
testing.assert_true(cond, message)
22-
except e:
23-
print(e)
21+
if not cond:
22+
var call_loc = __call_location()
23+
print(call_loc.file_name, ":", str(call_loc.line), ":", str(call_loc.col), ": ", "AssertionError: " , message, sep="")

pytest_mojo/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
By convention, a comment line (hashtag) signals the test item name.
2626
"""
2727

28-
TEST_FAILED_PREFIX = "AssertionError: "
28+
TEST_FAILED_TAG = "AssertionError: "
2929
"""
30-
This is the prefix used in Mojo assertions in the testing module
30+
This is the tag used in Mojo assertions in the util.mojo module.
3131
"""
3232

3333

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

116116
def runtest(self):
117117
if self.spec.get("code") or any(
118-
TEST_FAILED_PREFIX in item for item in self.spec["stdout"]
118+
TEST_FAILED_TAG in item for item in self.spec["stdout"]
119119
):
120120
raise MojoTestException(self, self.spec["stdout"][-1])
121121

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="pytest-mojo",
5-
version="24.3.0",
5+
version="24.3.1",
66
packages=find_packages(),
77
entry_points={"pytest11": ["mojo = pytest_mojo.plugin"]},
88
install_requires=["pytest"],

0 commit comments

Comments
 (0)