2017-02-16 141 views
2

我試圖獲得testfixtures測試與pytest傳球,但它一直試圖收集事情是不是測試:我怎樣才能讓pytest忽略不包含unittest的Test *類?

======================================================= pytest-warning summary ======================================================== 
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_comparison.py cannot collect test class 'TestClassA' because it has a __init__ constructor 
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_components.py cannot collect test class 'TestComponents' because it has a __init__ constructor 
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_datetime.py cannot collect test class 'TestTZInfo' because it has a __new__ constructor 
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_datetime.py cannot collect test class 'TestTZ2Info' because it has a __new__ constructor 
cannot collect test class 'TestContainer' because it has a __init__ constructor 
cannot collect test class 'TestTZInfo' because it has a __new__ constructor 

我怎樣才能得到pytest只收集繼承單元測試測試*類。測試用例?

回答

0

Pytest使用全局樣式名稱模式來收集測試,並且可以通過配置文件中的選項python_filespython_classespython_functions來自定義發現。

我認爲,默認的方式是這樣的:

[pytest] 
python_files=test_*.py 
python_classes=Test* 
python_functions=test_* 

也許你可以只定制它不通過重寫收集類:

# pytest.ini (or in tox.ini, or in setup.cfg) 
[pytest] # if using setup.cfg, this section name should instead be [tool:pytest] 
python_classes=NoThanks 

注:它並不侷限於一個模式在這裏,你可以指定它們的列表。

繼承unittest.TestCase的類仍應收集,不管此選項是。這是因爲unittest框架本身被用來收集這些測試。

1

我發現這個在Pytest 2.6 release notes

支持鼻式__test__屬性上的模塊,類和函數,包括單元測試樣式類。如果設置爲False,則不會收集測試。

這可以幫助,如果你的類是收集,當你不希望它是,但它不與幫助「無法收集測試類,因爲它有一個__init__構造」的警告。

+0

這不適用於pytest發佈版本的類,它不會繼承unittest.TestCase –