Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pytest中测试类的参数化_Python_Pytest - Fatal编程技术网

Python pytest中测试类的参数化

Python pytest中测试类的参数化,python,pytest,Python,Pytest,我有一个类来测试我的一些代码。我希望参数化设置并使用不同的参数重新运行该类: 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.plasm

我有一个类来测试我的一些代码。我希望参数化设置并使用不同的参数重新运行该类:

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

我希望以1000步为单位从t=2000运行这个类到t=20000。

创建一个参数化测试夹具,而不是您的设置函数:

ts = range(2000, 20001, 1000)  # This creates a list of numbers from 2000 to 20000 in increments of 1000.

@pytest.fixture(params=ts)
def plasma(request):
    return plasma.LTEPlasma.from_abundance(request.param, {'Si':1.0}, 1e-13, atom_data, 10*86400)
“参数化测试夹具”是指当您在测试用例中使用它时,pytest将为每个参数创建一个新的测试用例,并分别运行每个参数

您可以通过将名为“plasma”的函数参数添加到每个需要它的测试函数来使用测试夹具:

class TestNormalLTEPlasma:

    def test_beta_rad(self, plasma):
        assert plasma.beta_rad == 1 / (10000 * constants.k_B.cgs.value)

    def test_t_electron(self, plasma):
        assert plasma.t_electron == 0.9 * plasma.t_rad

    def test_saha_calculation_method(self, plasma):
        assert plasma.calculate_saha == plasma.calculate_saha_lte
pytest负责收集fixture,收集测试函数,确定哪些测试函数需要哪些fixture,并将fixture值传递给测试函数执行


查看文档了解更多详细信息:

您还可以应用
参数化
您的类,以便将相同的数据发送到类中的所有测试方法

首先,创建一个列表
plasmas
,其中包含要传递到每个测试的plasma元素。第二,使用decorator
@pytest.mark.parametrize
,并将
等离子体传递给它

plasmas = [plasma.LTEPlasma.from_abundance(t, {'Si':1.0}, 1e-13, atom_data, 10*86400) for t in range(2000, 20001, 1000)]

@pytest.mark.parametrize('plasma', plasmas)
class TestNormalLTEPlasma:
    def test_beta_rad(self, plasma):
        assert plasma.beta_rad == 1 / (10000 * constants.k_B.cgs.value)

    def test_t_electron(self, plasma):
        assert plasma.t_electron == 0.9 * plasma.t_rad

    def test_saha_calculation_method(self, plasma):
        assert plasma.calculate_saha == plasma.calculate_saha_lte

你试过什么?有一些文档就是这样做的。@WolfgangKerzendorf,我为你的问题添加了一个答案。可以使用类级参数化装饰器将参数传递给unittest.TestCase类的init方法吗?>@pytest.mark.parametize('test_-input',[1,2,3,4])类TestClass(unittest.TestCase):def u-init_uuuuuuuuuuuuuuuuuuuuuuuu(self,方法名称,测试输入):super()。uu-init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu