Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pytest中对多个值迭代test_函数?_Python_Linux_Integration Testing_Pytest_Python Unittest - Fatal编程技术网

Python 如何在pytest中对多个值迭代test_函数?

Python 如何在pytest中对多个值迭代test_函数?,python,linux,integration-testing,pytest,python-unittest,Python,Linux,Integration Testing,Pytest,Python Unittest,我想知道如何在pytest中对不同的值迭代测试函数()? 例如 现在,当我运行pytest-k test_方法时, 它只显示了一个通过的测试用例。 但我希望所有4个用例都使用单个函数运行,并且需要在输出中传递4个测试用例。 我怎样才能做到呢?试试看 它有多种类型。将值作为元组传递,并将值作为参数传递给测试函数 commands = ['ls','ps', 'df' ,'du'] #list of Linux commands @pytest.mark.parametrize("cmds

我想知道如何在pytest中对不同的值迭代测试函数()? 例如

现在,当我运行pytest-k test_方法时, 它只显示了一个通过的测试用例。 但我希望所有4个用例都使用单个函数运行,并且需要在输出中传递4个测试用例。 我怎样才能做到呢?

试试看


它有多种类型。将值作为元组传递,并将值作为参数传递给测试函数

commands = ['ls','ps', 'df' ,'du'] #list of Linux commands
@pytest.mark.parametrize("cmds",commands)
def test_method(cmds):
   r=subprocess.check_output(cmds)
   if r:
      assert True
   else:
      assert False
下面是作为4个独立测试运行的输出

 test_sflow.py::test_method[ls] PASSED [ 25%]                                                                                                                                                                                                          
 test_sflow.py::test_method[ps] PASSED [ 50%]                                                                                                                                                                                                         
 test_sflow.py::test_method[df] PASSED [ 75%]                                                                                                                                                                                                       
 test_sflow.py::test_method[du] PASSED [ 100%]

不要迭代函数,而是在函数内部迭代该值。我的意思是,在测试函数中的所有列表值上运行你的函数,这不是一件理想的事情。好吧,通过这样做,我只通过了一个测试用例,但我有多个值。那么,在这种情况下,是否可以获得像4个测试用例这样的输出呢?您可以,但这不是您应该如何进行单元测试。您的单元测试应该相互排斥。如果测试的所有值都有其他意义,那么您应该为每个值编写单独的测试。对,但是测试方法在所有4个测试用例中都是通用的,所以我不想让它成为冗余的,因为它应该使用单个函数来执行多个命令。为同一个测试方法编写多个函数不是一个好主意。那么为什么要为4个不同的输入测试它呢?如果它是公共的,并且测试通过了一个输入,那么就没有必要对不同的输入进行相同的测试。那么如果我的函数是类函数呢?如何在类函数中使用参数化?@DhavalMehta然后修饰方法,而不是类。好的,我能得到一些线索吗?我不知道这个方法。蒂亚。
 test_sflow.py::test_method[ls] PASSED [ 25%]                                                                                                                                                                                                          
 test_sflow.py::test_method[ps] PASSED [ 50%]                                                                                                                                                                                                         
 test_sflow.py::test_method[df] PASSED [ 75%]                                                                                                                                                                                                       
 test_sflow.py::test_method[du] PASSED [ 100%]