How to use pytest_collectstart method in Pytest

Best Python code snippet using pytest

test_collection.py

Source:test_collection.py Github

copy

Full Screen

1import pytest, py2from _pytest.main import Session3class TestCollector:4 def test_collect_versus_item(self):5 from pytest import Collector, Item6 assert not issubclass(Collector, Item)7 assert not issubclass(Item, Collector)8 def test_compat_attributes(self, testdir, recwarn):9 modcol = testdir.getmodulecol("""10 def test_pass(): pass11 def test_fail(): assert 012 """)13 recwarn.clear()14 assert modcol.Module == pytest.Module15 assert modcol.Class == pytest.Class16 assert modcol.Item == pytest.Item17 assert modcol.File == pytest.File18 assert modcol.Function == pytest.Function19 def test_check_equality(self, testdir):20 modcol = testdir.getmodulecol("""21 def test_pass(): pass22 def test_fail(): assert 023 """)24 fn1 = testdir.collect_by_name(modcol, "test_pass")25 assert isinstance(fn1, pytest.Function)26 fn2 = testdir.collect_by_name(modcol, "test_pass")27 assert isinstance(fn2, pytest.Function)28 assert fn1 == fn229 assert fn1 != modcol30 if py.std.sys.version_info < (3, 0):31 assert cmp(fn1, fn2) == 032 assert hash(fn1) == hash(fn2)33 fn3 = testdir.collect_by_name(modcol, "test_fail")34 assert isinstance(fn3, pytest.Function)35 assert not (fn1 == fn3)36 assert fn1 != fn337 for fn in fn1,fn2,fn3:38 assert fn != 339 assert fn != modcol40 assert fn != [1,2,3]41 assert [1,2,3] != fn42 assert modcol != fn43 def test_getparent(self, testdir):44 modcol = testdir.getmodulecol("""45 class TestClass:46 def test_foo():47 pass48 """)49 cls = testdir.collect_by_name(modcol, "TestClass")50 fn = testdir.collect_by_name(51 testdir.collect_by_name(cls, "()"), "test_foo")52 parent = fn.getparent(pytest.Module)53 assert parent is modcol54 parent = fn.getparent(pytest.Function)55 assert parent is fn56 parent = fn.getparent(pytest.Class)57 assert parent is cls58 def test_getcustomfile_roundtrip(self, testdir):59 hello = testdir.makefile(".xxx", hello="world")60 testdir.makepyfile(conftest="""61 import pytest62 class CustomFile(pytest.File):63 pass64 def pytest_collect_file(path, parent):65 if path.ext == ".xxx":66 return CustomFile(path, parent=parent)67 """)68 node = testdir.getpathnode(hello)69 assert isinstance(node, pytest.File)70 assert node.name == "hello.xxx"71 nodes = node.session.perform_collect([node.nodeid], genitems=False)72 assert len(nodes) == 173 assert isinstance(nodes[0], pytest.File)74class TestCollectFS:75 def test_ignored_certain_directories(self, testdir):76 tmpdir = testdir.tmpdir77 tmpdir.ensure("_darcs", 'test_notfound.py')78 tmpdir.ensure("CVS", 'test_notfound.py')79 tmpdir.ensure("{arch}", 'test_notfound.py')80 tmpdir.ensure(".whatever", 'test_notfound.py')81 tmpdir.ensure(".bzr", 'test_notfound.py')82 tmpdir.ensure("normal", 'test_found.py')83 for x in tmpdir.visit("test_*.py"):84 x.write("def test_hello(): pass")85 result = testdir.runpytest("--collect-only")86 s = result.stdout.str()87 assert "test_notfound" not in s88 assert "test_found" in s89 def test_custom_norecursedirs(self, testdir):90 testdir.makeini("""91 [pytest]92 norecursedirs = mydir xyz*93 """)94 tmpdir = testdir.tmpdir95 tmpdir.ensure("mydir", "test_hello.py").write("def test_1(): pass")96 tmpdir.ensure("xyz123", "test_2.py").write("def test_2(): 0/0")97 tmpdir.ensure("xy", "test_ok.py").write("def test_3(): pass")98 rec = testdir.inline_run()99 rec.assertoutcome(passed=1)100 rec = testdir.inline_run("xyz123/test_2.py")101 rec.assertoutcome(failed=1)102class TestCollectPluginHookRelay:103 def test_pytest_collect_file(self, testdir):104 wascalled = []105 class Plugin:106 def pytest_collect_file(self, path, parent):107 wascalled.append(path)108 testdir.makefile(".abc", "xyz")109 pytest.main([testdir.tmpdir], plugins=[Plugin()])110 assert len(wascalled) == 1111 assert wascalled[0].ext == '.abc'112 def test_pytest_collect_directory(self, testdir):113 wascalled = []114 class Plugin:115 def pytest_collect_directory(self, path, parent):116 wascalled.append(path.basename)117 testdir.mkdir("hello")118 testdir.mkdir("world")119 pytest.main(testdir.tmpdir, plugins=[Plugin()])120 assert "hello" in wascalled121 assert "world" in wascalled122class TestPrunetraceback:123 def test_collection_error(self, testdir):124 p = testdir.makepyfile("""125 import not_exists126 """)127 result = testdir.runpytest(p)128 assert "__import__" not in result.stdout.str(), "too long traceback"129 result.stdout.fnmatch_lines([130 "*ERROR collecting*",131 "*mport*not_exists*"132 ])133 def test_custom_repr_failure(self, testdir):134 p = testdir.makepyfile("""135 import not_exists136 """)137 testdir.makeconftest("""138 import pytest139 def pytest_collect_file(path, parent):140 return MyFile(path, parent)141 class MyError(Exception):142 pass143 class MyFile(pytest.File):144 def collect(self):145 raise MyError()146 def repr_failure(self, excinfo):147 if excinfo.errisinstance(MyError):148 return "hello world"149 return pytest.File.repr_failure(self, excinfo)150 """)151 result = testdir.runpytest(p)152 result.stdout.fnmatch_lines([153 "*ERROR collecting*",154 "*hello world*",155 ])156 @pytest.mark.xfail(reason="other mechanism for adding to reporting needed")157 def test_collect_report_postprocessing(self, testdir):158 p = testdir.makepyfile("""159 import not_exists160 """)161 testdir.makeconftest("""162 import pytest163 def pytest_make_collect_report(__multicall__):164 rep = __multicall__.execute()165 rep.headerlines += ["header1"]166 return rep167 """)168 result = testdir.runpytest(p)169 result.stdout.fnmatch_lines([170 "*ERROR collecting*",171 "*header1*",172 ])173class TestCustomConftests:174 def test_ignore_collect_path(self, testdir):175 testdir.makeconftest("""176 def pytest_ignore_collect(path, config):177 return path.basename.startswith("x") or \178 path.basename == "test_one.py"179 """)180 sub = testdir.mkdir("xy123")181 sub.ensure("test_hello.py").write("syntax error")182 sub.join("conftest.py").write("syntax error")183 testdir.makepyfile("def test_hello(): pass")184 testdir.makepyfile(test_one="syntax error")185 result = testdir.runpytest("--fulltrace")186 assert result.ret == 0187 result.stdout.fnmatch_lines(["*1 passed*"])188 def test_ignore_collect_not_called_on_argument(self, testdir):189 testdir.makeconftest("""190 def pytest_ignore_collect(path, config):191 return True192 """)193 p = testdir.makepyfile("def test_hello(): pass")194 result = testdir.runpytest(p)195 assert result.ret == 0196 assert "1 passed" in result.stdout.str()197 result = testdir.runpytest()198 assert result.ret == 0199 assert "1 passed" not in result.stdout.str()200 def test_collectignore_exclude_on_option(self, testdir):201 testdir.makeconftest("""202 collect_ignore = ['hello', 'test_world.py']203 def pytest_addoption(parser):204 parser.addoption("--XX", action="store_true", default=False)205 def pytest_configure(config):206 if config.getvalue("XX"):207 collect_ignore[:] = []208 """)209 testdir.mkdir("hello")210 testdir.makepyfile(test_world="def test_hello(): pass")211 result = testdir.runpytest()212 assert result.ret == 0213 assert "passed" not in result.stdout.str()214 result = testdir.runpytest("--XX")215 assert result.ret == 0216 assert "passed" in result.stdout.str()217 def test_pytest_fs_collect_hooks_are_seen(self, testdir):218 testdir.makeconftest("""219 import pytest220 class MyModule(pytest.Module):221 pass222 def pytest_collect_file(path, parent):223 if path.ext == ".py":224 return MyModule(path, parent)225 """)226 testdir.mkdir("sub")227 testdir.makepyfile("def test_x(): pass")228 result = testdir.runpytest("--collect-only")229 result.stdout.fnmatch_lines([230 "*MyModule*",231 "*test_x*"232 ])233 def test_pytest_collect_file_from_sister_dir(self, testdir):234 sub1 = testdir.mkpydir("sub1")235 sub2 = testdir.mkpydir("sub2")236 conf1 = testdir.makeconftest("""237 import pytest238 class MyModule1(pytest.Module):239 pass240 def pytest_collect_file(path, parent):241 if path.ext == ".py":242 return MyModule1(path, parent)243 """)244 conf1.move(sub1.join(conf1.basename))245 conf2 = testdir.makeconftest("""246 import pytest247 class MyModule2(pytest.Module):248 pass249 def pytest_collect_file(path, parent):250 if path.ext == ".py":251 return MyModule2(path, parent)252 """)253 conf2.move(sub2.join(conf2.basename))254 p = testdir.makepyfile("def test_x(): pass")255 p.copy(sub1.join(p.basename))256 p.copy(sub2.join(p.basename))257 result = testdir.runpytest("--collect-only")258 result.stdout.fnmatch_lines([259 "*MyModule1*",260 "*MyModule2*",261 "*test_x*"262 ])263class TestSession:264 def test_parsearg(self, testdir):265 p = testdir.makepyfile("def test_func(): pass")266 subdir = testdir.mkdir("sub")267 subdir.ensure("__init__.py")268 target = subdir.join(p.basename)269 p.move(target)270 testdir.chdir()271 subdir.chdir()272 config = testdir.parseconfig(p.basename)273 rcol = Session(config=config)274 assert rcol.fspath == subdir275 parts = rcol._parsearg(p.basename)276 assert parts[0] == target277 assert len(parts) == 1278 parts = rcol._parsearg(p.basename + "::test_func")279 assert parts[0] == target280 assert parts[1] == "test_func"281 assert len(parts) == 2282 def test_collect_topdir(self, testdir):283 p = testdir.makepyfile("def test_func(): pass")284 id = "::".join([p.basename, "test_func"])285 # XXX migrate to inline_genitems? (see below)286 config = testdir.parseconfig(id)287 topdir = testdir.tmpdir288 rcol = Session(config)289 assert topdir == rcol.fspath290 #rootid = rcol.nodeid291 #root2 = rcol.perform_collect([rcol.nodeid], genitems=False)[0]292 #assert root2 == rcol, rootid293 colitems = rcol.perform_collect([rcol.nodeid], genitems=False)294 assert len(colitems) == 1295 assert colitems[0].fspath == p296 def test_collect_protocol_single_function(self, testdir):297 p = testdir.makepyfile("def test_func(): pass")298 id = "::".join([p.basename, "test_func"])299 items, hookrec = testdir.inline_genitems(id)300 item, = items301 assert item.name == "test_func"302 newid = item.nodeid303 assert newid == id304 py.std.pprint.pprint(hookrec.calls)305 topdir = testdir.tmpdir # noqa306 hookrec.assert_contains([307 ("pytest_collectstart", "collector.fspath == topdir"),308 ("pytest_make_collect_report", "collector.fspath == topdir"),309 ("pytest_collectstart", "collector.fspath == p"),310 ("pytest_make_collect_report", "collector.fspath == p"),311 ("pytest_pycollect_makeitem", "name == 'test_func'"),312 ("pytest_collectreport", "report.nodeid.startswith(p.basename)"),313 ("pytest_collectreport", "report.nodeid == ''")314 ])315 def test_collect_protocol_method(self, testdir):316 p = testdir.makepyfile("""317 class TestClass:318 def test_method(self):319 pass320 """)321 normid = p.basename + "::TestClass::()::test_method"322 for id in [p.basename,323 p.basename + "::TestClass",324 p.basename + "::TestClass::()",325 normid,326 ]:327 items, hookrec = testdir.inline_genitems(id)328 assert len(items) == 1329 assert items[0].name == "test_method"330 newid = items[0].nodeid331 assert newid == normid332 def test_collect_custom_nodes_multi_id(self, testdir):333 p = testdir.makepyfile("def test_func(): pass")334 testdir.makeconftest("""335 import pytest336 class SpecialItem(pytest.Item):337 def runtest(self):338 return # ok339 class SpecialFile(pytest.File):340 def collect(self):341 return [SpecialItem(name="check", parent=self)]342 def pytest_collect_file(path, parent):343 if path.basename == %r:344 return SpecialFile(fspath=path, parent=parent)345 """ % p.basename)346 id = p.basename347 items, hookrec = testdir.inline_genitems(id)348 py.std.pprint.pprint(hookrec.calls)349 assert len(items) == 2350 hookrec.assert_contains([351 ("pytest_collectstart",352 "collector.fspath == collector.session.fspath"),353 ("pytest_collectstart",354 "collector.__class__.__name__ == 'SpecialFile'"),355 ("pytest_collectstart",356 "collector.__class__.__name__ == 'Module'"),357 ("pytest_pycollect_makeitem", "name == 'test_func'"),358 ("pytest_collectreport", "report.nodeid.startswith(p.basename)"),359 #("pytest_collectreport",360 # "report.fspath == %r" % str(rcol.fspath)),361 ])362 def test_collect_subdir_event_ordering(self, testdir):363 p = testdir.makepyfile("def test_func(): pass")364 aaa = testdir.mkpydir("aaa")365 test_aaa = aaa.join("test_aaa.py")366 p.move(test_aaa)367 items, hookrec = testdir.inline_genitems()368 assert len(items) == 1369 py.std.pprint.pprint(hookrec.calls)370 hookrec.assert_contains([371 ("pytest_collectstart", "collector.fspath == test_aaa"),372 ("pytest_pycollect_makeitem", "name == 'test_func'"),373 ("pytest_collectreport",374 "report.nodeid.startswith('aaa/test_aaa.py')"),375 ])376 def test_collect_two_commandline_args(self, testdir):377 p = testdir.makepyfile("def test_func(): pass")378 aaa = testdir.mkpydir("aaa")379 bbb = testdir.mkpydir("bbb")380 test_aaa = aaa.join("test_aaa.py")381 p.copy(test_aaa)382 test_bbb = bbb.join("test_bbb.py")383 p.move(test_bbb)384 id = "."385 items, hookrec = testdir.inline_genitems(id)386 assert len(items) == 2387 py.std.pprint.pprint(hookrec.calls)388 hookrec.assert_contains([389 ("pytest_collectstart", "collector.fspath == test_aaa"),390 ("pytest_pycollect_makeitem", "name == 'test_func'"),391 ("pytest_collectreport", "report.nodeid == 'aaa/test_aaa.py'"),392 ("pytest_collectstart", "collector.fspath == test_bbb"),393 ("pytest_pycollect_makeitem", "name == 'test_func'"),394 ("pytest_collectreport", "report.nodeid == 'bbb/test_bbb.py'"),395 ])396 def test_serialization_byid(self, testdir):397 testdir.makepyfile("def test_func(): pass")398 items, hookrec = testdir.inline_genitems()399 assert len(items) == 1400 item, = items401 items2, hookrec = testdir.inline_genitems(item.nodeid)402 item2, = items2403 assert item2.name == item.name404 assert item2.fspath == item.fspath405 def test_find_byid_without_instance_parents(self, testdir):406 p = testdir.makepyfile("""407 class TestClass:408 def test_method(self):409 pass410 """)411 arg = p.basename + ("::TestClass::test_method")412 items, hookrec = testdir.inline_genitems(arg)413 assert len(items) == 1414 item, = items415 assert item.nodeid.endswith("TestClass::()::test_method")416class Test_getinitialnodes:417 def test_global_file(self, testdir, tmpdir):418 x = tmpdir.ensure("x.py")419 config = testdir.parseconfigure(x)420 col = testdir.getnode(config, x)421 assert isinstance(col, pytest.Module)422 assert col.name == 'x.py'423 assert col.parent.name == testdir.tmpdir.basename424 assert col.parent.parent is None425 for col in col.listchain():426 assert col.config is config427 def test_pkgfile(self, testdir):428 testdir.chdir()429 tmpdir = testdir.tmpdir430 subdir = tmpdir.join("subdir")431 x = subdir.ensure("x.py")432 subdir.ensure("__init__.py")433 config = testdir.parseconfigure(x)434 col = testdir.getnode(config, x)435 assert isinstance(col, pytest.Module)436 assert col.name == 'x.py'437 assert col.parent.parent is None438 for col in col.listchain():439 assert col.config is config440class Test_genitems:441 def test_check_collect_hashes(self, testdir):442 p = testdir.makepyfile("""443 def test_1():444 pass445 def test_2():446 pass447 """)448 p.copy(p.dirpath(p.purebasename + "2" + ".py"))449 items, reprec = testdir.inline_genitems(p.dirpath())450 assert len(items) == 4451 for numi, i in enumerate(items):452 for numj, j in enumerate(items):453 if numj != numi:454 assert hash(i) != hash(j)455 assert i != j456 def test_example_items1(self, testdir):457 p = testdir.makepyfile('''458 def testone():459 pass460 class TestX:461 def testmethod_one(self):462 pass463 class TestY(TestX):464 pass465 ''')466 items, reprec = testdir.inline_genitems(p)467 assert len(items) == 3468 assert items[0].name == 'testone'469 assert items[1].name == 'testmethod_one'470 assert items[2].name == 'testmethod_one'471 # let's also test getmodpath here472 assert items[0].getmodpath() == "testone"473 assert items[1].getmodpath() == "TestX.testmethod_one"474 assert items[2].getmodpath() == "TestY.testmethod_one"475 s = items[0].getmodpath(stopatmodule=False)476 assert s.endswith("test_example_items1.testone")477 print(s)478 def test_class_and_functions_discovery_using_glob(self, testdir):479 """480 tests that python_classes and python_functions config options work481 as prefixes and glob-like patterns (issue #600).482 """483 testdir.makeini("""484 [pytest]485 python_classes = *Suite Test486 python_functions = *_test test487 """)488 p = testdir.makepyfile('''489 class MyTestSuite:490 def x_test(self):491 pass492 class TestCase:493 def test_y(self):494 pass495 ''')496 items, reprec = testdir.inline_genitems(p)497 ids = [x.getmodpath() for x in items]498 assert ids == ['MyTestSuite.x_test', 'TestCase.test_y']499def test_matchnodes_two_collections_same_file(testdir):500 testdir.makeconftest("""501 import pytest502 def pytest_configure(config):503 config.pluginmanager.register(Plugin2())504 class Plugin2:505 def pytest_collect_file(self, path, parent):506 if path.ext == ".abc":507 return MyFile2(path, parent)508 def pytest_collect_file(path, parent):509 if path.ext == ".abc":510 return MyFile1(path, parent)511 class MyFile1(pytest.Item, pytest.File):512 def runtest(self):513 pass514 class MyFile2(pytest.File):515 def collect(self):516 return [Item2("hello", parent=self)]517 class Item2(pytest.Item):518 def runtest(self):519 pass520 """)521 p = testdir.makefile(".abc", "")522 result = testdir.runpytest()523 assert result.ret == 0524 result.stdout.fnmatch_lines([525 "*2 passed*",526 ])527 res = testdir.runpytest("%s::hello" % p.basename)528 res.stdout.fnmatch_lines([529 "*1 passed*",530 ])531class TestNodekeywords:532 def test_no_under(self, testdir):533 modcol = testdir.getmodulecol("""534 def test_pass(): pass535 def test_fail(): assert 0536 """)537 l = list(modcol.keywords)538 assert modcol.name in l539 for x in l:540 assert not x.startswith("_")541 assert modcol.name in repr(modcol.keywords)542 def test_issue345(self, testdir):543 testdir.makepyfile("""544 def test_should_not_be_selected():545 assert False, 'I should not have been selected to run'546 def test___repr__():547 pass548 """)549 reprec = testdir.inline_run("-k repr")...

Full Screen

Full Screen

conftest.py

Source:conftest.py Github

copy

Full Screen

1import unittest.mock2import sys3def pytest_collectstart():...

Full Screen

Full Screen

Pytest Tutorial

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.

Chapters

  1. What is pytest
  2. Pytest installation: Want to start pytest from scratch? See how to install and configure pytest for Python automation testing.
  3. Run first test with pytest framework: Follow this step-by-step tutorial to write and run your first pytest script.
  4. Parallel testing with pytest: A hands-on guide to parallel testing with pytest to improve the scalability of your test automation.
  5. Generate pytest reports: Reports make it easier to understand the results of pytest-based test runs. Learn how to generate pytest reports.
  6. Pytest Parameterized tests: Create and run your pytest scripts while avoiding code duplication and increasing test coverage with parameterization.
  7. Pytest Fixtures: Check out how to implement pytest fixtures for your end-to-end testing needs.
  8. Execute Multiple Test Cases: Explore different scenarios for running multiple test cases in pytest from a single file.
  9. Stop Test Suite after N Test Failures: See how to stop your test suite after n test failures in pytest using the @pytest.mark.incremental decorator and maxfail command-line option.

YouTube

Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.

https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP

Run Pytest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful