2013-07-02 4 views
9

私は自分のコードのいくつかをテストするためのクラスを持っています。py.testはテストクラスをパラメータ化します

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年

+0

何を試しましたか? [パラメータテスト機能](http://pytest.org/latest/parametrize.html#parametrized-test-functions)と[フィクスチャ](http://pytest.org/latest/fixture.html#)のドキュメントがあります。フィクスチャー)。 –

答えて

15

のステップで= 20000をtにトンから= 2000を行くこのクラスを実行したいと思います:私はセットアップをパラメータと異なるパラメータを持つクラスを再実行したいと思います機能、パラメータ化テスト・フィクスチャを作成します。

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は、各パラメータの新しいテストケースを作成し、個別に実行されます、1です。

あなたがそれをしたいテスト機能のそれぞれに「プラズマ」と呼ばれる関数の引数を追加することにより、テスト・フィクスチャを使用します。

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は、テストを考え出す、テスト機能を収集し、備品を集めるの面倒を見ます関数はどのフィクスチャを必要とし、フィクスチャ値をテスト関数に渡して実行します。

詳細については、ドキュメントをご覧ください。http://pytest.org/latest/fixture.html#fixture-parametrize

関連する問題