import pytest

class TestCase:
    test_dict = {}
    def test_one(self):
        self.test_dict = self.login("user","password")
        assert self.test_dict["cookie"] == '1234'

    def test_two(self, request):
        print("当前内容:", self.test_dict)
        assert self.test_dict["cookie"] == "1234"

    def login(self,user,password):
        return {"cookie":"1234",user:password}

if __name__ == "__main__":
    pytest.main(["-s", "testcase.py"])

其中代码如上,但执行 test_two 方法时会报错,此时 test_one 修改字典后,在 test_two 后获取的字典值仍然为空。
类似这种情况,如果在同一个 pytest 的测试文件中,如果 test_two 想要 test_one 执行登录后生成的某个数值,有什么好的方法?


↙↙↙阅读原文可查看相关链接,并与作者交流