6

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,

briarfox
  • 525
  • 5
  • 13

1 Answers1

3

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.

Community
  • 1
  • 1
msw
  • 42,753
  • 9
  • 87
  • 112