我有一个类来测试我的一些代码。我想参数化设置并用不同的参数重新运行类:
class TestNormalLTEPlasma:
def setup(self, t=10000):
self.plasma = plasma.LTEPlasma.from_abundance(t, {'Si':1.0}, 1e-13, atom_data, 10*86400)
def test_beta_rad(self):
assert self.plasma.beta_rad == 1 / (10000 * constants.k_B.cgs.value)
def test_t_electron(self):
assert self.plasma.t_electron == 0.9 * self.plasma.t_rad
def test_saha_calculation_method(self):
assert self.plasma.calculate_saha == self.plasma.calculate_saha_lte
我想把这门课从t=2000改为t=20000,每1000步。
创建一个参数化的测试夹具,而不是设置函数:
“参数化测试设备”是这样一种:当您在测试用例中使用它时,pytest将为每个参数创建一个新的测试用例,并分别运行每个测试用例。
通过将名为“plasma”的函数参数添加到每个需要它的测试函数中,可以使用测试设备:
pytest负责收集fixture,收集测试函数,找出哪些测试函数需要哪些fixture,并将fixture值传递给测试函数以供执行。
查看文档了解更多详细信息:http://pytest.org/latest/fixture.html#fixture-parametrize
您还可以应用类
parametrize
,以便将相同的数据发送到类中的所有测试方法。首先,创建一个列表
plasmas
,其中包含要通过每个测试的等离子体元素。其次,使用decorator@pytest.mark.parametrize
,并将plasmas
传递给它。相关问题 更多 >
编程相关推荐