【发布时间】:2021-04-15 12:00:01
【问题描述】:
我正在尝试创建可重用的 PyTest 函数,这些函数在类中传递一组对象。目前,我已经编写了大约 12 个测试来断言某些条件,最值得注意的是,对象是否为 None,object 是否不是 None,Object 是 str 实例,Object 是否不是 str 实例。所以我想用 12 个函数来做几乎完全相同的事情,我可以使用 @pytest.mark.paramtrize 将这个数字减少到 4 个可重复使用的函数。
最后,到了问题的核心。我创建了一个如下所示的数据集并将数据集传递给 paramtrize 函数。在下面的示例中,我尝试传递夹具返回对象,并且我还尝试使用() 直接调用夹具,这导致直接调用夹具的警告。
代码
def setup_nifExplorer():
'''Returns a Nif Explorer Instance'''
explorer = NifExplorer()
explorer.BlockType = NifFormat.NiNode
explorer.ResultPath = None
explorer.SearchPath = None
return explorer
@pytest.mark.usefixtures("setup_nifExplorer")
class TestNifExplorer:
@pytest.mark.paramtrize('object', [setup_nifExplorer.BlockType, setup_nifExplorer.SearchPath, setup_nifExplorer.ResultPath])
def test_Object_Is_None(self, object):
assert object != None
def test_Class_Is_None(self, setup_nifExplorer):
assert setup_nifExplorer == None
def test_BlockType_Not_None(self, setup_nifExplorer):
assert setup_nifExplorer.BlockType != None
def test_BlockType_Is_None(self, setup_nifExplorer):
assert setup_nifExplorer.BlockType == None
def test_BlockType_Is_String(self, setup_nifExplorer):
assert isinstance(setup_nifExplorer.BlockType, str)
def test_BlockType_Not_String(self, setup_nifExplorer):
assert not isinstance(setup_nifExplorer.BlockType, str)
def test_SearchPath_Not_None(self, setup_nifExplorer):
assert setup_nifExplorer.SearchPath != None
def test_SearchPath_Is_None(self, setup_nifExplorer):
assert setup_nifExplorer.SearchPath == None
def test_SearchPath_Is_String(self, setup_nifExplorer):
assert isinstance(setup_nifExplorer.SearchPath, str)
def test_SearchPath_Not_String(self, setup_nifExplorer):
assert not isinstance(setup_nifExplorer.SearchPath, str)
def test_SearchPath_Directory_Does_Exist(self, setup_nifExplorer):
assert os.path.exists(setup_nifExplorer.SearchPath) == True
def test_SearchPath_Directory_Does_Not_Exist(self, setup_nifExplorer):
if setup_nifExplorer.SearchPath == None or os.path.exists(setup_nifExplorer.SearchPath) == False:
assert True
def test_ResultPath_Not_None(self, setup_nifExplorer):
assert setup_nifExplorer.ResultPath != None
def test_ResultPath_Is_None(self, setup_nifExplorer):
assert setup_nifExplorer.ResultPath == None
def test_ResultPath_Is_String(self, setup_nifExplorer):
assert isinstance(setup_nifExplorer.ResultPath, str)
def test_ResultPath_Not_String(self, setup_nifExplorer):
assert not isinstance(setup_nifExplorer.ResultPath, str)
def test_ResultPath_Directory_Does_Exist(self, setup_nifExplorer):
assert os.path.exists(setup_nifExplorer.ResultPath) == True
def test_ResultPath_Directory_Does_Not_Exist(self, setup_nifExplorer):
if setup_nifExplorer.ResultPath == None or os.path.exists(setup_nifExplorer.ResultPath) == False:
assert True
if __name__ == "__main__":
pytest.main()
我尝试过的: 在大量的谷歌搜索中,我在 GitHub 上遇到了一个作为功能请求的未解决问题,该问题仍然未解决。这个问题是在 2013 年提出的。https://github.com/pytest-dev/pytest/issues/349
别误会,其他测试有效,我只是想减少不必要的功能。
我正在使用 Visual Studio 2019 社区以及 Python 3.9.1 和 PyTest 6.2.1。
编辑 1
@pytest.mark.parametrize('funcs', [SearchPath_None, ResultPath_None, BlockType_None])
def test_NifExplorer_Variables_None(self, setup_nifExplorer, funcs):
funcs(self,setup_nifExplorer)
def SearchPath_None(self, setup_nifExplorer):
assert setup_nifExplorer.SearchPath == None
def ResultPath_None(self, setup_nifExlorer):
assert setup_nifExlorer.ResultPath == None
def BlockType_None(self, setup_nifExplorer):
assert setup_nifExplorer.BlockType == None
【问题讨论】:
-
现在您正在尝试访问夹具函数本身的属性,这没有意义,并且您的方法名称中有错字(parame三重奏)。但是如果你用三个值的列表来调用它,你的测试会运行三次——我认为你想要的不止这些。通常,您会将参数化测试分组,以便组中的所有测试都断言相同的事情,否则测试最终会充满条件逻辑。
-
@jonrsharpe 当您提到将测试分组在一起时,您指的是像 Edit 1 这样的东西吗?如果不是,请您澄清一下。