Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running pytest from inside a module, seems to cache tests

I recently started playing with pytest and I use pytest.main() to run the tests. However it seems that pytest caches the test. Any changes made to my module or to the tests gets ignored. I am unable to run pytest from command line so pytest.main() is my only option, this is due to writing python on my ipad.

I have googled this extensively and was able to find one similar issue with advice to run pytest from command line. Any help would be greatly appreciated.

Thanks,

like image 576
briarfox Avatar asked Nov 01 '22 22:11

briarfox


1 Answers

Pytest doesn't cache anything. A module (file) is read once and only once per instance of a Python interpreter.

There is a reload built-in, but it almost never does what you hope it will do.

So if you are running

import pytest
...
while True:
    import my_nifty_app
    my_nifty_app.be_nifty()
    pytest.main()

my_nifty_app.py will be read once and only once even if it changes on disk. What you really need is something like

 exit_code = pytest.main()
 sys.exit(exit_code)

which will end that instance of the interpreter which is the only way to ensure your source files get re-read.

like image 65
msw Avatar answered Nov 10 '22 13:11

msw