@pytest.mark.parametrize('arg', ['', 'arg'])
def test_setup_teardown_function_level_with_optional_argument(testdir, monkeypatch, arg):
 import sys
 trace_setups_teardowns = []
 monkeypatch.setattr(sys, 'trace_setups_teardowns', trace_setups_teardowns, raising=False)
 p = testdir.makepyfile("\n        import pytest\n        import sys\n\n        trace = sys.trace_setups_teardowns.append\n\n        def setup_module({arg}): trace('setup_module')\n        def teardown_module({arg}): trace('teardown_module')\n\n        def setup_function({arg}): trace('setup_function')\n        def teardown_function({arg}): trace('teardown_function')\n\n        def test_function_1(): pass\n        def test_function_2(): pass\n\n        class Test:\n            def setup_method(self, {arg}): trace('setup_method')\n            def teardown_method(self, {arg}): trace('teardown_method')\n\n            def test_method_1(self): pass\n            def test_method_2(self): pass\n    ".format(arg=arg))
 result = testdir.inline_run(p)
 result.assertoutcome(passed=4)
 expected = ['setup_module', 'setup_function', 'teardown_function', 'setup_function', 'teardown_function', 'setup_method', 'teardown_method', 'setup_method', 'teardown_method', 'teardown_module']
 assert (trace_setups_teardowns == expected)

@ pytest . mark . parametrize ( 'arg' , [ '' , 'arg' ] ) def test_setup_teardown_function_level_with_optional_argument ( testdir , monkeypatch , arg ) : import sys trace_setups_teardowns = [ ] monkeypatch . setattr ( sys , 'trace_setups_teardowns' , trace_setups_teardowns , raising = False ) p = testdir . makepyfile ( "\n        import pytest\n        import sys\n\n        trace = sys.trace_setups_teardowns.append\n\n        def setup_module({arg}): trace('setup_module')\n        def teardown_module({arg}): trace('teardown_module')\n\n        def setup_function({arg}): trace('setup_function')\n        def teardown_function({arg}): trace('teardown_function')\n\n        def test_function_1(): pass\n        def test_function_2(): pass\n\n        class Test:\n            def setup_method(self, {arg}): trace('setup_method')\n            def teardown_method(self, {arg}): trace('teardown_method')\n\n            def test_method_1(self): pass\n            def test_method_2(self): pass\n    " . format ( arg = arg ) ) result = testdir . inline_run ( p ) result . assertoutcome ( passed = 4 ) expected = [ 'setup_module' , 'setup_function' , 'teardown_function' , 'setup_function' , 'teardown_function' , 'setup_method' , 'teardown_method' , 'setup_method' , 'teardown_method' , 'teardown_module' ] assert ( trace_setups_teardowns == expected )
parameter to setup/teardown xunit-style functions parameter is now optional .