Use @ pytest.mark.parametrize parameter passing test

Excerpt: https://blog.csdn.net/sunny_happy08/article/details/83658661

 

Recently because of work you need to use @ pytest.mark.parametrize () pass multiple parameters to achieve the implementation of different data.

    I demand
data source is excel, excel each row is a test case, it has achieved reading test logic and processing test cases. The next step is to test data through pytest.mark.parametrize () function is passed the test, the test data to perform these cycles, the results need to assert embodiment with each execution, so that the test results show the results file to pytest (by pytest --html =. / report / report.html)
     feasibility of  
the article has been pointed out in great detail the situation with @ pytest.mark.parametrize) pass multiple parameters to achieve the implementation of different data (. By way of example the last article, we find, pytest cycle is performed automatically, and transparently to the user, the user does not need to write their own circular logic. The test directly throw into the list, it will loop is executed, which is what we expect. Also meets my needs.
      Problems encountered
 security according to this logic, I just want to read out all the data as a list of excel spread test function in on it, waiting for pytest scheduled for execution on their own, and finally assert each execution result is True or False became . Actually: pytest indeed performed each test case, although each case also wrote assert, but pytest all cases as a whole over the implementation of the results returned assert. recording is performed pytest's also only a display case. I expected this result is not the same. How to do?
       Solutions
  can not think of reason, had to bite the bullet to write test code by the result guess why.
  It does not support multiple parameters? Less likely, because obviously written a number of parameters. That is the question a number of parameters wording slightly. Turned code found that, when a plurality of transmission parameters, the second parameter is a list of the plurality of tuples. To list to write. I tuple is parameterized by a parameter go through. With this guesswork, write down code verification, it really is such a way.
  By executing the test code is found, write up a list, is the element of each tuple as a parameter pass in; it is passed by parameter pass in a list, so each element will be to list there is a result, according to the parameter passed just over the implementation of the results on the whole list. ok, problem solved.
       Experiment Code
         

       
          

          Results of the
           

           PS: When there are multiple parameters are the list, and inconsistent length of the list, so to list them would be a little awkward, and record it pytest.mark.parametrize () is limited. Further, when the @ pytest.mark.parametrize () only one parameter, each element of the list is as a parameter passed, will assert the results of each execution.

When a transmission parameter can be two way, see the following specific examples

A wording:

@pytest.mark.parametrize('name', ['sunny', 'kevinse', 'jacky'])

def test_name(name):

    print name

Written two:

name_list = ['sunny', 'kevinse', 'jacky']

@pytest.mark.parametrize('name', name_list)

def test_name(name):

    print name

Guess you like

Origin www.cnblogs.com/fyly/p/11223287.html