【发布时间】:2019-03-14 00:31:52
【问题描述】:
有没有使用pytest 固定装置(尤其是pytest-tmpdir)的好方法,只有在测试通过时才进行清理?
我正在测试一些 terraform,并希望保留带有状态文件的测试目录,以防万一失败,我必须清理 aws 资源。
我可以使用xtest 样式,但宁愿不要。
不知道如何在 send 中使用 yield 语法,但似乎可行。
我现在是
@pytest.fixture(scope='function')
def tf_ut():
tmp_dir = tempfile.mkdtemp(dir=test_root) # test_root is a session dir that contains the test dirs
logging.debug('test fixture directory: %s', tmp_dir)
shutil.copy(os.path.join(PROJECT_ROOT, 'terraform-provider-http'), tmp_dir)
shutil.copy(os.path.join(PROJECT_ROOT, 'terraform-provider-bwafapi'), tmp_dir)
tf = Terraform(working_dir=tmp_dir)
tf.init(PROJECT_ROOT)
return tf
def test_plan_default(tf_ut):
ret, out, err = tf_ut.init()
assert ret is 0
ret, out, err = tf_ut.plan(PROJECT_ROOT, var_file=os.path.join(PROJECT_ROOT, 'presets/stsdev-dms.tfvars'))
assert 'Terraform will perform the following actions:' in out
shutil.rmtree(tf_ut.working_dir)
【问题讨论】: