We defined all our user statements in a separate python file, which is not a test module.
For example: custom_asserts.py
class CustomAsserts(object): def silly_assert(self, foo, bar): assert foo == bar , 'some error message'
If we use assert directly in tests, we will get additional information about AssertionError, which is very useful.
Conclusion of direct use in tests:
> assert 'foo' == 'bar', 'some error message' E AssertionError: some error message E assert 'foo' == 'bar' E - foo E + bar
But we found that if we call the approval method that we defined in a separate module, additional information will not be displayed.
from custom_asserts import CustomAsserts asserts = CustomAsserts() def test_silly(): asserts.silly_assert('foo', 'bar')
Result after running the test:
> assert 'foo' == 'bar', 'some error message' E AssertionError: some error message
And we also found this in the pytest docs: Advanced Claims Introspection
pytest only overwrites the test modules directly detected by its test collection process, therefore claims in supporting modules that are not the test modules themselves will not be overwritten.
So my question is, is there a way to let pytest do the same thing as rewriting other modules as test modules? Or is there some hacker way to achieve this?