New issue 427: Option to have pytest rewrite assertions in additional non-test 
modules
https://bitbucket.org/hpk42/pytest/issue/427/option-to-have-pytest-rewrite-assertions

Jurko Gospodnetić:

Sometimes, when refactoring a project's test code, it becomes useful to extract 
some useful project specific testing functions into a separate testing utility 
module. A function in that module may then perform an assertion while actual 
test functions just call that utility function as needed.

Problem in this case is that such assertions are ignored when running the tests 
using the '-O' command-line option. However, assertions used directly inside 
test modules are not, since pytest's assertion rewriting preserves them.

This could be resolved by allowing a project to list extra modules pytest's 
assertion rewriting should be applied to. A new configuration option seems like 
a natural way to allow this.

Some alternative solution ideas:
1. It is possible to refactor the project code so that the utility function 
just returns a True/False boolean indicator and then have the test function 
assert that the function returned True, but having the function perform the 
actual assertion often provides better error reports in case of test failure. 
For example, if you have a utility function comparing two XML structures, it 
seems much better if the test fails and reports that node X on one side and 
node X on the other side have different content, then if it just says 'the two 
XML documents do not match'.
2. Utility functions could raise some sort of a project specific exception 
instead of an assertion failure - a bit smelly, would not directly provide a 
clean test failure explanation error message but would allow the user to drop 
into the debugger at the location where the exception was raised.

Best regards,
  Jurko Gospodnetić


_______________________________________________
pytest-commit mailing list
pytest-commit@python.org
https://mail.python.org/mailman/listinfo/pytest-commit

Reply via email to