django-rest-framework/runtests.py

52 lines
1.5 KiB
Python
Raw Normal View History

#! /usr/bin/env python3
2015-06-25 23:58:24 +03:00
import sys
2014-08-19 16:28:07 +04:00
2015-06-25 23:58:24 +03:00
import pytest
2014-08-19 16:28:07 +04:00
2015-06-25 23:58:24 +03:00
2014-08-19 16:28:07 +04:00
def split_class_and_function(string):
class_string, function_string = string.split('.', 1)
return "%s and %s" % (class_string, function_string)
2014-08-19 16:28:07 +04:00
def is_function(string):
# `True` if it looks like a test function is included in the string.
return string.startswith('test_') or '.test_' in string
2014-08-19 16:28:07 +04:00
def is_class(string):
# `True` if first character is uppercase - assume it's a class name.
return string[0] == string[0].upper()
if __name__ == "__main__":
if len(sys.argv) > 1:
pytest_args = sys.argv[1:]
first_arg = pytest_args[0]
try:
pytest_args.remove('--coverage')
except ValueError:
pass
else:
pytest_args = [
'--cov', '.',
'--cov-report', 'xml',
] + pytest_args
2014-08-19 16:28:07 +04:00
if first_arg.startswith('-'):
# `runtests.py [flags]`
pytest_args = ['tests'] + pytest_args
elif is_class(first_arg) and is_function(first_arg):
# `runtests.py TestCase.test_function [flags]`
expression = split_class_and_function(first_arg)
pytest_args = ['tests', '-k', expression] + pytest_args[1:]
elif is_class(first_arg) or is_function(first_arg):
2015-06-25 23:58:24 +03:00
# `runtests.py TestCase [flags]`
2014-08-19 16:28:07 +04:00
# `runtests.py test_function [flags]`
pytest_args = ['tests', '-k', pytest_args[0]] + pytest_args[1:]
else:
pytest_args = []
2015-06-25 23:58:24 +03:00
sys.exit(pytest.main(pytest_args))