У меня странное поведение с одной из моих задач с сельдереем.
def run_single_test(test_name_or_decorator):
# get dict of test names, test paths
test_dict = get_single_test_names()
# check to see if test is in the dict
if test_name_or_decorator not in test_dict:
return 'The requested test could not be found.'
for test_name, test_path in test_dict.items():
# if test name is valid run associated test
if test_name == test_name_or_decorator:
pytest.main(['-p', 'no:django','--json-report', test_path])
report = return_test_result_json('.report.json')
report_id = str(uuid.uuid4())
test_run_data = TestResults.objects.create(name=report_id, data=report)
return 'this is your test report: {}'.format(get_report(test_run_data.id))
Эта задача выполнит команду pytest.main () и выполнит тест, однако, когда он вставит файл .report.json в БД с помощью .create (), я получаю следующую ошибку:
Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
Теперь, если я возьму все функциональные возможности в блоке for test_name....
, упростим вещи и переместу его в свою собственную функцию, все будет работать:
def run_single_test_path():
test_path = 'test_folder/test_file.py::TestClass::specific_test_name'
pytest.main(['-p', 'no:django','--json-report', test_path])
report = return_test_result_json('.report.json')
report_id = str(uuid.uuid4())
test_run_data = TestResults.objects.create(name=report_id, data=report)
return 'this is your test report: {}'.format(get_report(test_run_data.id))
Я получу ожидаемый доход:
"this is your test report: {'created': datetime.datetime(2018, 9, 27, 15, 51, 59, 297991, tzinfo=<UTC>), 'summary': {'total': 1, 'passed': 1}, 'exitcode': 0}"
Такое поведение не происходит с моими другими задачами, которые используют варианты тех же pytest.main () и .create ().Еще одно наблюдение заключается в том, что если я сначала выполню эту задачу, и будет сгенерирована ошибка доступа к базе данных, все остальные задачи завершатся с той же ошибкой.
Моя рабочая теория заключается в том, что что-то в цикле for или if приводит к сумасшествию .create (), но я абсолютно не знаю, почему.