def __init__(self, testcases_to_run, nr_of_tests, all_should_pass=True,
print_bad=True, runner_options=None):
runner_options = runner_options or {}
self.nr_of_tests = nr_of_tests
self.all_should_pass = all_should_pass
self.print_bad = print_bad
django_runner_cls = get_runner(settings)
django_runner = django_runner_cls(**runner_options)
self.suite = django_runner.test_suite()
for testcase_cls in testcases_to_run:
tests = django_runner.test_loader.loadTestsFromTestCase(
testcase_cls)
self.suite.addTests(tests)
self.test_runner = django_runner.test_runner(
resultclass=django_runner.get_resultclass(),
stream=six.StringIO()
)
python类get_runner()的实例源码
def integrate_into_django_test_runner():
utils.get_runner = get_runner_with_djpt_mixin
DjptTestRunnerMixin.collectors = {}
DjptTestRunnerMixin.limits = {}
collector_ids = [
'test method', 'test setUp', 'test tearDown',
'test setUpClass', 'test tearDownClass',
]
for collector_id in collector_ids:
collectors = DjptTestRunnerMixin.collectors[collector_id] = []
limits = DjptTestRunnerMixin.limits[collector_id] = []
for limit_cls in djpt_core.limits_registry.name2cls.values():
collector = limit_cls.collector_cls(id_=collector_id)
collectors.append(collector)
limit = limit_cls(collector_id=collector_id, settings_based=True)
limits.append(limit)
def add_arguments(self, parser):
parser.add_argument(
'args', metavar='test_label', nargs='*',
help='Module paths to test; can be modulename, modulename.TestCase or modulename.TestCase.test_method'
)
parser.add_argument(
'--noinput', '--no-input', action='store_false', dest='interactive', default=True,
help='Tells Django to NOT prompt the user for input of any kind.',
)
parser.add_argument(
'--failfast', action='store_true', dest='failfast', default=False,
help='Tells Django to stop running the test suite after first failed test.',
)
parser.add_argument(
'--testrunner', action='store', dest='testrunner',
help='Tells Django to use specified test runner class instead of '
'the one specified by the TEST_RUNNER setting.',
)
test_runner_class = get_runner(settings, self.test_runner)
if hasattr(test_runner_class, 'add_arguments'):
test_runner_class.add_arguments(parser)
def add_arguments(self, parser):
parser.add_argument(
'args', metavar='test_label', nargs='*',
help='Module paths to test; can be modulename, modulename.TestCase or modulename.TestCase.test_method'
)
parser.add_argument(
'--noinput', '--no-input', action='store_false', dest='interactive', default=True,
help='Tells Django to NOT prompt the user for input of any kind.',
)
parser.add_argument(
'--failfast', action='store_true', dest='failfast', default=False,
help='Tells Django to stop running the test suite after first failed test.',
)
parser.add_argument(
'--testrunner', action='store', dest='testrunner',
help='Tells Django to use specified test runner class instead of '
'the one specified by the TEST_RUNNER setting.',
)
test_runner_class = get_runner(settings, self.test_runner)
if hasattr(test_runner_class, 'add_arguments'):
test_runner_class.add_arguments(parser)
def run_tests(self):
import django
django.setup()
from django.conf import settings
from django.test.utils import get_runner
TestRunner = get_runner(settings, self.testrunner)
test_runner = TestRunner(
pattern=self.pattern,
top_level=self.top_level_directory,
verbosity=self.verbose,
interactive=(not self.noinput),
failfast=self.failfast,
keepdb=self.keepdb,
reverse=self.reverse,
debug_sql=self.debug_sql,
output_dir=self.output_dir)
failures = test_runner.run_tests(self.test_labels)
sys.exit(bool(failures))
def add_arguments(self, parser):
parser.add_argument(
'args', metavar='test_label', nargs='*',
help='Module paths to test; can be modulename, modulename.TestCase or modulename.TestCase.test_method'
)
parser.add_argument(
'--noinput', '--no-input', action='store_false', dest='interactive', default=True,
help='Tells Django to NOT prompt the user for input of any kind.',
)
parser.add_argument(
'--failfast', action='store_true', dest='failfast', default=False,
help='Tells Django to stop running the test suite after first failed test.',
)
parser.add_argument(
'--testrunner', action='store', dest='testrunner',
help='Tells Django to use specified test runner class instead of '
'the one specified by the TEST_RUNNER setting.',
)
test_runner_class = get_runner(settings, self.test_runner)
if hasattr(test_runner_class, 'add_arguments'):
test_runner_class.add_arguments(parser)
def run_tests(base_dir=None, apps=None, verbosity=1, interavtive=False):
base_dir = base_dir or os.path.dirname(__file__)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path.insert(0, os.path.join(base_dir, 'src'))
sys.path.insert(0, os.path.join(base_dir, 'tests'))
import django
if django.VERSION >= (1,7):
django.setup()
from django.conf import settings
from django.test.utils import get_runner
TestRunner = get_runner(settings)
test_runner = TestRunner(verbosity=verbosity,
interavtive=interavtive,
failfast=False)
if apps:
app_tests = [x.strip() for x in apps if x]
else:
app_tests = [
'notification'
]
failures = test_runner.run_tests(app_tests)
sys.exit(bool(failures))
def runtests(args=None):
test_dir = os.path.dirname(__file__)
sys.path.insert(0, test_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
import django
from django.test.utils import get_runner
from django.conf import settings
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner(verbosity=1, interactive=True)
args = args or ['.']
failures = test_runner.run_tests(args)
sys.exit(failures)
def django_tests(verbosity, interactive, failfast, test_labels):
from django.conf import settings
state = setup(verbosity, test_labels)
extra_tests = []
# Run the test suite, including the extra validation tests.
from django.test.utils import get_runner
if not hasattr(settings, 'TEST_RUNNER'):
settings.TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner'
TestRunner = get_runner(settings)
test_runner = TestRunner(verbosity=verbosity, interactive=interactive,
failfast=failfast)
failures = test_runner.run_tests(test_labels or get_test_modules(), extra_tests=extra_tests)
teardown(state)
return failures
def add_arguments(self, parser):
parser.add_argument(
'args', metavar='test_label', nargs='*',
help='Module paths to test; can be modulename, modulename.TestCase or modulename.TestCase.test_method'
)
parser.add_argument(
'--noinput', '--no-input', action='store_false', dest='interactive', default=True,
help='Tells Django to NOT prompt the user for input of any kind.',
)
parser.add_argument(
'--failfast', action='store_true', dest='failfast', default=False,
help='Tells Django to stop running the test suite after first failed test.',
)
parser.add_argument(
'--testrunner', action='store', dest='testrunner',
help='Tells Django to use specified test runner class instead of '
'the one specified by the TEST_RUNNER setting.',
)
test_runner_class = get_runner(settings, self.test_runner)
if hasattr(test_runner_class, 'add_arguments'):
test_runner_class.add_arguments(parser)
def runtests(args=None):
test_dir = os.path.dirname(__file__)
sys.path.insert(0, test_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
import django
from django.test.utils import get_runner
from django.conf import settings
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner(verbosity=1, interactive=True)
args = args or ['.']
failures = test_runner.run_tests(args)
sys.exit(failures)
def run_tests(*test_args):
args, test_args = make_parser().parse_known_args(test_args)
if args.elasticsearch:
os.environ.setdefault('ELASTICSEARCH_URL', args.elasticsearch)
if not test_args:
test_args = ['tests']
settings = get_settings()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(test_args)
if failures:
sys.exit(bool(failures))
def handle(self, *test_labels, **options):
from django.conf import settings
from django.test.utils import get_runner
TestRunner = get_runner(settings, options.get('testrunner'))
options['verbosity'] = int(options.get('verbosity'))
if options.get('liveserver') is not None:
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = options['liveserver']
del options['liveserver']
test_runner = TestRunner(**options)
failures = test_runner.run_tests(test_labels)
if failures:
sys.exit(bool(failures))
def run_tests():
# Making Django run this way is a two-step process. First, call
# settings.configure() to give Django settings to work with:
from django.conf import settings
settings.configure(**SETTINGS_DICT)
# Then, call django.setup() to initialize the application cache
# and other bits:
import django
if hasattr(django, 'setup'):
django.setup()
# Now we instantiate a test runner...
from django.test.utils import get_runner
TestRunner = get_runner(settings)
# And then we run tests and return the results.
test_runner = TestRunner(verbosity=1, interactive=True)
failures = test_runner.run_tests(['registration.tests'])
sys.exit(bool(failures))
def run_tests():
# Making Django run this way is a two-step process. First, call
# settings.configure() to give Django settings to work with:
from django.conf import settings
settings.configure(**SETTINGS_DICT)
# Then, call django.setup() to initialize the application cache
# and other bits:
import django
if hasattr(django, 'setup'):
django.setup()
# Now we instantiate a test runner...
from django.test.utils import get_runner
TestRunner = get_runner(settings)
# And then we run tests and return the results.
test_runner = TestRunner(verbosity=1, interactive=True)
failures = test_runner.run_tests(['yandex_cash_register.tests'])
sys.exit(bool(failures))
def add_arguments(self, parser):
parser.add_argument(
'args', metavar='test_label', nargs='*',
help='Module paths to test; can be modulename, modulename.TestCase or modulename.TestCase.test_method'
)
parser.add_argument(
'--noinput', '--no-input', action='store_false', dest='interactive', default=True,
help='Tells Django to NOT prompt the user for input of any kind.',
)
parser.add_argument(
'--failfast', action='store_true', dest='failfast', default=False,
help='Tells Django to stop running the test suite after first failed test.',
)
parser.add_argument(
'--testrunner', action='store', dest='testrunner',
help='Tells Django to use specified test runner class instead of '
'the one specified by the TEST_RUNNER setting.',
)
test_runner_class = get_runner(settings, self.test_runner)
if hasattr(test_runner_class, 'add_arguments'):
test_runner_class.add_arguments(parser)
def run_tests():
settings_mod = os.environ.get('DJANGO_SETTINGS_MODULE', 'test_settings')
os.environ['DJANGO_SETTINGS_MODULE'] = settings_mod
import django
django.setup()
from django.test.utils import get_runner
from django.conf import settings
test_filter = os.environ.get('TEST_FILTER')
test_labels = [test_filter] if test_filter else []
test_runner = get_runner(settings)
failures = test_runner(
pattern='test_*.py',
verbosity=1,
interactive=True
).run_tests(test_labels)
sys.exit(failures)
def add_arguments(self, parser):
parser.add_argument(
'args', metavar='test_label', nargs='*',
help='Module paths to test; can be modulename, modulename.TestCase or modulename.TestCase.test_method'
)
parser.add_argument(
'--noinput', '--no-input', action='store_false', dest='interactive', default=True,
help='Tells Django to NOT prompt the user for input of any kind.',
)
parser.add_argument(
'--failfast', action='store_true', dest='failfast', default=False,
help='Tells Django to stop running the test suite after first failed test.',
)
parser.add_argument(
'--testrunner', action='store', dest='testrunner',
help='Tells Django to use specified test runner class instead of '
'the one specified by the TEST_RUNNER setting.',
)
test_runner_class = get_runner(settings, self.test_runner)
if hasattr(test_runner_class, 'add_arguments'):
test_runner_class.add_arguments(parser)
def patch_runner(cls):
# FIXME: this is incompatible with --parallel and --test-runner
# command arguments
test_runner = get_runner(settings)
if (not hasattr(test_runner, 'setup_test_environment') or not
hasattr(test_runner, 'teardown_test_environment')):
return
test_runner.setup_test_environment = cls.wrap_setup_test_environment(
test_runner.setup_test_environment
)
test_runner.teardown_test_environment = \
cls.wrap_teardown_test_environment(
test_runner.teardown_test_environment
)
test_integrates_with_django_testrunner.py 文件源码
项目:django-performance-testing
作者: PaesslerAG
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def test_runner_keeps_default_classes_in_inheritance_chain(
settings, runner_cls_name, test_runner_cls, test_suite_cls):
settings.TEST_RUNNER = runner_cls_name
django_runner_cls = get_runner(settings)
def assert_is_djpt_mixin(cls, base_cls, mixin_base_name):
fullname = 'django_performance_testing.test_runner.{}'.format(
mixin_base_name)
mixin_cls_name = '{}Mixin'.format(mixin_base_name)
mixin_cls = getattr(djpt_test_runner_module, mixin_cls_name)
assert fullname == to_dotted_name(cls)
assert issubclass(cls, mixin_cls)
assert cls.__mro__[1] == mixin_cls
if any(isinstance(base_cls, str_tp) for str_tp in six.string_types):
assert base_cls == to_dotted_name(cls.__mro__[2])
elif isinstance(base_cls, type):
assert issubclass(cls, base_cls)
assert cls.__mro__[2] == base_cls
else:
raise NotImplementedError(
'Cannot handle type {}'.format(type(base_cls)))
assert_is_djpt_mixin(
cls=django_runner_cls, base_cls=runner_cls_name,
mixin_base_name='DjptDjangoTestRunner')
assert_is_djpt_mixin(
cls=django_runner_cls.test_runner, base_cls=test_runner_cls,
mixin_base_name='DjptTestRunner')
assert django_runner_cls.test_suite == test_suite_cls
def run_tests(*test_args):
if not test_args:
test_args = ['tests']
# Run tests
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(test_args)
if failures:
sys.exit(bool(failures))
def run_tests(*test_args):
if not test_args:
test_args = ['tests']
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(test_args)
sys.exit(bool(failures))
def runtests():
test_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, test_dir)
settings.configure(
DEBUG=True,
SECRET_KEY='123',
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3'
}
},
INSTALLED_APPS=[
'django.contrib.auth',
'django.contrib.contenttypes',
'drf_simple_auth'
],
ROOT_URLCONF='drf_simple_auth.urls',
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
},
]
)
django.setup()
from django.test.utils import get_runner
TestRunner = get_runner(settings) # noqa
test_runner = TestRunner(verbosity=1, interactive=True)
if hasattr(django, 'setup'):
django.setup()
failures = test_runner.run_tests(['drf_simple_auth'])
sys.exit(bool(failures))
def run_tests(*test_args):
if not test_args:
test_args = ['tests']
# Run tests
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(test_args)
if failures:
sys.exit(bool(failures))
def run_tests(self):
# import here, cause outside the eggs aren't loaded
import django
from django.conf import settings
from django.test.utils import get_runner
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(['tests'])
sys.exit(bool(failures))
def main():
# configure django settings with test settings
from tests import settings as test_settings
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(['tests'])
sys.exit(failures)
def runtests():
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
test_runner = get_runner(settings)
if sys.argv[0] != 'setup.py' and len(sys.argv) > 1:
tests = sys.argv[1:]
else:
tests = ['tests']
failures = test_runner().run_tests(tests)
sys.exit(bool(failures))
def run_tests(*test_args):
if not test_args:
test_args = ['tests']
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(test_args)
sys.exit(bool(failures))
def run_tests(*test_args):
"""Discover and run tests."""
if not test_args:
test_args = ['tests']
# Run tests
runner = get_runner(settings)
test_runner = runner()
failures = test_runner.run_tests(test_args)
if failures:
sys.exit(bool(failures))
def runtests(*test_args):
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(["tests"])
sys.exit(bool(failures))