Best Python code snippet using pytest
metafunc.py
Source:metafunc.py
...134 assert metafunc._calls[0].params == dict(x=1,y=2)135 assert metafunc._calls[1].params == dict(x=1,y=3)136 def test_parametrize_functional(self, testdir):137 testdir.makepyfile("""138 def pytest_generate_tests(metafunc):139 metafunc.parametrize('x', [1,2], indirect=True)140 metafunc.parametrize('y', [2])141 def pytest_funcarg__x(request):142 return request.param * 10143 def pytest_funcarg__y(request):144 return request.param145 def test_simple(x,y):146 assert x in (10,20)147 assert y == 2148 """)149 result = testdir.runpytest("-v")150 result.stdout.fnmatch_lines([151 "*test_simple*1-2*",152 "*test_simple*2-2*",153 "*2 passed*",154 ])155 def test_parametrize_onearg(self):156 metafunc = self.Metafunc(lambda x: None)157 metafunc.parametrize("x", [1,2])158 assert len(metafunc._calls) == 2159 assert metafunc._calls[0].funcargs == dict(x=1)160 assert metafunc._calls[0].id == "1"161 assert metafunc._calls[1].funcargs == dict(x=2)162 assert metafunc._calls[1].id == "2"163 def test_parametrize_onearg_indirect(self):164 metafunc = self.Metafunc(lambda x: None)165 metafunc.parametrize("x", [1,2], indirect=True)166 assert metafunc._calls[0].params == dict(x=1)167 assert metafunc._calls[0].id == "1"168 assert metafunc._calls[1].params == dict(x=2)169 assert metafunc._calls[1].id == "2"170 def test_parametrize_twoargs(self):171 metafunc = self.Metafunc(lambda x,y: None)172 metafunc.parametrize(("x", "y"), [(1,2), (3,4)])173 assert len(metafunc._calls) == 2174 assert metafunc._calls[0].funcargs == dict(x=1, y=2)175 assert metafunc._calls[0].id == "1-2"176 assert metafunc._calls[1].funcargs == dict(x=3, y=4)177 assert metafunc._calls[1].id == "3-4"178 def test_parametrize_multiple_times(self, testdir):179 testdir.makepyfile("""180 import pytest181 pytestmark = pytest.mark.parametrize("x", [1,2])182 def test_func(x):183 assert 0, x184 class TestClass:185 pytestmark = pytest.mark.parametrize("y", [3,4])186 def test_meth(self, x, y):187 assert 0, x188 """)189 result = testdir.runpytest()190 assert result.ret == 1191 result.stdout.fnmatch_lines([192 "*6 fail*",193 ])194 def test_parametrize_class_scenarios(self, testdir):195 testdir.makepyfile("""196 # same as doc/en/example/parametrize scenario example197 def pytest_generate_tests(metafunc):198 idlist = []199 argvalues = []200 for scenario in metafunc.cls.scenarios:201 idlist.append(scenario[0])202 items = scenario[1].items()203 argnames = [x[0] for x in items]204 argvalues.append(([x[1] for x in items]))205 metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")206 class Test(object):207 scenarios = [['1', {'arg': {1: 2}, "arg2": "value2"}],208 ['2', {'arg':'value2', "arg2": "value2"}]]209 def test_1(self, arg, arg2):210 pass211 def test_2(self, arg2, arg):212 pass213 def test_3(self, arg, arg2):214 pass215 """)216 result = testdir.runpytest("-v")217 assert result.ret == 0218 result.stdout.fnmatch_lines("""219 *test_1*1*220 *test_2*1*221 *test_3*1*222 *test_1*2*223 *test_2*2*224 *test_3*2*225 *6 passed*226 """)227class TestMetafuncFunctional:228 def test_attributes(self, testdir):229 p = testdir.makepyfile("""230 # assumes that generate/provide runs in the same process231 import py, pytest232 def pytest_generate_tests(metafunc):233 metafunc.addcall(param=metafunc)234 def pytest_funcarg__metafunc(request):235 assert request._pyfuncitem._genid == "0"236 return request.param237 def test_function(metafunc, pytestconfig):238 assert metafunc.config == pytestconfig239 assert metafunc.module.__name__ == __name__240 assert metafunc.function == test_function241 assert metafunc.cls is None242 class TestClass:243 def test_method(self, metafunc, pytestconfig):244 assert metafunc.config == pytestconfig245 assert metafunc.module.__name__ == __name__246 if py.std.sys.version_info > (3, 0):247 unbound = TestClass.test_method248 else:249 unbound = TestClass.test_method.im_func250 # XXX actually have an unbound test function here?251 assert metafunc.function == unbound252 assert metafunc.cls == TestClass253 """)254 result = testdir.runpytest(p, "-v")255 result.stdout.fnmatch_lines([256 "*2 passed in*",257 ])258 def test_addcall_with_two_funcargs_generators(self, testdir):259 testdir.makeconftest("""260 def pytest_generate_tests(metafunc):261 assert "arg1" in metafunc.fixturenames262 metafunc.addcall(funcargs=dict(arg1=1, arg2=2))263 """)264 p = testdir.makepyfile("""265 def pytest_generate_tests(metafunc):266 metafunc.addcall(funcargs=dict(arg1=1, arg2=1))267 class TestClass:268 def test_myfunc(self, arg1, arg2):269 assert arg1 == arg2270 """)271 result = testdir.runpytest("-v", p)272 result.stdout.fnmatch_lines([273 "*test_myfunc*0*PASS*",274 "*test_myfunc*1*FAIL*",275 "*1 failed, 1 passed*"276 ])277 def test_two_functions(self, testdir):278 p = testdir.makepyfile("""279 def pytest_generate_tests(metafunc):280 metafunc.addcall(param=10)281 metafunc.addcall(param=20)282 def pytest_funcarg__arg1(request):283 return request.param284 def test_func1(arg1):285 assert arg1 == 10286 def test_func2(arg1):287 assert arg1 in (10, 20)288 """)289 result = testdir.runpytest("-v", p)290 result.stdout.fnmatch_lines([291 "*test_func1*0*PASS*",292 "*test_func1*1*FAIL*",293 "*test_func2*PASS*",294 "*1 failed, 3 passed*"295 ])296 def test_noself_in_method(self, testdir):297 p = testdir.makepyfile("""298 def pytest_generate_tests(metafunc):299 assert 'xyz' not in metafunc.fixturenames300 class TestHello:301 def test_hello(xyz):302 pass303 """)304 result = testdir.runpytest(p)305 result.stdout.fnmatch_lines([306 "*1 pass*",307 ])308 def test_generate_plugin_and_module(self, testdir):309 testdir.makeconftest("""310 def pytest_generate_tests(metafunc):311 assert "arg1" in metafunc.fixturenames312 metafunc.addcall(id="world", param=(2,100))313 """)314 p = testdir.makepyfile("""315 def pytest_generate_tests(metafunc):316 metafunc.addcall(param=(1,1), id="hello")317 def pytest_funcarg__arg1(request):318 return request.param[0]319 def pytest_funcarg__arg2(request):320 return request.param[1]321 class TestClass:322 def test_myfunc(self, arg1, arg2):323 assert arg1 == arg2324 """)325 result = testdir.runpytest("-v", p)326 result.stdout.fnmatch_lines([327 "*test_myfunc*hello*PASS*",328 "*test_myfunc*world*FAIL*",329 "*1 failed, 1 passed*"330 ])331 def test_generate_tests_in_class(self, testdir):332 p = testdir.makepyfile("""333 class TestClass:334 def pytest_generate_tests(self, metafunc):335 metafunc.addcall(funcargs={'hello': 'world'}, id="hello")336 def test_myfunc(self, hello):337 assert hello == "world"338 """)339 result = testdir.runpytest("-v", p)340 result.stdout.fnmatch_lines([341 "*test_myfunc*hello*PASS*",342 "*1 passed*"343 ])344 def test_two_functions_not_same_instance(self, testdir):345 p = testdir.makepyfile("""346 def pytest_generate_tests(metafunc):347 metafunc.addcall({'arg1': 10})348 metafunc.addcall({'arg1': 20})349 class TestClass:350 def test_func(self, arg1):351 assert not hasattr(self, 'x')352 self.x = 1353 """)354 result = testdir.runpytest("-v", p)355 result.stdout.fnmatch_lines([356 "*test_func*0*PASS*",357 "*test_func*1*PASS*",358 "*2 pass*",359 ])360 def test_issue28_setup_method_in_generate_tests(self, testdir):361 p = testdir.makepyfile("""362 def pytest_generate_tests(metafunc):363 metafunc.addcall({'arg1': 1})364 class TestClass:365 def test_method(self, arg1):366 assert arg1 == self.val367 def setup_method(self, func):368 self.val = 1369 """)370 result = testdir.runpytest(p)371 result.stdout.fnmatch_lines([372 "*1 pass*",373 ])374 def test_parametrize_functional2(self, testdir):375 testdir.makepyfile("""376 def pytest_generate_tests(metafunc):377 metafunc.parametrize("arg1", [1,2])378 metafunc.parametrize("arg2", [4,5])379 def test_hello(arg1, arg2):380 assert 0, (arg1, arg2)381 """)382 result = testdir.runpytest()383 result.stdout.fnmatch_lines([384 "*(1, 4)*",385 "*(1, 5)*",386 "*(2, 4)*",387 "*(2, 5)*",388 "*4 failed*",389 ])390 def test_parametrize_and_inner_getfuncargvalue(self, testdir):391 p = testdir.makepyfile("""392 def pytest_generate_tests(metafunc):393 metafunc.parametrize("arg1", [1], indirect=True)394 metafunc.parametrize("arg2", [10], indirect=True)395 def pytest_funcarg__arg1(request):396 x = request.getfuncargvalue("arg2")397 return x + request.param398 def pytest_funcarg__arg2(request):399 return request.param400 def test_func1(arg1, arg2):401 assert arg1 == 11402 """)403 result = testdir.runpytest("-v", p)404 result.stdout.fnmatch_lines([405 "*test_func1*1*PASS*",406 "*1 passed*"407 ])408 def test_parametrize_on_setup_arg(self, testdir):409 p = testdir.makepyfile("""410 def pytest_generate_tests(metafunc):411 assert "arg1" in metafunc.fixturenames412 metafunc.parametrize("arg1", [1], indirect=True)413 def pytest_funcarg__arg1(request):414 return request.param415 def pytest_funcarg__arg2(request, arg1):416 return 10 * arg1417 def test_func(arg2):418 assert arg2 == 10419 """)420 result = testdir.runpytest("-v", p)421 result.stdout.fnmatch_lines([422 "*test_func*1*PASS*",423 "*1 passed*"424 ])425 def test_parametrize_with_ids(self, testdir):426 testdir.makepyfile("""427 import pytest428 def pytest_generate_tests(metafunc):429 metafunc.parametrize(("a", "b"), [(1,1), (1,2)],430 ids=["basic", "advanced"])431 def test_function(a, b):432 assert a == b433 """)434 result = testdir.runpytest("-v")435 assert result.ret == 1436 result.stdout.fnmatch_lines_random([437 "*test_function*basic*PASSED",438 "*test_function*advanced*FAILED",439 ])440 def test_parametrize_without_ids(self, testdir):441 testdir.makepyfile("""442 import pytest443 def pytest_generate_tests(metafunc):444 metafunc.parametrize(("a", "b"),445 [(1,object()), (1.3,object())])446 def test_function(a, b):447 assert 1448 """)449 result = testdir.runpytest("-v")450 result.stdout.fnmatch_lines("""451 *test_function*1-b0*452 *test_function*1.3-b1*453 """)454 @pytest.mark.parametrize(("scope", "length"),455 [("module", 2), ("function", 4)])456 def test_parametrize_scope_overrides(self, testdir, scope, length):457 testdir.makepyfile("""458 import pytest459 l = []460 def pytest_generate_tests(metafunc):461 if "arg" in metafunc.funcargnames:462 metafunc.parametrize("arg", [1,2], indirect=True,463 scope=%r)464 def pytest_funcarg__arg(request):465 l.append(request.param)466 return request.param467 def test_hello(arg):468 assert arg in (1,2)469 def test_world(arg):470 assert arg in (1,2)471 def test_checklength():472 assert len(l) == %d473 """ % (scope, length))474 reprec = testdir.inline_run()475 reprec.assertoutcome(passed=5)476 def test_usefixtures_seen_in_generate_tests(self, testdir):477 testdir.makepyfile("""478 import pytest479 def pytest_generate_tests(metafunc):480 assert "abc" in metafunc.fixturenames481 metafunc.parametrize("abc", [1])482 @pytest.mark.usefixtures("abc")483 def test_function():484 pass485 """)486 reprec = testdir.inline_run()487 reprec.assertoutcome(passed=1)488 def test_generate_tests_only_done_in_subdir(self, testdir):489 sub1 = testdir.mkpydir("sub1")490 sub2 = testdir.mkpydir("sub2")491 sub1.join("conftest.py").write(py.code.Source("""492 def pytest_generate_tests(metafunc):493 assert metafunc.function.__name__ == "test_1"494 """))495 sub2.join("conftest.py").write(py.code.Source("""496 def pytest_generate_tests(metafunc):497 assert metafunc.function.__name__ == "test_2"498 """))499 sub1.join("test_in_sub1.py").write("def test_1(): pass")500 sub2.join("test_in_sub2.py").write("def test_2(): pass")501 result = testdir.runpytest("-v", "-s", sub1, sub2, sub1)502 result.stdout.fnmatch_lines([503 "*3 passed*"...
test_verbose_parametrize.py
Source:test_verbose_parametrize.py
...12 metafunc.definition.get_closest_marker.return_value = p13 return metafunc14def test_generates_ids_from_tuple():15 metafunc = get_metafunc((None, [(1, 2, 3)]))16 pytest_generate_tests(metafunc)17 assert metafunc.function.parametrize.kwargs['ids'] == ['1-2-3']18def test_generates_ids_from_tuple_of_strings():19 metafunc = get_metafunc((None, [("11", "22", "33")]))20 pytest_generate_tests(metafunc)21 assert metafunc.function.parametrize.kwargs['ids'] == ['11-22-33']22def test_truncates_args_tuple():23 metafunc = get_metafunc((None, [tuple(range(100))]))24 pytest_generate_tests(metafunc)25 kwargs = metafunc.function.parametrize.kwargs26 assert len(kwargs['ids'][0]) == 6427 assert kwargs['ids'][0].endswith('...')28def test_generates_ids_single_param():29 metafunc = get_metafunc(("test_param", [1, 2, 3]))30 pytest_generate_tests(metafunc)31 assert metafunc.function.parametrize.kwargs['ids'] == ['1', '2', '3']32def test_generates_ids_single__string_param():33 metafunc = get_metafunc(("test_param", ["111", "222", "333"]))34 pytest_generate_tests(metafunc)35 assert metafunc.function.parametrize.kwargs['ids'] == ['111', '222', '333']36def test_truncates_single_arg():37 metafunc = get_metafunc((None, ["1" * 100]))38 pytest_generate_tests(metafunc)39 kwargs = metafunc.function.parametrize.kwargs40 assert len(kwargs['ids'][0]) == 3241 assert kwargs['ids'][0].endswith('...')42def test_generates_ids_from_duplicates():43 metafunc = get_metafunc((None, [(1, 2, 3), (1, 2, 3)]))44 pytest_generate_tests(metafunc)45 assert metafunc.function.parametrize.kwargs['ids'] == ['1-2-3', '1-2-3#1']46def test_generates_ids_from_apparent_duplicates():47 metafunc = get_metafunc((None, [(1, 2, 3), ('1', '2', '3')]))48 pytest_generate_tests(metafunc)49 assert metafunc.function.parametrize.kwargs['ids'] == ['1-2-3', '1-2-3#1']50def test_ok_on_non_parametrized_function():51 pytest_generate_tests(object())52def test_unicode_parameters():53 metafunc = get_metafunc(("test_param", [u"111", u"¬ËÃââ", u"ð ð ð 𤣠ð ð ð
ð"]))54 pytest_generate_tests(metafunc)...
test_parameterization3.py
Source:test_parameterization3.py
...22from selenium.webdriver.common.keys import Keys23from time import sleep24# @pytest.mark.parametrize("input_browser", ['chrome', 'firefox'])25# @pytest.mark.parametrize("input_url", ['https://www.lambdatest.com', 'https://www.duckduckgo.com'])26def pytest_generate_tests(metafunc):27 if "input_browser" in metafunc.fixturenames:28 metafunc.parametrize("input_browser", ["chrome"]) # ["chrome", "firefox"]29 if "input_url" in metafunc.fixturenames:30 metafunc.parametrize("input_url", ["https://www.lambdatest.com", "https://www.duckduckgo.com"])31def test_url_on_browsers(input_browser, input_url):32 global web_driver33 if input_browser == "chrome":34 web_driver = webdriver.Chrome("D:\My Folder\DS\Python Automation\Drivers\chromedriver_win32\chromedriver.exe")35 # if input_browser == "firefox":36 # web_driver = webdriver.Firefox()37 web_driver.maximize_window()38 web_driver.get(input_url)39 print(web_driver.title)40 sleep(5)...
test_pytest.py
Source:test_pytest.py
...3def test_generates_ids_from_tuple():4 p = Mock(kwargs={}, args=(None, [(1, 2, 3)]))5 metafunc = Mock()6 metafunc.function.parametrize = [p]7 pytest_generate_tests(metafunc)8 assert p.kwargs['ids'] == ['1-2-3']9def test_generates_ids_from_tuple_of_strings():10 p = Mock(kwargs={}, args=(None, [("11", "22", "33")]))11 metafunc = Mock()12 metafunc.function.parametrize = [p]13 pytest_generate_tests(metafunc)14 assert p.kwargs['ids'] == ['11-22-33']15def test_truncates_args_tuple():16 p = Mock(kwargs={}, args=(None, [tuple(range(100))]))17 metafunc = Mock()18 metafunc.function.parametrize = [p]19 pytest_generate_tests(metafunc)20 assert len(p.kwargs['ids'][0]) == 6421 assert p.kwargs['ids'][0].endswith('...')22def test_generates_ids_single_param():23 p = Mock(kwargs={}, args=("test_param", [1, 2, 3]))24 metafunc = Mock()25 metafunc.function.parametrize = [p]26 pytest_generate_tests(metafunc)27 assert p.kwargs['ids'] == ['1', '2', '3']28def test_generates_ids_single__string_param():29 p = Mock(kwargs={}, args=("test_param", ["111", "222", "333"]))30 metafunc = Mock()31 metafunc.function.parametrize = [p]32 pytest_generate_tests(metafunc)33 assert p.kwargs['ids'] == ['111', '222', '333']34def test_truncates_single_arg():35 p = Mock(kwargs={}, args=(None, ["1" * 100]))36 metafunc = Mock()37 metafunc.function.parametrize = [p]38 pytest_generate_tests(metafunc)39 assert len(p.kwargs['ids'][0]) == 3240 assert p.kwargs['ids'][0].endswith('...')41def test_generates_ids_from_duplicates():42 p = Mock(kwargs={}, args=(None, [(1, 2, 3), (1, 2, 3)]))43 metafunc = Mock()44 metafunc.function.parametrize = [p]45 pytest_generate_tests(metafunc)46 assert p.kwargs['ids'] == ['1-2-3', '1-2-3#1']47def test_generates_ids_from_apparent_duplicates():48 p = Mock(kwargs={}, args=(None, [(1, 2, 3), ('1', '2', '3')]))49 metafunc = Mock()50 metafunc.function.parametrize = [p]51 pytest_generate_tests(metafunc)52 assert p.kwargs['ids'] == ['1-2-3', '1-2-3#1']53def test_ok_on_non_parametrized_function():...
Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!