mercredi 31 juillet 2019

How can I run all Unit Tests from the test directory with Python? (structured packages)

I set up my project in IntelliJ IDEA (or PyCharm) like this:

project_root
  + src
    + business
      - production_code.py (has a random class I want to test)
  + test
    + business
      - test_production_code.py (using unittest module)

I tried multiple solutions I found online, but unfortunately none of them allow me to run all of the tests inside test directory and its subdirectories, without adding __init__.py everywhere (I'd like to avoid that) or changing sources or changing the configured directory structure.

Note that using pytest actually allows me to find all the tests and run them, but then the imports fail. Looks like none of the classes/modules from src could be found from the test directory - I'm guessing because by default src is not added to the test path.

Also note that my tests run fine from the IDE, but I noticed that IntelliJ somehow seems to add the src directory to the path, so that's probably why it works.

Aucun commentaire:

Enregistrer un commentaire