2017-02-21 51 views
2

測試看起來是這樣的:Pytest報告測試用unittest.skip跳過如通過

import unittest 

class FooTestCase(unittest.TestCase): 

    @unittest.skip 
    def test_bar(self): 
     self.assertIsNone('not none') 

當使用pytest運行,該報告看起來是這樣的:

path/to/my/tests/test.py::FooTestCase::test_bar <- ../../../../../usr/lib/python3.5/unittest/case.py PASSED 

。另一方面,如果我將@unittest.skip替換爲@pytest.mark.skip,則將其正確地報告爲跳過:

path/to/my/tests/test.py::FooTestCase::test_bar <- ../../../../../usr/lib/python3.5/unittest/case.py SKIPPED 

如果有人能說,我做錯了什麼或者是pytest中的錯誤?

+2

看起來你需要調用'unittest.skip'。試試'@ unittest.skip()'。 – vaultah

回答

2

unittest.skip()裝飾需要一個參數:

@unittest.skip(reason)

無條件跳過裝飾測試。 原因應該描述爲什麼 測試正在被跳過。

它的使用是在發現自己的examples

class MyTestCase(unittest.TestCase): 

    @unittest.skip("demonstrating skipping") 
    def test_nothing(self): 
     self.fail("shouldn't happen") 

因此unittest.skip不是一個裝飾本身,而是一種裝飾廠 - 實際的裝飾就像調用unittest.skip的結果獲得。

這就解釋了爲什麼您的測試通過了,而不是被跳過或失敗,因爲它實際上是等同於以下:

import unittest 

class FooTestCase(unittest.TestCase): 

    def test_bar(self): 
     self.assertIsNone('not none') 

    test_bar = unittest.skip(test_bar) 
    # now test_bar becomes a decorator but is instead invoked by 
    # pytest as if it were a unittest method and passes