我在测试一段无法理解的Python代码时遇到了一个问题。在下面的代码中,我测试一个模块的方法,该方法在发现提交时返回200,在未找到提交时返回404。获取提交的方法是我正在测试的模块的外部,因此必须在测试中模拟它。
当下面的代码运行时,第二个测试失败,因为返回的状态代码是200。当我打印analysis_result时,它打印第一个测试中提供的get_single_submission的模拟值。如果我注释掉第一个测试,测试套件就可以通过了。我很感谢大家的意见和建议!
测试代码:

class AnalysisTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        pass

    def tearDown(self):
        pass

    @mock.patch('lib.retention.Submissions')
    @mock.patch('lib.retention.Analysis')
    def test_analysis_get_submission(self, mock_Analysis, mock_Submissions):
        mock_Submissions.return_value.get_single_submission.return_value = Submission(
            md5="md5",
            analyze_completed="01-23-12"
        )

        from api import analysis
        test_event = {
            "pathParameters": {
                "query": ["test"],
                "id": 1
            }
        }

        analysis_result = analysis.analysis_get(test_event)
        print(analysis_result)

        self.assertEqual(analysis_result["statusCode"], 200)

    @mock.patch('lib.retention.Submissions')
    @mock.patch('lib.retention.Analysis')
    def test_analysis_get_submission_not_found(self, mock_Analysis, mock_Submissions):
        mock_Submissions.return_value.get_single_submission.return_value = None

        from api import analysis
        test_event = {
            "pathParameters": {
                "query": ["test"],
                "id": 1
            }
        }

        analysis_result = analysis.analysis_get(test_event)
        print("not found")
        print(analysis_result)

        self.assertEqual(analysis_result["statusCode"], 404)

和pytest结果:
@mock.patch('lib.retention.Submissions')
@mock.patch('lib.retention.Analysis')
def test_analysis_get_submission_not_found(self, mock_Analysis, mock_Submissions):
    mock_Submissions.return_value.get_single_submission.return_value = None

    from api import analysis
    test_event = {
        "pathParameters": {
            "query": ["test"],
            "id": 1
        }
    }

    analysis_result = analysis.analysis_get(test_event)
    print("not found")
    print(analysis_result)

  self.assertEqual(analysis_result["statusCode"], 404)

断言者:200!=404个
api/tests/analysis_test.py:72: AssertionError
------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------
not found
{'statusCode': 200, 'isBase64Encoded': False, 'headers': {'Content-Type': 'application/json'}, 'body': '{\n "metadata": {\n  "md5": "md5",\n  "analyzed_date": "01-23-12"\n }\n}'}

更新:
文件结构
project/lib/retention.py
       /api/analysis.py
           /tests/analysis_test.py

更新2:
我在Submissions中打印出analysis_get,发现两个测试都在使用同一个mock
<MagicMock name='Submissions' id='4599620440'>

最佳答案

上面代码中的问题是,我假设每个测试都可以通过在每个测试中声明analysis来导入自己的from api import analysis模块的新版本。实际上,这只是第一次导入analysis而不是随后导入。由于只有一个导入,因此只有第一个mock_提交被修补到类中,因为Submissionsanalysis模块中的有状态组件。解决方案是编辑代码以使用reload包在每个测试中重新加载导入。

from importlib import reload
class AnalysisTest(unittest.TestCase):

@classmethod
@mock.patch('lib.retention.Submissions')
@mock.patch('lib.retention.Analysis')
def setUpClass(cls):
    from api import analysis

@classmethod
def tearDownClass(cls):
    pass

def setUp(self):
    pass

def tearDown(self):
    pass

@mock.patch('lib.retention.Submissions')
@mock.patch('lib.retention.Analysis')
def test_analysis_get_submission(self, mock_Analysis, mock_Submissions):
    mock_Submissions.return_value.get_single_submission.return_value = Submission(
        md5="md5",
        analyze_completed="01-23-12"
    )

    from api import analysis
    reload(analysis)
    test_event = {
        "pathParameters": {
            "query": ["test"],
            "id": 1
        }
    }

    analysis_result = analysis.analysis_get(test_event)
    print(analysis_result)

    self.assertEqual(analysis_result["statusCode"], 200)

@mock.patch('lib.retention.Submissions')
@mock.patch('lib.retention.Analysis')
def test_analysis_get_submission_not_found(self, mock_Analysis, mock_Submissions):
    mock_Submissions.return_value.get_single_submission.return_value = None

    from api import analysis
    reload(analysis)
    test_event = {
        "pathParameters": {
            "query": ["test"],
            "id": 1
        }
    }

    analysis_result = analysis.analysis_get(test_event)
    print("not found")
    print(analysis_result)

    self.assertEqual(analysis_result["statusCode"], 404)

10-08 04:22