Python assert pytest命令已运行

Python assert pytest命令已运行,python,pytest,Python,Pytest,我有一个django应用程序路由,如果满足某些条件,它将运行pytest.main()命令: def run_single_test(request, single_test_name): # get dict of test names, test paths test_dict = get_single_test_names() # check to see if test is in the dict if single_test_name in test_

我有一个django应用程序路由,如果满足某些条件,它将运行pytest.main()命令:

def run_single_test(request, single_test_name):
    # get dict of test names, test paths
    test_dict = get_single_test_names()
    # check to see if test is in the dict
    if single_test_name in test_dict:
        for test_name,test_path in test_dict.items():
            # if testname is valid run associated test
            if test_name == single_test_name:
                os.chdir('/lib/tests/')
                run_test = pytest.main(['-v', '--json-report', test_path])
    else:
        return 'The requested test could not be found.'
我想包括一个单元测试,用于验证
run\u test
是否已执行

这样做的最佳方法是什么?Mock和unittest对我来说是新的

我试着和stdout混在一起:

def test_run_single_test_flow_control(self):
        mock_get = patch('test_automation_app.views.get_single_test_names')
        mock_get = mock_get.start()
        mock_get.return_value = {'test_search': 'folder/test_file.py::TestClass::test'}

        results = run_single_test('this-request', 'test_search')
        output = sys.stdout
        self.assertEqual(output, '-v --json-report folder/test_file.py::TestClass::test')
但这也带来了:

<_pytest.capture.EncodedFile object at XXXXXXXXXXXXXX>

以下是两个示例测试,它们验证在通过有效测试名称时调用
pytest.main
,否则不调用。我还添加了一些不同的调用
mock\u pytest\u main.assert\u调用
作为示例;它们的作用几乎相同,只需额外检查函数调用时传递的参数。希望这能帮助您编写更复杂的测试

from unittest.mock import patch
from test_automation_app.views import run_single_test


def test_pytest_invoked_when_test_name_valid():
    with patch('pytest.main') as mock_pytest_main, patch('test_automation_app.views.get_single_test_names') as mock_get:
        mock_get.return_value = {'test_search': 'folder/test_file.py::TestClass::test'}
        results = run_single_test('this-request', 'test_search')
        mock_pytest_main.assert_called()
        mock_pytest_main.assert_called_with(['-v', '--json-report', 'folder/test_file.py::TestClass::test'])
        mock_pytest_main.assert_called_once()
        mock_pytest_main.assert_called_once_with(['-v', '--json-report', 'folder/test_file.py::TestClass::test'])


def test_pytest_not_invoked_when_test_name_invalid():
    with patch('pytest.main') as mock_pytest_main, patch('test_automation_app.views.get_single_test_names') as mock_get:
        mock_get.return_value = {'test_search': 'folder/test_file.py::TestClass::test'}
        results = run_single_test('this-request', 'test_non_existent')
        mock_pytest_main.assert_not_called()

您可以模拟
pytest.main
,然后验证它是否被调用。我会尝试一下,谢谢。我认为我做得不对。我创建了mock_pytest=patch('pytest.main'),然后对添加的mock_get.assert_调用。这将始终通过,因此我必须错误地实现。当调用
mock\u get.assert\u called()
时,检查测试中是否调用了
get\u single\u test\u names()
;您需要使用正确的mock来检查是否调用了
pytest.main
。我已经添加了一个答案,应该给你一个模拟的例子,并断言模拟被调用。非常感谢,这现在是有意义的。谢谢你的帮助!很好,很高兴我能帮忙!