PytestCollectionWarning: cannot c ollect test class 'Test_ClareTestClass' because it has a __init__ constructor (from: test_ClareTestClass.py)
今天打算開始使用pytest,然后突然發現個問題,就是如果我的類是以 Test_開頭的,pytest是 怎么執行我類下邊的方法呢? 然后就做了個測試,類下邊包含了
def __init__(self) 方法,然后就得到了上邊這個warning信息,然后百度,然后各種查詢?
然后看到一個表完整的解釋是:1. Test 開頭的類下邊是不能包含 初始化方法的,如果是你確實就想要你的類名包含Test,那就修改你的過濾測試類的方法 2. 如果你是想把這個 class當做一個收集 測試用例的容器的話,就去掉 初始化的__init__方法 3. 如果你確實需要你的測試方法開始執行的時候,初始化一些數據,那你應該用pytest提供的
setup_module setup_session setup_class setup_method 這些方法下邊去初始化 ,這些初始化的級別對應有結束的 teardown 方法
模塊級別 寫在模塊的開頭
def setup_module(): print("初始化。。。")
def teardown_module(): print("清理。。。")
函數級別的,寫在函數調用之前
def setup_function(): print("初始化。。。")
def teardown_function(): print("清理。。。")
類級別的,寫在類的開頭
def setup_class(self): print("初始化。。。")
def teardown_class(self): print("清理。。。")
方法級別的,也是寫在類的開頭
def setup_method(self): print("初始化。。。")
def teardown_method(self): print("清理。。。")