擅长:python、mysql、java
<p>一种方法是在测试方法中创建对象并从中与之交互:</p>
<pre><code>def test_action(self, x):
o = SuperCool()
assert o.action(2) == 4
</code></pre>
<p>显然,您可以使用下面的方法使用类似于经典的<code>setup</code>和<code>teardown</code>样式的unittest:<a href="http://doc.pytest.org/en/latest/xunit_setup.html" rel="nofollow noreferrer">http://doc.pytest.org/en/latest/xunit_setup.html</a></p>
<p>我不能百分之百确定它们是如何使用的,因为pytest的文档是非常糟糕的。</p>
<p><strong>编辑:</strong>很明显,如果你做了</p>
<pre><code>class TestSuperCool():
def setup(self):
self.sc = SuperCool()
...
# test using self.sc down here
</code></pre>