Skip to content

Latest commit

 

History

History
90 lines (68 loc) · 2.15 KB

VDR108.md

File metadata and controls

90 lines (68 loc) · 2.15 KB

VDR108. Calling functions in parametrization

Functions called in scenario parameters are executed before all tests, resulting in:

  • a delay in the execution of other scenarios,
  • introducing potentially unexpected behavior in tests.

Only contexts’ function call is considered to be a bad practice in order to avoid warnings to object constructors and other third-party libraries.

❌ Anti-pattern

    # contexts/example.py
    def greetings(string):
        print(f"executing greetings({string})")
        return f"Hello, {string}!"
    
    def goodbye(string):
        print(f"executing goodbye({string})")
        return f"Buy, {string}!"
    
    # scenarios/example/lambda.py
    from contexts import greetings, goodbye
    class Scenario(vedro.Scenario):
        subject = "example subject"
    
        @params(greetings("Mike"))
        @params(goodbye("Jane"))
        def __init__(self, tmp):
            self.tmp = tmp
    
    # scenarios/example/another_scenario.py
    class Scenario(vedro.Scenario):
        subject = "another scenario subject"
    
        def given_user(self):
           pass
        
        def when_user_registers(self):
          pass
$ vedro run --subject="another scenario subject"
executing greetings(Mike)
executing goodbye(Jane)
Scenarios
* example
 ✔ another scenario subject (0.0s) 

✅ Best practice

It’s better to use composition or lambda in parametrization.

# scenarios/example/lambda.py
from contexts import greetings, goodbye

class Scenario(vedro.Scenario):
    subject = "example subject"
    
    @params(greetings, "Mike")
    @params(goodbye, "Jane")
    def __init__(self, func, tmp):
        self.tmp = func(tmp)
    
    # or
    @params(lambda: greetings("Mike"))
    @params(lambda: greetings("Mike"))
    def __init__(self, f):
       self.tmp = f()


# scenarios/example/another_scenario.py
class Scenario(vedro.Scenario):
    subject = "another scenario subject"

    def given_user(self):
       pass
    
    def when_user_registers(self):
      pass
$ vedro run --subject="another scenario subject"
Scenarios
* example
 ✔ another scenario subject (0.0s)