【问题标题】:assert pytest command has been run断言 pytest 命令已运行
【发布时间】:2018-08-16 14:08:10
【问题描述】:

我有一个 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_test 已执行。

执行此操作的最佳方法是什么?模拟和单元测试对我来说是新的。

我试着弄乱标准输出:

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.assert_called调用的。
  • 我试试看,谢谢。
  • 我不认为我这样做是正确的。我创建了 mock_pytest = patch('pytest.main'),然后创建了 mock_get.assert_call。这总会过去,所以我一定是实施不正确。
  • 当调用mock_get.assert_called()时,你检查get_single_test_names()在测试中被调用了;您需要使用正确的模拟来检查是否调用了pytest.main。我添加了一个答案,它应该给你一个模拟和断言模拟被调用的例子。

标签: python pytest


【解决方案1】:

这里有两个示例测试,用于验证 pytest.main 在通过有效测试名称时被调用,否则不会被调用。我还添加了一些mock_pytest_main.assert_called 的不同调用作为示例;它们的作用几乎相同,只是对函数调用时传递的 args 进行了额外检查。希望这可以帮助您编写更复杂的测试!

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()

【讨论】:

  • 非常感谢,现在说得通了。感谢您的帮助!
  • 很好,很高兴我能帮上忙!
猜你喜欢
  • 2020-11-27
  • 2020-05-02
  • 2021-08-14
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
相关资源
最近更新 更多