2013-11-14 72 views
1

是否有可能通過夾具來pytest生成測試?py.test 2.3.5:使用固定裝置生成測試?

import py.test 

@py.test.fixture(scope="module") 
def fixture(): 
    return True 

def test_1(fixture): 
    def checker(datum): 
     assert datum == fixture 
    for i in [True, True, True]: 
     # Does not work. 
     yield checker, i 
     # Does work. 
     #checker(i) 

以上代碼生成

>  for i, x in enumerate(self.obj()): 
      name, call, args = self.getcallargs(x) 
E   TypeError: test_1() takes exactly 1 argument (0 given) 

我們使用py.test 2.3.5從Debian的。

回答

3

不知道到底是什麼yield測試定義裏面想的事情。

有一個在燈具Fixture functions using 「yield」/context manager integration作爲一個希望在第一次的作品不yield

如果要對燈具的序列來遍歷同樣的測試,你可能需要Parametrizing a fixture

+0

它應該產生大量的測試對一組參數。不知怎的,我用它,因爲我曾經在幾篇博客看到它,但顯然這種方法有利於'@ pytest.mark.parametrize'的棄用。 –