0

I've got a package - we'll be creative and call it package - and in there are api and dashboard packages, each with a tests module and various files filled with tests.

I'm using Django test runner and rather than having package.api.tests and package.dashboard.tests in my run configs, I'd like to just have package.tests and have that run all the tests in the packages below it.

I added a tests package to package and in the init tried a few things like from package.api.tests import * or using an all declaration but that didn't work.

Is there a way to make this happen? It's not the most annoying thing in the world, but it's a package that gets brought in to each project we do, and it would just be a bit simpler to have instructions of "Run package.tests", especially if we end up adding more packages beyond api and dashboard.

Shane
  • 613
  • 6
  • 15
  • 1
    Does this help ? https://stackoverflow.com/a/20932450/6505847 – AzyCrw4282 Nov 03 '20 at 19:18
  • 1
    That did help unlock a few ways for me. I used the directory rather than the module names, so the test runner finds everything within that directory. No need to worry about packages at all. I also realized that `from package.api.tests import *` wouldn't work, as I had my tests there also split into separate files, so I could either import * from each test file, or use an __all__ I went with the directory approach as it was simpler. Appreciate the nudge. Is it fair to turn that into an answer, and I can mark as correct? – Shane Nov 03 '20 at 19:28

1 Answers1

1

From the version of 1.6 in Django, you can:

Have test files matching the pattern test*.py. You can therefore have test files in the form of app/tests/test_models.py and app/tests/test_views.py.

You could also create a file named app/tests/test.py and include your other files from there. The thing is that the runner will look for files matching the pattern test*.py and execute them.

As you mentioned, from package.api.tests import * wouldn't have worked as the test functions could have been split into many different files. Therefore performing import * or using an all import approach is plausible in this case.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35