55import subprocess
66import sys
77
8+ import pytest
89
9- def run_tests (args , settings_module ):
10+
11+ def run_tests (extra_args , settings_module , test_app ):
1012 """
1113 Run Django tests with the specified settings module in a separate subprocess.
12- Executes the command inside the 'tests/' directory.
1314 """
14- os .environ ['DJANGO_SETTINGS_MODULE' ] = settings_module
15- result = subprocess .run (['python' , 'manage.py' ] + args , cwd = 'tests' )
15+ args = [
16+ './tests/manage.py' ,
17+ 'test' ,
18+ test_app ,
19+ '--settings' ,
20+ settings_module ,
21+ '--pythonpath' ,
22+ 'tests' ,
23+ ]
24+ args .extend (extra_args )
25+ if os .environ .get ('COVERAGE_RUN' , False ):
26+ # Since the Django tests are run in a separate process (using subprocess),
27+ # we need to run coverage in the subprocess as well.
28+ args = ['coverage' , 'run' ] + args
29+ result = subprocess .run (args )
1630 if result .returncode != 0 :
17- sys .exit (result .returncode ) # Exit immediately if tests fail
31+ sys .exit (result .returncode )
1832
1933
2034if __name__ == '__main__' :
@@ -29,23 +43,17 @@ def run_tests(args, settings_module):
2943 test_app = 'openwisp2'
3044 app_dir = 'tests/openwisp2/'
3145 # Run all tests except Selenium tests using SQLite
32- sqlite_args = ['test' , test_app , ' --exclude-tag' , 'selenium_tests' ] + base_args
33- run_tests (sqlite_args , 'openwisp2.settings' )
46+ sqlite_args = ['--exclude-tag' , 'selenium_tests' ] + base_args
47+ run_tests (sqlite_args , 'openwisp2.settings' , test_app )
3448
3549 # Run Selenium tests using PostgreSQL
3650 psql_args = [
37- 'test' ,
38- test_app ,
3951 '--tag' ,
4052 'db_tests' ,
4153 '--tag' ,
4254 'selenium_tests' ,
4355 ] + base_args
44- run_tests (psql_args , 'openwisp2.postgresql_settings' )
56+ run_tests (psql_args , 'openwisp2.postgresql_settings' , test_app )
4557
4658 # Run pytest tests
47- result = subprocess .run (
48- ['pytest' , app_dir ],
49- )
50- if result .returncode != 0 :
51- sys .exit (result .returncode ) # Exit immediately if tests fail
59+ sys .exit (pytest .main ([app_dir ]))
0 commit comments