I am having a very complicated tests.py
file.
Actually the tests classes and methods are generated at run time w/ type
(to account for data listed in auxiliary files). I am doing things in the following fashion (see below for more code):
klass = type(name, (TestCase,), attrs)
setattr(current_module, name, klass)
FYI, with the usual django test runner, all those tests get run when doing ./manage.py test myapp
(thanks to the setattr
shown above).
What I want to do is run only part of those tests, without listing their names by hand.
For example, I could give each test "tags" in the class names or method names so that I could filter on them. For example I would then perform: run all tests which method name contains the string "test_postgres_backend_"
I tried using django-nose
because of nose
's -m
option, which should be able to select tests based on regular expressions, an ideal solution to my problem.
Unfortunately, here is what is happening when using django-nose as the django test runner:
./manage.py test myapp
is not finding automatically thetype
-generated test classes (contrarily to the django test runner)- neither
./manage.py test -m ".*" myapp
nor./manage.py test myapp -m ".*"
find ANY test, even if normalTestCase
classes are present in the file
So:
- Do you have another kind of solution to my general problem, rather than trying to use django-nose
-m
? - With
django-nose
, do you know how to make the-m
work?
mcve
Add the following to an empty myapp/tests.py
file:
from django.test import TestCase
from sys import modules
current_module = modules[__name__]
def passer(self, *args, **kw):
self.assertEqual(1, 1)
def failer(self, *args, **kw):
self.assertEqual(1, 2)
# Create a hundred ...
for i in xrange(100):
# ... of a stupid TestCase class that has 1 method that passes if `i` is
# even and fails if `i` is odd
klass_name = "Test_%s" % i
if i % 2: # Test passes if even
klass_attrs = {
'test_something_%s' % i: passer
}
else: # Fail if odd
klass_attrs = {
'test_something_%s' % i: failer
}
klass = type(klass_name, (TestCase,), klass_attrs)
# Set the class as "child" of the current module so that django test runner
# finds it
setattr(current_module, klass_name, klass)
If makes for this output run (in alphab order) by django test runnner:
F.F.F.F.F.F.FF.F.F.F.F..F.F.F.F.F.FF.F.F.F.F..F.F.F.F.F.FF.F.F.F.F..F.F.F.F.F.FF.F.F.F.F..F.F.F.F.F..
If you change to django_nose
test runner, nothing happens on ./manage.py test myapp
.
After fixing this, I would then like would be able to run only the test methods which name end with a 0
(or some other kind of regexable filtering)