|
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | 4 | import os |
| 5 | +import subprocess |
5 | 6 | import sys |
6 | 7 |
|
7 | 8 | import pytest |
8 | | -from django.core.management import execute_from_command_line |
9 | 9 |
|
10 | 10 |
|
11 | 11 | def run_tests(args, settings_module): |
12 | 12 | """ |
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. |
14 | 15 | """ |
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 |
17 | 21 |
|
18 | 22 |
|
19 | 23 | 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:] |
24 | 25 | if not os.environ.get('SAMPLE_APP', False): |
25 | | - args.insert(2, 'openwisp_controller') |
| 26 | + test_app = 'openwisp_controller' |
26 | 27 | app_dir = 'openwisp_controller/' |
27 | 28 | else: |
28 | | - args.insert(2, 'openwisp2') |
| 29 | + test_app = 'openwisp2' |
29 | 30 | app_dir = 'tests/openwisp2/' |
30 | | - |
31 | 31 | # 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') |
35 | 34 |
|
36 | 35 | # 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