lundi 9 juillet 2018

How to run tests which are located in same file as production code with pytest?

I know this violates any best practices which require/assume proper packaging of Python production code: In some cases it might be helpful to beeing able to define production and test code in the same file (e.g. in case of simple scripts). How can I run all or specific tests in a file with pytest then?

EDIT - The solution for my particular use case:

file structure in <root>:

pytest.ini
scrip_with_tests.py

content of pytest.ini:

[pytest]
python_files = script_with_tests.py

content of script_with_tests.py:

import pytest  # this is not required, works without as well

def test_always_pass():
    pass

if __name__ == "__main__":
    main()

pytest invocation in <root>:

pytest script_with_tests.py

Aucun commentaire:

Enregistrer un commentaire