简体   繁体   中英

How can I iterate over test_function in pytest for multiple values?

I want to know how can I iterate over a test_funtion() in pytest for different values? Eg.

list = ['ls','ps', 'df' ,'du'] #list of Linux commands
def test_method(self):
    for I in list:
       r=subprocess.check_output(I)
       if r:
          assert True
       else: 
          assert False

Now when I run pytest -k test_method, It shows me only one test case passed. But I want all 4 cases ran using single function and need 4 test cases passed in output. How can I achieve it?

expanding on previous answer by @Nithin-Mohan

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

Here is the output running as 4 separate tests

 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%]

Try pytest parameterization

It has got a variety of types. Pass values as tuples and pass to the test functions as arguments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM