python 2.7 - adding parameters to pytest setup function -


i trying learn pytest, , i'm running snag. need define variable in setup_method defined via command line parameter. able in individual tests no problem, trying same thing in setup method fails. message -

typeerror: setup_method() takes 3 arguments (2 given) 

am doing wrong? appreciated.

here conftest.py

# conftest.py import pytest  def pytest_addoption(parser):     parser.addoption("--var_one", action="store", default=false)     parser.addoption("--var_two", action="store", default=false)  @pytest.fixture def var_one(request):     var_one_param = request.config.getoption("--var_one")     return var_one  @pytest.fixture def var_two(request):     var_two_param = request.config.getoption("--var_two")     return var_two_param 

actualy running test -

py.test --var_one=true --var_two==true tests.py  

and here file, hope variables defined , called when file executed -

import pytest  class tests:      def setup_method(self, method, var_one):         if var_one == true:             # stuff         else:             # other stuff      def test_one(self, var_two):         assert var_two == true      def teardown_method(self):         #not needed here         pass 

the .setup_method() , .teardown_method() functions nose-compatibility options , not accept fixtures parameters. better use autouse fixture on class:

class tests:      @pytest.fixture(autouse=true)     def do_stuff_based_on_var_one(self, var_one):         if var_one:             # stuff         else:             # other stuff      def test_one(self, var_two):         assert var_two true 

as sidenote, notice comparing booleans == considered bad practice, in conditions use it's implied truth value. if need compare acknowledge it's singleton nature , use object identity is.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -