2016-09-16 59 views
1

我有一個可以與兩個不同數據庫配合使用的組件,並且有兩種不同的模式,功能上的細微差別取決於使用哪種數據庫和哪種模式。我有一組測試,通常應該針對數據庫/模式的每個組合運行,但是有些測試僅適用於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

是的。另一種方法是使用'pytest_generate_tests'鉤子​​,就像我爲[aiopg測試套件](https://github.com/aio-libs/aiopg/blob/master/tests/conftest.py#L101-L110)所做的那樣,但也許它是過度的爲你的情況。 –

+0

嗯,考慮到我會有相當多的這些測試和兩個以上的維度,我可能會嘗試'pytest_generate_tests'路線。您能否將其添加到答案中,最好指出兩種解決方案的缺點和優點? (順便說一下,aiopg的例子非常好,謝謝你連接它) – liori