55import subprocess
66import sys
77
8+ import pytest
89
9- def run_tests (args , settings_module ):
10+
11+ def exec_command (args ):
12+ """
13+ Execute a command in a subprocess.
14+ """
15+ if os .environ .get ('COVERAGE_RUN' , False ):
16+ args = ['coverage' , 'run' ] + args
17+ result = subprocess .run (args )
18+ if result .returncode != 0 :
19+ sys .exit (result .returncode )
20+
21+
22+ def run_tests (extra_args , settings_module , test_app ):
1023 """
1124 Run Django tests with the specified settings module in a separate subprocess.
1225 Executes the command inside the 'tests/' directory.
1326 """
14- os .environ ['DJANGO_SETTINGS_MODULE' ] = settings_module
15- result = subprocess .run (['python' , 'manage.py' ] + args , cwd = 'tests' )
16- if result .returncode != 0 :
17- sys .exit (result .returncode ) # Exit immediately if tests fail
27+ args = [
28+ './tests/manage.py' ,
29+ 'test' ,
30+ test_app ,
31+ '--settings' ,
32+ settings_module ,
33+ '--pythonpath' ,
34+ 'tests' ,
35+ ]
36+ args .extend (extra_args )
37+ exec_command (args )
1838
1939
2040if __name__ == '__main__' :
@@ -29,23 +49,17 @@ def run_tests(args, settings_module):
2949 test_app = 'openwisp2'
3050 app_dir = 'tests/openwisp2/'
3151 # 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' )
52+ sqlite_args = ['--exclude-tag' , 'selenium_tests' ] + base_args
53+ run_tests (sqlite_args , 'openwisp2.settings' , test_app )
3454
3555 # Run Selenium tests using PostgreSQL
3656 psql_args = [
37- 'test' ,
38- test_app ,
3957 '--tag' ,
4058 'db_tests' ,
4159 '--tag' ,
4260 'selenium_tests' ,
4361 ] + base_args
44- run_tests (psql_args , 'openwisp2.postgresql_settings' )
62+ run_tests (psql_args , 'openwisp2.postgresql_settings' , test_app )
4563
4664 # 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
65+ sys .exit (pytest .main ([app_dir ]))
0 commit comments