我有一些使用pytest和fixtures编写的测试,例如:
class TestThing:
@pytest.fixture()
def temp_dir(self, request):
my_temp_dir = tempfile.mkdtemp()
def fin():
shutil.rmtree(my_temp_dir)
request.addfinalizer(fin)
return my_temp_dir
def test_something(self, temp_dir)
with open(os.path.join(temp_dir, 'test.txt'), 'w') as f:
f.write('test')
当从shell中调用测试时,这个很好用,例如:
$ py.test
但我不知道如何在Python/IPython会话中运行它们;尝试类似于
tt = TestThing()
tt.test_something(tt.temp_dir())
失败是因为temp_dir
需要传递一个request
对象。那么,如何使用注入request
对象的fixture?
!py.test
)就可以在iPython中运行... - Dairpy.test
需要你在包含测试脚本的目录中,这在很大程度上破坏了它的目的。此外,它也不允许你访问回溯和 Python 交互式调试器,而直接交互地运行函数才是真正的目的。 - keflavich