Debian Codegraph @

runtests.py

raw
1 #! /usr/bin/env python3
2 import os
3 import sys
4
5 import pytest
6
7
8 def split_class_and_function(string):
9 class_string, function_string = string.split('.', 1)
10 return "%s and %s" % (class_string, function_string)
11
12
13 def is_function(string):
14 # `True` if it looks like a test function is included in the string.
15 return string.startswith('test_') or '.test_' in string
16
17
18 def is_class(string):
19 # `True` if first character is uppercase - assume it's a class name.
20 return string[0] == string[0].upper()
21
22
23 if __name__ == "__main__":
24 if len(sys.argv) > 1:
25 pytest_args = sys.argv[1:]
26 first_arg = pytest_args[0]
27
28 try:
29 pytest_args.remove('--coverage')
30 except ValueError:
31 pass
32 else:
33 pytest_args = [
34 '--cov', '.',
35 '--cov-report', 'xml',
36 ] + pytest_args
37
38 try:
39 pytest_args.remove('--no-pkgroot')
40 except ValueError:
41 pass
42 else:
43 sys.path.pop(0)
44
45 # import rest_framework before pytest re-adds the package root directory.
46 import rest_framework
47 package_dir = os.path.join(os.getcwd(), 'rest_framework')
48 assert not rest_framework.__file__.startswith(package_dir)
49
50 if first_arg.startswith('-'):
51 # `runtests.py [flags]`
52 pytest_args = ['tests'] + pytest_args
53 elif is_class(first_arg) and is_function(first_arg):
54 # `runtests.py TestCase.test_function [flags]`
55 expression = split_class_and_function(first_arg)
56 pytest_args = ['tests', '-k', expression] + pytest_args[1:]
57 elif is_class(first_arg) or is_function(first_arg):
58 # `runtests.py TestCase [flags]`
59 # `runtests.py test_function [flags]`
60 pytest_args = ['tests', '-k', pytest_args[0]] + pytest_args[1:]
61 else:
62 pytest_args = []
63
64 sys.exit(pytest.main(pytest_args))