2016-09-16 6 views
1

2つの異なるデータベースと、2つの異なるモードで、どのデータベースが使用され、どのモードが使用されるかによって機能がわずかに異なるコンポーネントがあります。私はデータベース/モードの組み合わせごとにテストを実行する必要がありますが、4つの組み合わせのうち3つでしか動作しないテストがあります。テスト中のフィクスチャのいくつかのパラメータを無視する

私の統合テストは、次のようにモデル化されています。データベース接続を提供するパラメータ化されたフィクスチャと、特定のモードで初期化されたコンポーネントがあります。そして、これらの備品を取って、それぞれのテストは、当然、各組み合わせに対して実行されます。

import pytest 

class TestMyComponent(object): 
    @pytest.fixture(autouse=True, params=['sqlite', 'postgresql']) 
    def database(self, request): 
     if request.param == 'sqlite': 
      return 'Opened in-memory sqlite connection' 
     else: 
      return 'Opened postgresql connection' 

    @pytest.fixture(autouse=True, params=['live', 'batch']) 
    def component(self, request): 
     if request.param == 'live': 
      return 'Component initialized in a live mode' 
     else: 
      return 'Component initialized in a batch mode' 

    def test_empty_db_has_no_items(self): 
     assert True # all four tests pass 

    def test_stream_new_items(self, database, mode): 
     # we don't want to test streaming with sqlite and live mode 
     assert 'postgresql' in database or 'batch' in mode 

py.testサポートされていない組み合わせのテストを実行していないための最善の方法は何ですか?

答えて

1

禁止された組み合わせのテストスキップを上げるのがあなたの問題を解決するように見えます。例えば。

def test_stream_new_items(self, database, mode): 
    # we don't want to test streaming with sqlite and live mode 
    if 'sqlite' in database and 'live' in mode: 
     pytest.skip("streaming with sqlite and live mode is not supported" 
    assert 'postgresql' in database or 'batch' in mode 
+0

したがって、テストは実行されますが、スキップされたとマークされますか? – liori

+0

はい。別の方法は、[aiopg test suite](https://github.com/aio-libs/aiopg/blob/master/tests/conftest.py#L101-L110)のように 'pytest_generate_tests'フックを使用していますが、おそらくそれはオーバーコードですあなたのために。 –

+0

ええ、これらのテストと2つ以上のディメンションがかなりたくさんあるので、私は 'pytest_generate_tests'ルートを試してみるかもしれません。あなたはそれを答えに加えて、両方のソリューションの欠点と利点を指摘できますか? (btw、aiopgの例は非常にいいです、リンクしてくれてありがとう) – liori

関連する問題