0

I have a test runner in a django package that is split across two directories, like so:

package/models.py
package/tests/__init__.py
package/tests/test_foo.py
package/contrib/bar/models.py
package/contrib/bar/tests/__init__.py
package/contrib/bar/tests/test_bar.py

In package/tests/__init__.py I have this:

from package.contrib.tests import test_bar

And can run this just fine:

./manage.py test package.tests.test_bar

However, if I run either of these:

./manage.py test package
./manage.py test package.tests

test_bar isn't run.

I've confirmed this by writing a test that will always fail and when run with either of the above calls the tests work.

Is there a way to import a test_suite into another test suite?

  • Have you tried importing `*`? `from package.contrib.tests.test_bar import *`? – v1k45 Mar 22 '16 at 04:28
  • @v1k45 That worked, but I'm seeking other answers too. But if nothing else comes up you should make that an answer so I can accept (or at least upvote) it. –  Mar 22 '16 at 21:39

1 Answers1

2

In order to run tests from contrib package using

$ ./manage.py test package.tests

You must import * from contrib tests in your tests package's __init__.py.

Like this:

from package.contrib.tests.tests_bar import *
from .test_foo import *

This will run all contrib tests even if you pass package.tests as CLI argument.

But you wont be able to specify to run only contrib.test_bar as package.tests.test_bar. To do that, simply import test_bar module in the __init__.py file too.

v1k45
  • 8,070
  • 2
  • 30
  • 38
  • I've tried the latter `import test_bar` but it doesn't seem to get picked up by the test runner. The rest worked though. Thanks :) –  Mar 23 '16 at 09:00
  • It worked for me. You sure that you imported it same as you did in the question? – v1k45 Mar 23 '16 at 09:40
  • Note to readers: this used to be the accepted answer, until OP ragequit and attempted to punish bystanders for their [political disagreement with Stack Overflow](http://meta.stackoverflow.com/q/342994) in the process. Feel free to throw it an upvote to compensate if you like ;-) – Zero Piraeus Feb 05 '17 at 23:44