2016-07-26 84 views
0

我有一些python代碼和一堆pytest測試。爲什麼py.test將我的xfail測試顯示爲跳過?

有些我想到失敗(即我測試錯誤處理,所以我通過這應該引起我的代碼拋出異常投入。)

爲了做到這一點的測試,我有一些代碼像這樣:

CF_TESTDATA =[('data/1_input.csv', 'data/1_input.csv', 'utf-8'), 
       pytest.mark.xfail(('data/1_input_unknown_encoding.txt', '', 'utf-8')), 
       ('data/1_input_macintosh.txt', 'data/1_converted_macroman.csv', 'macroman')] 
@pytest.mark.parametrize('input_file, expected_output, encoding', CF_TESTDATA) 

def test_convert_file(testdir, tmpdir, input_file, expected_output, encoding): 
    ''' 
    Test the function that converts a file to UTF-8. 
    ''' 
... 

這裏的想法是,本次測試的第二輪,與INPUT_FILE =「數據/ 1_input_unknown_encoding.txt」,我希望被測代碼失敗。

這似乎工作正常,並且當我從命令行運行時,pytest告訴我測試xfailed。我可以按照調試器中的代碼看到它拋出了預期的異常。所以這一切都很好。

但詹金斯顯示這項測試爲skkipped。當我看到輸出時,我看到消息:

Skip Message 

expected test failure 

爲什麼測試顯示爲跳過?這似乎是測試運行,並按預期失敗。這與正在跳過的測試不同。

+0

當你從詹金斯跑,你從相同的當前目錄運行它作爲交互式運行? –

+0

我猜Jenkins讀取JUnitXML輸出?我猜JUnitXML根本沒有辦法說xfail,所以pytest使用skip。但我其實並不知道。 –

回答

1

答案是我錯誤地使用了xfail。它不應該用來預期測試失敗。它不這樣做。如果測試失敗或不失敗,則記錄爲通過。

我需要做的是改變測試以捕捉我期望的異常,並且在不拋出異常的情況下出錯。然後我將刪除xfails。

xfail似乎適用於您暫時失敗並且您希望測試套件通過的情況。所以它不會期望失敗,因爲不關心失敗。

雖然測試仍在運行,但不會檢查結果,因此報告顯示此測試已跳過是合理的。

我現在的測試是這樣的:

# Test the run function. 
ex1 = False 
try: 
     result = main.run(base_dir + '/'+ input_file, \ 
       client_config['properties']) 
except ValueError as e: 
     ex1 = True 
     print e 

if expect_fail: 
     assert ex1 
else: 
     assert not ex1 
     assert filecmp.cmp(result, base_dir + '/' + expected_output), \ 
      "Ouput file is not the same as the expected file: " + expected_output 
+0

請參閱http://doc.pytest.org/en/latest/assert.html#assertions-about-expected-exceptions – Daenyth

+0

它應該用於預期失敗,問題在於,當測試代碼不應該失敗時行爲正確(引發異常)。你的測試只有在代碼沒有正確行爲時纔會失敗(未能拋出異常)。 –