Skip to content

Commit ee1b3a5

Browse files
committed
[fix] Fixed runtests.py
1 parent 87497fd commit ee1b3a5

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

runtests.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,46 @@
22
# -*- coding: utf-8 -*-
33

44
import os
5+
import subprocess
56
import sys
67

78
import pytest
8-
from django.core.management import execute_from_command_line
99

1010

1111
def run_tests(args, settings_module):
1212
"""
13-
Run Django tests with the specified settings module.
13+
Run Django tests with the specified settings module in a separate subprocess.
14+
Executes the command inside the 'tests/' directory.
1415
"""
15-
os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
16-
execute_from_command_line(args)
16+
env = os.environ.copy()
17+
env['DJANGO_SETTINGS_MODULE'] = settings_module
18+
result = subprocess.run(['python', 'manage.py'] + args, env=env, cwd='tests')
19+
if result.returncode != 0:
20+
sys.exit(result.returncode) # Exit immediately if tests fail
1721

1822

1923
if __name__ == '__main__':
20-
sys.path.insert(0, 'tests')
21-
22-
args = sys.argv
23-
args.insert(1, 'test')
24+
base_args = sys.argv.copy()[1:]
2425
if not os.environ.get('SAMPLE_APP', False):
25-
args.insert(2, 'openwisp_controller')
26+
test_app = 'openwisp_controller'
2627
app_dir = 'openwisp_controller/'
2728
else:
28-
args.insert(2, 'openwisp2')
29+
test_app = 'openwisp2'
2930
app_dir = 'tests/openwisp2/'
30-
3131
# Run all tests except Selenium tests using SQLite
32-
sqlite_args = args.copy()
33-
sqlite_args.extend(['--exclude-tag', 'selenium_tests'])
34-
run_tests(sqlite_args, settings_module='openwisp2.settings')
32+
sqlite_args = ['test', test_app, '--exclude-tag', 'selenium_tests'] + base_args
33+
run_tests(sqlite_args, 'openwisp2.settings')
3534

3635
# Run Selenium tests using PostgreSQL
37-
psql_args = args.copy()
38-
psql_args.extend(['--tag', 'selenium_tests', '--tag', 'db_tests'])
39-
run_tests(psql_args, settings_module='openwisp2.postgresql_settings')
40-
41-
sys.exit(pytest.main([app_dir]))
36+
psql_args = [
37+
'test',
38+
test_app,
39+
'--tag',
40+
'db_tests',
41+
'--tag',
42+
'selenium_tests',
43+
] + base_args
44+
run_tests(psql_args, 'openwisp2.postgresql_settings')
45+
46+
# Run pytest tests
47+
sys.exit(pytest.main([app_dir]))

0 commit comments

Comments
 (0)