2

First of all, my English is poor and I'm new here. I'm sorry if I do something wrong.

My question is if I have multiple parameters, but I only want call setup_method once for every test_function.

However, it call setup every parameters (test case). I don't know how to solve this problem.

Here is my code:

class Test_Class:

    def setup_method(self):    
        print("==========set up===========")


    @pytest.mark.parametrize('people', ['Mike','Alice','Bob'])
    @pytest.mark.parametrize('say', ['Hi','how is going on'])
    def test_function(self, people, say):
        print(people, say)


Here is the result

==========set up===========
Mike Hi

==========set up===========
Alice Hi

==========set up===========
Bob Hi

==========set up===========
Mike how is going on

==========set up===========
Alice how is going on

==========set up===========
Bob how is going on

But I want the result like this:

==========set up===========
Mike Hi

Alice Hi

Bob Hi

==========set up===========
Mike how is going on

Alice how is going on

Bob how is going on

The setup function is called with the next parameter.

Any suggestions will be greatly helpful

Thanks

RD_TomShen
  • 21
  • 2

1 Answers1

0

I'm not sure that your approach is correct that is the root cause. Usually, default parametrizing with list or key:value approach will be enough, try to deep investigate your aims. From my opinion you need to look at key:value approach, for example:

>>> a = ['Mike','Alice','Bob']
>>> b = ['Hi','how is going on']
>>> c = {x:a for x in b}
>>> c
{'Hi': ['Mike', 'Alice', 'Bob'], 'how is going on': ['Mike', 'Alice', 'Bob']}

approach -> Action: [list of names]

Also, you can prepare your data using pre-condition, for example in conftest.py, please read my answer here: https://stackoverflow.com/a/60430631/4361977

If my suggestions is not actual, so please provide real example and I will try to help you.

ChantOfSpirit
  • 157
  • 1
  • 1
  • 9