Best Python code snippet using pytest
test_mark.py
Source:test_mark.py
1import py, pytest2from _pytest.mark import MarkGenerator as Mark3class TestMark:4 def test_markinfo_repr(self):5 from _pytest.mark import MarkInfo6 m = MarkInfo("hello", (1,2), {})7 repr(m)8 def test_pytest_exists_in_namespace_all(self):9 assert 'mark' in py.test.__all__10 assert 'mark' in pytest.__all__11 def test_pytest_mark_notcallable(self):12 mark = Mark()13 pytest.raises((AttributeError, TypeError), "mark()")14 def test_pytest_mark_bare(self):15 mark = Mark()16 def f():17 pass18 mark.hello(f)19 assert f.hello20 def test_pytest_mark_keywords(self):21 mark = Mark()22 def f():23 pass24 mark.world(x=3, y=4)(f)25 assert f.world26 assert f.world.kwargs['x'] == 327 assert f.world.kwargs['y'] == 428 def test_apply_multiple_and_merge(self):29 mark = Mark()30 def f():31 pass32 marker = mark.world33 mark.world(x=3)(f)34 assert f.world.kwargs['x'] == 335 mark.world(y=4)(f)36 assert f.world.kwargs['x'] == 337 assert f.world.kwargs['y'] == 438 mark.world(y=1)(f)39 assert f.world.kwargs['y'] == 140 assert len(f.world.args) == 041 def test_pytest_mark_positional(self):42 mark = Mark()43 def f():44 pass45 mark.world("hello")(f)46 assert f.world.args[0] == "hello"47 mark.world("world")(f)48 def test_pytest_mark_reuse(self):49 mark = Mark()50 def f():51 pass52 w = mark.some53 w("hello", reason="123")(f)54 assert f.some.args[0] == "hello"55 assert f.some.kwargs['reason'] == "123"56 def g():57 pass58 w("world", reason2="456")(g)59 assert g.some.args[0] == "world"60 assert 'reason' not in g.some.kwargs61 assert g.some.kwargs['reason2'] == "456"62def test_ini_markers(testdir):63 testdir.makeini("""64 [pytest]65 markers =66 a1: this is a webtest marker67 a2: this is a smoke marker68 """)69 testdir.makepyfile("""70 def test_markers(pytestconfig):71 markers = pytestconfig.getini("markers")72 print (markers)73 assert len(markers) >= 274 assert markers[0].startswith("a1:")75 assert markers[1].startswith("a2:")76 """)77 rec = testdir.inline_run()78 rec.assertoutcome(passed=1)79def test_markers_option(testdir):80 testdir.makeini("""81 [pytest]82 markers =83 a1: this is a webtest marker84 a1some: another marker85 """)86 result = testdir.runpytest("--markers", )87 result.stdout.fnmatch_lines([88 "*a1*this is a webtest*",89 "*a1some*another marker",90 ])91def test_strict_prohibits_unregistered_markers(testdir):92 testdir.makepyfile("""93 import pytest94 @pytest.mark.unregisteredmark95 def test_hello():96 pass97 """)98 result = testdir.runpytest("--strict")99 assert result.ret != 0100 result.stdout.fnmatch_lines([101 "*unregisteredmark*not*registered*",102 ])103@pytest.mark.multi(spec=[104 ("xyz", ("test_one",)),105 ("xyz and xyz2", ()),106 ("xyz2", ("test_two",)),107 ("xyz or xyz2", ("test_one", "test_two"),)108])109def test_mark_option(spec, testdir):110 testdir.makepyfile("""111 import pytest112 @pytest.mark.xyz113 def test_one():114 pass115 @pytest.mark.xyz2116 def test_two():117 pass118 """)119 opt, passed_result = spec120 rec = testdir.inline_run("-m", opt)121 passed, skipped, fail = rec.listoutcomes()122 passed = [x.nodeid.split("::")[-1] for x in passed]123 assert len(passed) == len(passed_result)124 assert list(passed) == list(passed_result)125@pytest.mark.multi(spec=[126 ("interface", ("test_interface",)),127 ("not interface", ("test_nointer",)),128])129def test_mark_option_custom(spec, testdir):130 testdir.makeconftest("""131 import pytest132 def pytest_collection_modifyitems(items):133 for item in items:134 if "interface" in item.nodeid:135 item.keywords["interface"] = pytest.mark.interface136 """)137 testdir.makepyfile("""138 def test_interface():139 pass140 def test_nointer():141 pass142 """)143 opt, passed_result = spec144 rec = testdir.inline_run("-m", opt)145 passed, skipped, fail = rec.listoutcomes()146 passed = [x.nodeid.split("::")[-1] for x in passed]147 assert len(passed) == len(passed_result)148 assert list(passed) == list(passed_result)149@pytest.mark.multi(spec=[150 ("interface", ("test_interface",)),151 ("not interface", ("test_nointer",)),152])153def test_keyword_option_custom(spec, testdir):154 testdir.makepyfile("""155 def test_interface():156 pass157 def test_nointer():158 pass159 """)160 opt, passed_result = spec161 rec = testdir.inline_run("-k", opt)162 passed, skipped, fail = rec.listoutcomes()163 passed = [x.nodeid.split("::")[-1] for x in passed]164 assert len(passed) == len(passed_result)165 assert list(passed) == list(passed_result)166class TestFunctional:167 def test_mark_per_function(self, testdir):168 p = testdir.makepyfile("""169 import pytest170 @pytest.mark.hello171 def test_hello():172 assert hasattr(test_hello, 'hello')173 """)174 result = testdir.runpytest(p)175 result.stdout.fnmatch_lines(["*1 passed*"])176 def test_mark_per_module(self, testdir):177 item = testdir.getitem("""178 import pytest179 pytestmark = pytest.mark.hello180 def test_func():181 pass182 """)183 keywords = item.keywords184 assert 'hello' in keywords185 def test_marklist_per_class(self, testdir):186 item = testdir.getitem("""187 import pytest188 class TestClass:189 pytestmark = [pytest.mark.hello, pytest.mark.world]190 def test_func(self):191 assert TestClass.test_func.hello192 assert TestClass.test_func.world193 """)194 keywords = item.keywords195 assert 'hello' in keywords196 def test_marklist_per_module(self, testdir):197 item = testdir.getitem("""198 import pytest199 pytestmark = [pytest.mark.hello, pytest.mark.world]200 class TestClass:201 def test_func(self):202 assert TestClass.test_func.hello203 assert TestClass.test_func.world204 """)205 keywords = item.keywords206 assert 'hello' in keywords207 assert 'world' in keywords208 @pytest.mark.skipif("sys.version_info < (2,6)")209 def test_mark_per_class_decorator(self, testdir):210 item = testdir.getitem("""211 import pytest212 @pytest.mark.hello213 class TestClass:214 def test_func(self):215 assert TestClass.test_func.hello216 """)217 keywords = item.keywords218 assert 'hello' in keywords219 @pytest.mark.skipif("sys.version_info < (2,6)")220 def test_mark_per_class_decorator_plus_existing_dec(self, testdir):221 item = testdir.getitem("""222 import pytest223 @pytest.mark.hello224 class TestClass:225 pytestmark = pytest.mark.world226 def test_func(self):227 assert TestClass.test_func.hello228 assert TestClass.test_func.world229 """)230 keywords = item.keywords231 assert 'hello' in keywords232 assert 'world' in keywords233 def test_merging_markers(self, testdir):234 p = testdir.makepyfile("""235 import pytest236 pytestmark = pytest.mark.hello("pos1", x=1, y=2)237 class TestClass:238 # classlevel overrides module level239 pytestmark = pytest.mark.hello(x=3)240 @pytest.mark.hello("pos0", z=4)241 def test_func(self):242 pass243 """)244 items, rec = testdir.inline_genitems(p)245 item, = items246 keywords = item.keywords247 marker = keywords['hello']248 assert marker.args == ("pos0", "pos1")249 assert marker.kwargs == {'x': 1, 'y': 2, 'z': 4}250 # test the new __iter__ interface251 l = list(marker)252 assert len(l) == 3253 assert l[0].args == ("pos0",)254 assert l[1].args == ()255 assert l[2].args == ("pos1", )256 @pytest.mark.xfail(reason='unfixed')257 def test_merging_markers_deep(self, testdir):258 # issue 199 - propagate markers into nested classes259 p = testdir.makepyfile("""260 import pytest261 class TestA:262 pytestmark = pytest.mark.a263 def test_b(self):264 assert True265 class TestC:266 # this one didnt get marked267 def test_d(self):268 assert True269 """)270 items, rec = testdir.inline_genitems(p)271 for item in items:272 print (item, item.keywords)273 assert 'a' in item.keywords274 def test_mark_with_wrong_marker(self, testdir):275 reprec = testdir.inline_runsource("""276 import pytest277 class pytestmark:278 pass279 def test_func():280 pass281 """)282 l = reprec.getfailedcollections()283 assert len(l) == 1284 assert "TypeError" in str(l[0].longrepr)285 def test_mark_dynamically_in_funcarg(self, testdir):286 testdir.makeconftest("""287 import pytest288 def pytest_funcarg__arg(request):289 request.applymarker(pytest.mark.hello)290 def pytest_terminal_summary(terminalreporter):291 l = terminalreporter.stats['passed']292 terminalreporter.writer.line("keyword: %s" % l[0].keywords)293 """)294 testdir.makepyfile("""295 def test_func(arg):296 pass297 """)298 result = testdir.runpytest()299 result.stdout.fnmatch_lines([300 "keyword: *hello*"301 ])302 def test_merging_markers_two_functions(self, testdir):303 p = testdir.makepyfile("""304 import pytest305 @pytest.mark.hello("pos1", z=4)306 @pytest.mark.hello("pos0", z=3)307 def test_func():308 pass309 """)310 items, rec = testdir.inline_genitems(p)311 item, = items312 keywords = item.keywords313 marker = keywords['hello']314 l = list(marker)315 assert len(l) == 2316 assert l[0].args == ("pos0",)317 assert l[1].args == ("pos1",)318 def test_no_marker_match_on_unmarked_names(self, testdir):319 p = testdir.makepyfile("""320 import pytest321 @pytest.mark.shouldmatch322 def test_marked():323 assert 1324 def test_unmarked():325 assert 1326 """)327 reprec = testdir.inline_run("-m", "test_unmarked", p)328 passed, skipped, failed = reprec.listoutcomes()329 assert len(passed) + len(skipped) + len(failed) == 0330 dlist = reprec.getcalls("pytest_deselected")331 deselected_tests = dlist[0].items332 assert len(deselected_tests) == 2333 def test_keywords_at_node_level(self, testdir):334 p = testdir.makepyfile("""335 import pytest336 @pytest.fixture(scope="session", autouse=True)337 def some(request):338 request.keywords["hello"] = 42339 assert "world" not in request.keywords340 @pytest.fixture(scope="function", autouse=True)341 def funcsetup(request):342 assert "world" in request.keywords343 assert "hello" in request.keywords344 @pytest.mark.world345 def test_function():346 pass347 """)348 reprec = testdir.inline_run()349 reprec.assertoutcome(passed=1)350 def test_keyword_added_for_session(self, testdir):351 testdir.makeconftest("""352 import pytest353 def pytest_collection_modifyitems(session):354 session.add_marker("mark1")355 session.add_marker(pytest.mark.mark2)356 session.add_marker(pytest.mark.mark3)357 pytest.raises(ValueError, lambda:358 session.add_marker(10))359 """)360 testdir.makepyfile("""361 def test_some(request):362 assert "mark1" in request.keywords363 assert "mark2" in request.keywords364 assert "mark3" in request.keywords365 assert 10 not in request.keywords366 marker = request.node.get_marker("mark1")367 assert marker.name == "mark1"368 assert marker.args == ()369 assert marker.kwargs == {}370 """)371 reprec = testdir.inline_run("-m", "mark1")372 reprec.assertoutcome(passed=1)373class TestKeywordSelection:374 def test_select_simple(self, testdir):375 file_test = testdir.makepyfile("""376 def test_one():377 assert 0378 class TestClass(object):379 def test_method_one(self):380 assert 42 == 43381 """)382 def check(keyword, name):383 reprec = testdir.inline_run("-s", "-k", keyword, file_test)384 passed, skipped, failed = reprec.listoutcomes()385 assert len(failed) == 1386 assert failed[0].nodeid.split("::")[-1] == name387 assert len(reprec.getcalls('pytest_deselected')) == 1388 for keyword in ['test_one', 'est_on']:389 check(keyword, 'test_one')390 check('TestClass and test', 'test_method_one')391 @pytest.mark.parametrize("keyword", [392 'xxx', 'xxx and test_2', 'TestClass', 'xxx and -test_1',393 'TestClass and test_2', 'xxx and TestClass and test_2'])394 def test_select_extra_keywords(self, testdir, keyword):395 p = testdir.makepyfile(test_select="""396 def test_1():397 pass398 class TestClass:399 def test_2(self):400 pass401 """)402 testdir.makepyfile(conftest="""403 def pytest_pycollect_makeitem(__multicall__, name):404 if name == "TestClass":405 item = __multicall__.execute()406 item.extra_keyword_matches.add("xxx")407 return item408 """)409 reprec = testdir.inline_run(p.dirpath(), '-s', '-k', keyword)410 py.builtin.print_("keyword", repr(keyword))411 passed, skipped, failed = reprec.listoutcomes()412 assert len(passed) == 1413 assert passed[0].nodeid.endswith("test_2")414 dlist = reprec.getcalls("pytest_deselected")415 assert len(dlist) == 1416 assert dlist[0].items[0].name == 'test_1'417 def test_select_starton(self, testdir):418 threepass = testdir.makepyfile(test_threepass="""419 def test_one(): assert 1420 def test_two(): assert 1421 def test_three(): assert 1422 """)423 reprec = testdir.inline_run("-k", "test_two:", threepass)424 passed, skipped, failed = reprec.listoutcomes()425 assert len(passed) == 2426 assert not failed427 dlist = reprec.getcalls("pytest_deselected")428 assert len(dlist) == 1429 item = dlist[0].items[0]430 assert item.name == "test_one"431 def test_keyword_extra(self, testdir):432 p = testdir.makepyfile("""433 def test_one():434 assert 0435 test_one.mykeyword = True436 """)437 reprec = testdir.inline_run("-k", "mykeyword", p)438 passed, skipped, failed = reprec.countoutcomes()439 assert failed == 1440 @pytest.mark.xfail441 def test_keyword_extra_dash(self, testdir):442 p = testdir.makepyfile("""443 def test_one():444 assert 0445 test_one.mykeyword = True446 """)447 # with argparse the argument to an option cannot448 # start with '-'449 reprec = testdir.inline_run("-k", "-mykeyword", p)450 passed, skipped, failed = reprec.countoutcomes()451 assert passed + skipped + failed == 0452 def test_no_magic_values(self, testdir):453 """Make sure the tests do not match on magic values,454 no double underscored values, like '__dict__',455 and no instance values, like '()'.456 """457 p = testdir.makepyfile("""458 def test_one(): assert 1459 """)460 def assert_test_is_not_selected(keyword):461 reprec = testdir.inline_run("-k", keyword, p)462 passed, skipped, failed = reprec.countoutcomes()463 dlist = reprec.getcalls("pytest_deselected")464 assert passed + skipped + failed == 0465 deselected_tests = dlist[0].items466 assert len(deselected_tests) == 1467 assert_test_is_not_selected("__")...
__init__.py
Source:__init__.py
...95 if selectuntil:96 keywordexpr = None97 remaining.append(colitem)98 if deselected:99 config.hook.pytest_deselected(items=deselected)100 items[:] = remaining101def deselect_by_mark(items, config):102 matchexpr = config.option.markexpr103 if not matchexpr:104 return105 remaining = []106 deselected = []107 for item in items:108 if matchmark(item, matchexpr):109 remaining.append(item)110 else:111 deselected.append(item)112 if deselected:113 config.hook.pytest_deselected(items=deselected)114 items[:] = remaining115def pytest_collection_modifyitems(items, config):116 deselect_by_keyword(items, config)117 deselect_by_mark(items, config)118def pytest_configure(config):119 config._old_mark_config = MARK_GEN._config120 MARK_GEN._config = config121 empty_parameterset = config.getini(EMPTY_PARAMETERSET_OPTION)122 if empty_parameterset not in ("skip", "xfail", "fail_at_collect", None, ""):123 raise UsageError(124 "{!s} must be one of skip, xfail or fail_at_collect"125 " but it is {!r}".format(EMPTY_PARAMETERSET_OPTION, empty_parameterset)126 )127def pytest_unconfigure(config):...
test_genitems.py
Source:test_genitems.py
1import py2class Test_genitems: 3 def test_check_collect_hashes(self, testdir):4 p = testdir.makepyfile("""5 def test_1():6 pass7 def test_2():8 pass9 """)10 p.copy(p.dirpath(p.purebasename + "2" + ".py"))11 items, reprec = testdir.inline_genitems(p.dirpath())12 assert len(items) == 413 for numi, i in enumerate(items):14 for numj, j in enumerate(items):15 if numj != numi:16 assert hash(i) != hash(j)17 assert i != j18 19 def test_root_conftest_syntax_error(self, testdir):20 # do we want to unify behaviour with21 # test_subdir_conftest_error? 22 p = testdir.makepyfile(conftest="raise SyntaxError\n")23 py.test.raises(SyntaxError, testdir.inline_genitems, p.dirpath())24 25 def test_subdir_conftest_error(self, testdir):26 tmp = testdir.tmpdir27 tmp.ensure("sub", "conftest.py").write("raise SyntaxError\n")28 items, reprec = testdir.inline_genitems(tmp)29 collectionfailures = reprec.getfailedcollections()30 assert len(collectionfailures) == 131 ev = collectionfailures[0] 32 assert ev.longrepr.reprcrash.message.startswith("SyntaxError")33 def test_example_items1(self, testdir):34 p = testdir.makepyfile('''35 def testone():36 pass37 class TestX:38 def testmethod_one(self):39 pass40 class TestY(TestX):41 pass42 ''')43 items, reprec = testdir.inline_genitems(p)44 assert len(items) == 345 assert items[0].name == 'testone'46 assert items[1].name == 'testmethod_one'47 assert items[2].name == 'testmethod_one'48 # let's also test getmodpath here49 assert items[0].getmodpath() == "testone"50 assert items[1].getmodpath() == "TestX.testmethod_one"51 assert items[2].getmodpath() == "TestY.testmethod_one"52 s = items[0].getmodpath(stopatmodule=False)53 assert s.endswith("test_example_items1.testone")54 print s55class TestKeywordSelection:56 def test_select_simple(self, testdir):57 file_test = testdir.makepyfile("""58 def test_one(): assert 059 class TestClass(object): 60 def test_method_one(self): 61 assert 42 == 43 62 """)63 def check(keyword, name):64 reprec = testdir.inline_run("-s", "-k", keyword, file_test)65 passed, skipped, failed = reprec.listoutcomes()66 assert len(failed) == 167 assert failed[0].item.name == name 68 assert len(reprec.getcalls('pytest_deselected')) == 169 for keyword in ['test_one', 'est_on']:70 #yield check, keyword, 'test_one'71 check(keyword, 'test_one')72 check('TestClass.test', 'test_method_one')73 def test_select_extra_keywords(self, testdir):74 p = testdir.makepyfile(test_select="""75 def test_1():76 pass 77 class TestClass: 78 def test_2(self): 79 pass80 """)81 testdir.makepyfile(conftest="""82 import py83 class Class(py.test.collect.Class): 84 def _keywords(self):85 return ['xxx', self.name]86 """)87 for keyword in ('xxx', 'xxx test_2', 'TestClass', 'xxx -test_1', 88 'TestClass test_2', 'xxx TestClass test_2',): 89 reprec = testdir.inline_run(p.dirpath(), '-s', '-k', keyword)90 print "keyword", repr(keyword)91 passed, skipped, failed = reprec.listoutcomes()92 assert len(passed) == 193 assert passed[0].item.name == "test_2"94 dlist = reprec.getcalls("pytest_deselected")95 assert len(dlist) == 196 assert dlist[0].items[0].name == 'test_1'97 def test_select_starton(self, testdir):98 threepass = testdir.makepyfile(test_threepass="""99 def test_one(): assert 1100 def test_two(): assert 1101 def test_three(): assert 1102 """)103 reprec = testdir.inline_run("-k", "test_two:", threepass)104 passed, skipped, failed = reprec.listoutcomes()105 assert len(passed) == 2106 assert not failed 107 dlist = reprec.getcalls("pytest_deselected")108 assert len(dlist) == 1109 item = dlist[0].items[0]110 assert item.name == "test_one" 111 def test_keyword_extra(self, testdir):112 p = testdir.makepyfile("""113 def test_one(): 114 assert 0 115 test_one.mykeyword = True116 """)117 reprec = testdir.inline_run("-k", "-mykeyword", p)118 passed, skipped, failed = reprec.countoutcomes()119 assert passed + skipped + failed == 0 120 reprec = testdir.inline_run("-k", "mykeyword", p)121 passed, skipped, failed = reprec.countoutcomes()...
cacheprovider.py
Source:cacheprovider.py
...45 else:46 previously_passed.append(item)47 if previously_failed:48 items[:] = previously_failed49 config.hook.pytest_deselected(items=previously_passed)50 else:51 config.hook.pytest_deselected(items=items)52 items[:] = []53 def pytest_sessionfinish(self, session):54 config = self.config55 if config.getvalue("cacheshow") or hasattr(config, "slaveinput"):56 return57 prev_failed = config.cache.get(58 "cache/{}".format(config.lastfailed_file), None59 ) is not None60 if (session.testscollected and prev_failed) or self.lastfailed:61 config.cache.set(62 "cache/{}".format(config.lastfailed_file), self.lastfailed...
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!!