parametrizing test classes in pytest

后端 未结 2 1103
清酒与你
清酒与你 2021-02-18 21:28

I have a class for testing some of my code. I would like to parametrize the setup and rerun the class with different parameters:

class TestNormalLTEPlasma:


            


        
2条回答
  •  故里飘歌
    2021-02-18 22:10

    You can also apply parametrize your class, so the same data will be sent to all test methods in the class.

    First, create a list plasmas that contains the plasma elements you want to pass to each test. Second, use the decorator @pytest.mark.parametrize, and pass plasmas to it.

    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
    

提交回复
热议问题