lundi 29 décembre 2014

Unittest failed with sys.exit

I'm trying to run my tests with unittest. Here is my structure :



projectname/
projectname/
foo.py
bar.py
tests/
test_foo.py
test_bar.py


I run it with :



cd tests/
python -m unittest discover


But in one file, for example foo.py, I use a sys.exit(0), and unittest doesn't really like it :



$ python -m unittest discover
....E.
======================================================================
ERROR: test_foo (...)
----------------------------------------------------------------------
Traceback (most recent call last):
...
...
File "/home/.../projectname/foo.py", line 12, in write
sys.exit(0)
SystemExit: 0

----------------------------------------------------------------------
Ran 6 tests in 0.018s

FAILED (errors=1)


The sys.exit() use is voluntary, I can't remove it. I know there is an option called exit for the unittest.main function :



if __name__ == "__main__":
unittest.main(exit=False)


But I want to test all the files in the tests directory. Another way is to do :



if __name__ == '__main__':
tests = unittest.TestLoader().discover('.')
unittest.TextTestRunner(verbosity=1).run(tests)


It finds all the test_ files, but the sys.exit() makes unittest crash.


Aucun commentaire:

Enregistrer un commentaire