Rah*_*hul 5 python pytest python-2.7 python-3.x
我有一个简单的测试课
@pytest.mark.incremental
class TestXYZ:
def test_x(self):
print(self)
def test_y(self):
print(self)
def test_z(self):
print(self)
Run Code Online (Sandbox Code Playgroud)
运行此命令时,将得到以下输出:
test.TestXYZ对象位于0x7f99b729c9b0
test.TestXYZ对象位于0x7f99b7299b70
位于0x7f99b7287eb8的testTestXYZ对象
这表明在TestXYZ对象的3个不同实例上调用了3个方法。无论如何,有没有改变这种行为并使pytest在同一个对象实例上调用所有3个方法。这样我就可以使用self来存储一些值。
小智 6
Sanju 在评论中给出了上面的答案,我想引起人们对这个答案的关注并提供一个例子。在下面的示例中,您使用类的名称来引用类变量,您还可以使用相同的语法来设置或操作值,例如在测试函数中设置z或更改 的值。ytest_x()
class TestXYZ():
# Variables to share across test methods
x = 5
y = 10
def test_x(self):
TestXYZ.z = TestXYZ.x + TestXYZ.y # create new value
TestXYZ.y = TestXYZ.x * TestXYZ.y # modify existing value
assert TestXYZ.x == 5
def test_y(self):
assert TestXYZ.y == 50
def test_z(self):
assert TestXYZ.z == 15
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
601 次 |
| 最近记录: |