Best Python code snippet using pytest
acceptance_test.py
Source:acceptance_test.py
...12import pytest13from _pytest.main import EXIT_NOTESTSCOLLECTED14from _pytest.main import EXIT_USAGEERROR15from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG16def prepend_pythonpath(*dirs):17 cur = os.getenv("PYTHONPATH")18 if cur:19 dirs += (cur,)20 return os.pathsep.join(str(p) for p in dirs)21class TestGeneralUsage(object):22 def test_config_error(self, testdir):23 testdir.copy_example("conftest_usageerror/conftest.py")24 result = testdir.runpytest(testdir.tmpdir)25 assert result.ret == EXIT_USAGEERROR26 result.stderr.fnmatch_lines(["*ERROR: hello"])27 result.stdout.fnmatch_lines(["*pytest_unconfigure_called"])28 def test_root_conftest_syntax_error(self, testdir):29 testdir.makepyfile(conftest="raise SyntaxError\n")30 result = testdir.runpytest()31 result.stderr.fnmatch_lines(["*raise SyntaxError*"])32 assert result.ret != 033 def test_early_hook_error_issue38_1(self, testdir):34 testdir.makeconftest(35 """36 def pytest_sessionstart():37 0 / 038 """39 )40 result = testdir.runpytest(testdir.tmpdir)41 assert result.ret != 042 # tracestyle is native by default for hook failures43 result.stdout.fnmatch_lines(44 ["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"]45 )46 result = testdir.runpytest(testdir.tmpdir, "--fulltrace")47 assert result.ret != 048 # tracestyle is native by default for hook failures49 result.stdout.fnmatch_lines(50 ["*INTERNALERROR*def pytest_sessionstart():*", "*INTERNALERROR*0 / 0*"]51 )52 def test_early_hook_configure_error_issue38(self, testdir):53 testdir.makeconftest(54 """55 def pytest_configure():56 0 / 057 """58 )59 result = testdir.runpytest(testdir.tmpdir)60 assert result.ret != 061 # here we get it on stderr62 result.stderr.fnmatch_lines(63 ["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"]64 )65 def test_file_not_found(self, testdir):66 result = testdir.runpytest("asd")67 assert result.ret != 068 result.stderr.fnmatch_lines(["ERROR: file not found*asd"])69 def test_file_not_found_unconfigure_issue143(self, testdir):70 testdir.makeconftest(71 """72 def pytest_configure():73 print("---configure")74 def pytest_unconfigure():75 print("---unconfigure")76 """77 )78 result = testdir.runpytest("-s", "asd")79 assert result.ret == 4 # EXIT_USAGEERROR80 result.stderr.fnmatch_lines(["ERROR: file not found*asd"])81 result.stdout.fnmatch_lines(["*---configure", "*---unconfigure"])82 def test_config_preparse_plugin_option(self, testdir):83 testdir.makepyfile(84 pytest_xyz="""85 def pytest_addoption(parser):86 parser.addoption("--xyz", dest="xyz", action="store")87 """88 )89 testdir.makepyfile(90 test_one="""91 def test_option(pytestconfig):92 assert pytestconfig.option.xyz == "123"93 """94 )95 result = testdir.runpytest("-p", "pytest_xyz", "--xyz=123", syspathinsert=True)96 assert result.ret == 097 result.stdout.fnmatch_lines(["*1 passed*"])98 @pytest.mark.parametrize("load_cov_early", [True, False])99 def test_early_load_setuptools_name(self, testdir, monkeypatch, load_cov_early):100 pkg_resources = pytest.importorskip("pkg_resources")101 testdir.makepyfile(mytestplugin1_module="")102 testdir.makepyfile(mytestplugin2_module="")103 testdir.makepyfile(mycov_module="")104 testdir.syspathinsert()105 loaded = []106 @attr.s107 class DummyEntryPoint(object):108 name = attr.ib()109 module = attr.ib()110 version = "1.0"111 @property112 def project_name(self):113 return self.name114 def load(self):115 __import__(self.module)116 loaded.append(self.name)117 return sys.modules[self.module]118 @property119 def dist(self):120 return self121 def _get_metadata(self, *args):122 return []123 entry_points = [124 DummyEntryPoint("myplugin1", "mytestplugin1_module"),125 DummyEntryPoint("myplugin2", "mytestplugin2_module"),126 DummyEntryPoint("mycov", "mycov_module"),127 ]128 def my_iter(group, name=None):129 assert group == "pytest11"130 for ep in entry_points:131 if name is not None and ep.name != name:132 continue133 yield ep134 monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)135 params = ("-p", "mycov") if load_cov_early else ()136 testdir.runpytest_inprocess(*params)137 if load_cov_early:138 assert loaded == ["mycov", "myplugin1", "myplugin2"]139 else:140 assert loaded == ["myplugin1", "myplugin2", "mycov"]141 def test_assertion_magic(self, testdir):142 p = testdir.makepyfile(143 """144 def test_this():145 x = 0146 assert x147 """148 )149 result = testdir.runpytest(p)150 result.stdout.fnmatch_lines(["> assert x", "E assert 0"])151 assert result.ret == 1152 def test_nested_import_error(self, testdir):153 p = testdir.makepyfile(154 """155 import import_fails156 def test_this():157 assert import_fails.a == 1158 """159 )160 testdir.makepyfile(import_fails="import does_not_work")161 result = testdir.runpytest(p)162 result.stdout.fnmatch_lines(163 [164 # XXX on jython this fails: "> import import_fails",165 "ImportError while importing test module*",166 "*No module named *does_not_work*",167 ]168 )169 assert result.ret == 2170 def test_not_collectable_arguments(self, testdir):171 p1 = testdir.makepyfile("")172 p2 = testdir.makefile(".pyc", "123")173 result = testdir.runpytest(p1, p2)174 assert result.ret175 result.stderr.fnmatch_lines(["*ERROR: not found:*{}".format(p2.basename)])176 @pytest.mark.filterwarnings("default")177 def test_better_reporting_on_conftest_load_failure(self, testdir, request):178 """Show a user-friendly traceback on conftest import failures (#486, #3332)"""179 testdir.makepyfile("")180 testdir.makeconftest(181 """182 def foo():183 import qwerty184 foo()185 """186 )187 result = testdir.runpytest("--help")188 result.stdout.fnmatch_lines(189 """190 *--version*191 *warning*conftest.py*192 """193 )194 result = testdir.runpytest()195 dirname = request.node.name + "0"196 exc_name = (197 "ModuleNotFoundError" if sys.version_info >= (3, 6) else "ImportError"198 )199 result.stderr.fnmatch_lines(200 [201 "ImportError while loading conftest '*{sep}{dirname}{sep}conftest.py'.".format(202 dirname=dirname, sep=os.sep203 ),204 "conftest.py:3: in <module>",205 " foo()",206 "conftest.py:2: in foo",207 " import qwerty",208 "E {}: No module named {q}qwerty{q}".format(209 exc_name, q="'" if six.PY3 else ""210 ),211 ]212 )213 def test_early_skip(self, testdir):214 testdir.mkdir("xyz")215 testdir.makeconftest(216 """217 import pytest218 def pytest_collect_directory():219 pytest.skip("early")220 """221 )222 result = testdir.runpytest()223 assert result.ret == EXIT_NOTESTSCOLLECTED224 result.stdout.fnmatch_lines(["*1 skip*"])225 def test_issue88_initial_file_multinodes(self, testdir):226 testdir.copy_example("issue88_initial_file_multinodes")227 p = testdir.makepyfile("def test_hello(): pass")228 result = testdir.runpytest(p, "--collect-only")229 result.stdout.fnmatch_lines(["*MyFile*test_issue88*", "*Module*test_issue88*"])230 def test_issue93_initialnode_importing_capturing(self, testdir):231 testdir.makeconftest(232 """233 import sys234 print("should not be seen")235 sys.stderr.write("stder42\\n")236 """237 )238 result = testdir.runpytest()239 assert result.ret == EXIT_NOTESTSCOLLECTED240 assert "should not be seen" not in result.stdout.str()241 assert "stderr42" not in result.stderr.str()242 def test_conftest_printing_shows_if_error(self, testdir):243 testdir.makeconftest(244 """245 print("should be seen")246 assert 0247 """248 )249 result = testdir.runpytest()250 assert result.ret != 0251 assert "should be seen" in result.stdout.str()252 @pytest.mark.skipif(253 not hasattr(py.path.local, "mksymlinkto"),254 reason="symlink not available on this platform",255 )256 def test_chdir(self, testdir):257 testdir.tmpdir.join("py").mksymlinkto(py._pydir)258 p = testdir.tmpdir.join("main.py")259 p.write(260 textwrap.dedent(261 """\262 import sys, os263 sys.path.insert(0, '')264 import py265 print(py.__file__)266 print(py.__path__)267 os.chdir(os.path.dirname(os.getcwd()))268 print(py.log)269 """270 )271 )272 result = testdir.runpython(p)273 assert not result.ret274 def test_issue109_sibling_conftests_not_loaded(self, testdir):275 sub1 = testdir.mkdir("sub1")276 sub2 = testdir.mkdir("sub2")277 sub1.join("conftest.py").write("assert 0")278 result = testdir.runpytest(sub2)279 assert result.ret == EXIT_NOTESTSCOLLECTED280 sub2.ensure("__init__.py")281 p = sub2.ensure("test_hello.py")282 result = testdir.runpytest(p)283 assert result.ret == EXIT_NOTESTSCOLLECTED284 result = testdir.runpytest(sub1)285 assert result.ret == EXIT_USAGEERROR286 def test_directory_skipped(self, testdir):287 testdir.makeconftest(288 """289 import pytest290 def pytest_ignore_collect():291 pytest.skip("intentional")292 """293 )294 testdir.makepyfile("def test_hello(): pass")295 result = testdir.runpytest()296 assert result.ret == EXIT_NOTESTSCOLLECTED297 result.stdout.fnmatch_lines(["*1 skipped*"])298 def test_multiple_items_per_collector_byid(self, testdir):299 c = testdir.makeconftest(300 """301 import pytest302 class MyItem(pytest.Item):303 def runtest(self):304 pass305 class MyCollector(pytest.File):306 def collect(self):307 return [MyItem(name="xyz", parent=self)]308 def pytest_collect_file(path, parent):309 if path.basename.startswith("conftest"):310 return MyCollector(path, parent)311 """312 )313 result = testdir.runpytest(c.basename + "::" + "xyz")314 assert result.ret == 0315 result.stdout.fnmatch_lines(["*1 pass*"])316 def test_skip_on_generated_funcarg_id(self, testdir):317 testdir.makeconftest(318 """319 import pytest320 def pytest_generate_tests(metafunc):321 metafunc.parametrize('x', [3], ids=['hello-123'])322 def pytest_runtest_setup(item):323 print(item.keywords)324 if 'hello-123' in item.keywords:325 pytest.skip("hello")326 assert 0327 """328 )329 p = testdir.makepyfile("""def test_func(x): pass""")330 res = testdir.runpytest(p, SHOW_PYTEST_WARNINGS_ARG)331 assert res.ret == 0332 res.stdout.fnmatch_lines(["*1 skipped*"])333 def test_direct_addressing_selects(self, testdir):334 p = testdir.makepyfile(335 """336 def pytest_generate_tests(metafunc):337 metafunc.parametrize('i', [1, 2], ids=["1", "2"])338 def test_func(i):339 pass340 """341 )342 res = testdir.runpytest(343 p.basename + "::" + "test_func[1]", SHOW_PYTEST_WARNINGS_ARG344 )345 assert res.ret == 0346 res.stdout.fnmatch_lines(["*1 passed*"])347 def test_direct_addressing_notfound(self, testdir):348 p = testdir.makepyfile(349 """350 def test_func():351 pass352 """353 )354 res = testdir.runpytest(p.basename + "::" + "test_notfound")355 assert res.ret356 res.stderr.fnmatch_lines(["*ERROR*not found*"])357 def test_docstring_on_hookspec(self):358 from _pytest import hookspec359 for name, value in vars(hookspec).items():360 if name.startswith("pytest_"):361 assert value.__doc__, "no docstring for %s" % name362 def test_initialization_error_issue49(self, testdir):363 testdir.makeconftest(364 """365 def pytest_configure():366 x367 """368 )369 result = testdir.runpytest()370 assert result.ret == 3 # internal error371 result.stderr.fnmatch_lines(["INTERNAL*pytest_configure*", "INTERNAL*x*"])372 assert "sessionstarttime" not in result.stderr.str()373 @pytest.mark.parametrize("lookfor", ["test_fun.py::test_a"])374 def test_issue134_report_error_when_collecting_member(self, testdir, lookfor):375 testdir.makepyfile(376 test_fun="""377 def test_a():378 pass379 def"""380 )381 result = testdir.runpytest(lookfor)382 result.stdout.fnmatch_lines(["*SyntaxError*"])383 if "::" in lookfor:384 result.stderr.fnmatch_lines(["*ERROR*"])385 assert result.ret == 4 # usage error only if item not found386 def test_report_all_failed_collections_initargs(self, testdir):387 testdir.makeconftest(388 """389 from _pytest.main import EXIT_USAGEERROR390 def pytest_sessionfinish(exitstatus):391 assert exitstatus == EXIT_USAGEERROR392 print("pytest_sessionfinish_called")393 """394 )395 testdir.makepyfile(test_a="def", test_b="def")396 result = testdir.runpytest("test_a.py::a", "test_b.py::b")397 result.stderr.fnmatch_lines(["*ERROR*test_a.py::a*", "*ERROR*test_b.py::b*"])398 result.stdout.fnmatch_lines(["pytest_sessionfinish_called"])399 assert result.ret == EXIT_USAGEERROR400 @pytest.mark.usefixtures("recwarn")401 def test_namespace_import_doesnt_confuse_import_hook(self, testdir):402 """403 Ref #383. Python 3.3's namespace package messed with our import hooks404 Importing a module that didn't exist, even if the ImportError was405 gracefully handled, would make our test crash.406 Use recwarn here to silence this warning in Python 2.7:407 ImportWarning: Not importing directory '...\not_a_package': missing __init__.py408 """409 testdir.mkdir("not_a_package")410 p = testdir.makepyfile(411 """412 try:413 from not_a_package import doesnt_exist414 except ImportError:415 # We handle the import error gracefully here416 pass417 def test_whatever():418 pass419 """420 )421 res = testdir.runpytest(p.basename)422 assert res.ret == 0423 def test_unknown_option(self, testdir):424 result = testdir.runpytest("--qwlkej")425 result.stderr.fnmatch_lines(426 """427 *unrecognized*428 """429 )430 def test_getsourcelines_error_issue553(self, testdir, monkeypatch):431 monkeypatch.setattr("inspect.getsourcelines", None)432 p = testdir.makepyfile(433 """434 def raise_error(obj):435 raise IOError('source code not available')436 import inspect437 inspect.getsourcelines = raise_error438 def test_foo(invalid_fixture):439 pass440 """441 )442 res = testdir.runpytest(p)443 res.stdout.fnmatch_lines(444 ["*source code not available*", "E*fixture 'invalid_fixture' not found"]445 )446 def test_plugins_given_as_strings(self, tmpdir, monkeypatch, _sys_snapshot):447 """test that str values passed to main() as `plugins` arg448 are interpreted as module names to be imported and registered.449 #855.450 """451 with pytest.raises(ImportError) as excinfo:452 pytest.main([str(tmpdir)], plugins=["invalid.module"])453 assert "invalid" in str(excinfo.value)454 p = tmpdir.join("test_test_plugins_given_as_strings.py")455 p.write("def test_foo(): pass")456 mod = types.ModuleType("myplugin")457 monkeypatch.setitem(sys.modules, "myplugin", mod)458 assert pytest.main(args=[str(tmpdir)], plugins=["myplugin"]) == 0459 def test_parametrized_with_bytes_regex(self, testdir):460 p = testdir.makepyfile(461 """462 import re463 import pytest464 @pytest.mark.parametrize('r', [re.compile(b'foo')])465 def test_stuff(r):466 pass467 """468 )469 res = testdir.runpytest(p)470 res.stdout.fnmatch_lines(["*1 passed*"])471 def test_parametrized_with_null_bytes(self, testdir):472 """Test parametrization with values that contain null bytes and unicode characters (#2644, #2957)"""473 p = testdir.makepyfile(474 u"""475 # encoding: UTF-8476 import pytest477 @pytest.mark.parametrize("data", [b"\\x00", "\\x00", u'ação'])478 def test_foo(data):479 assert data480 """481 )482 res = testdir.runpytest(p)483 res.assert_outcomes(passed=3)484class TestInvocationVariants(object):485 def test_earlyinit(self, testdir):486 p = testdir.makepyfile(487 """488 import pytest489 assert hasattr(pytest, 'mark')490 """491 )492 result = testdir.runpython(p)493 assert result.ret == 0494 @pytest.mark.xfail("sys.platform.startswith('java')")495 def test_pydoc(self, testdir):496 for name in ("py.test", "pytest"):497 result = testdir.runpython_c("import {};help({})".format(name, name))498 assert result.ret == 0499 s = result.stdout.str()500 assert "MarkGenerator" in s501 def test_import_star_py_dot_test(self, testdir):502 p = testdir.makepyfile(503 """504 from py.test import *505 #collect506 #cmdline507 #Item508 # assert collect.Item is Item509 # assert collect.Collector is Collector510 main511 skip512 xfail513 """514 )515 result = testdir.runpython(p)516 assert result.ret == 0517 def test_import_star_pytest(self, testdir):518 p = testdir.makepyfile(519 """520 from pytest import *521 #Item522 #File523 main524 skip525 xfail526 """527 )528 result = testdir.runpython(p)529 assert result.ret == 0530 def test_double_pytestcmdline(self, testdir):531 p = testdir.makepyfile(532 run="""533 import pytest534 pytest.main()535 pytest.main()536 """537 )538 testdir.makepyfile(539 """540 def test_hello():541 pass542 """543 )544 result = testdir.runpython(p)545 result.stdout.fnmatch_lines(["*1 passed*", "*1 passed*"])546 def test_python_minus_m_invocation_ok(self, testdir):547 p1 = testdir.makepyfile("def test_hello(): pass")548 res = testdir.run(sys.executable, "-m", "pytest", str(p1))549 assert res.ret == 0550 def test_python_minus_m_invocation_fail(self, testdir):551 p1 = testdir.makepyfile("def test_fail(): 0/0")552 res = testdir.run(sys.executable, "-m", "pytest", str(p1))553 assert res.ret == 1554 def test_python_pytest_package(self, testdir):555 p1 = testdir.makepyfile("def test_pass(): pass")556 res = testdir.run(sys.executable, "-m", "pytest", str(p1))557 assert res.ret == 0558 res.stdout.fnmatch_lines(["*1 passed*"])559 def test_equivalence_pytest_pytest(self):560 assert pytest.main == py.test.cmdline.main561 def test_invoke_with_invalid_type(self, capsys):562 with pytest.raises(563 TypeError, match="expected to be a list or tuple of strings, got: '-h'"564 ):565 pytest.main("-h")566 def test_invoke_with_path(self, tmpdir, capsys):567 retcode = pytest.main(tmpdir)568 assert retcode == EXIT_NOTESTSCOLLECTED569 out, err = capsys.readouterr()570 def test_invoke_plugin_api(self, testdir, capsys):571 class MyPlugin(object):572 def pytest_addoption(self, parser):573 parser.addoption("--myopt")574 pytest.main(["-h"], plugins=[MyPlugin()])575 out, err = capsys.readouterr()576 assert "--myopt" in out577 def test_pyargs_importerror(self, testdir, monkeypatch):578 monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)579 path = testdir.mkpydir("tpkg")580 path.join("test_hello.py").write("raise ImportError")581 result = testdir.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True)582 assert result.ret != 0583 result.stdout.fnmatch_lines(["collected*0*items*/*1*errors"])584 def test_cmdline_python_package(self, testdir, monkeypatch):585 import warnings586 monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)587 path = testdir.mkpydir("tpkg")588 path.join("test_hello.py").write("def test_hello(): pass")589 path.join("test_world.py").write("def test_world(): pass")590 result = testdir.runpytest("--pyargs", "tpkg")591 assert result.ret == 0592 result.stdout.fnmatch_lines(["*2 passed*"])593 result = testdir.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True)594 assert result.ret == 0595 result.stdout.fnmatch_lines(["*1 passed*"])596 empty_package = testdir.mkpydir("empty_package")597 monkeypatch.setenv("PYTHONPATH", str(empty_package), prepend=os.pathsep)598 # the path which is not a package raises a warning on pypy;599 # no idea why only pypy and not normal python warn about it here600 with warnings.catch_warnings():601 warnings.simplefilter("ignore", ImportWarning)602 result = testdir.runpytest("--pyargs", ".")603 assert result.ret == 0604 result.stdout.fnmatch_lines(["*2 passed*"])605 monkeypatch.setenv("PYTHONPATH", str(testdir), prepend=os.pathsep)606 result = testdir.runpytest("--pyargs", "tpkg.test_missing", syspathinsert=True)607 assert result.ret != 0608 result.stderr.fnmatch_lines(["*not*found*test_missing*"])609 def test_cmdline_python_namespace_package(self, testdir, monkeypatch):610 """611 test --pyargs option with namespace packages (#1567)612 Ref: https://packaging.python.org/guides/packaging-namespace-packages/613 """614 monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)615 search_path = []616 for dirname in "hello", "world":617 d = testdir.mkdir(dirname)618 search_path.append(d)619 ns = d.mkdir("ns_pkg")620 ns.join("__init__.py").write(621 "__import__('pkg_resources').declare_namespace(__name__)"622 )623 lib = ns.mkdir(dirname)624 lib.ensure("__init__.py")625 lib.join("test_{}.py".format(dirname)).write(626 "def test_{}(): pass\ndef test_other():pass".format(dirname)627 )628 # The structure of the test directory is now:629 # .630 # âââ hello631 # â âââ ns_pkg632 # â âââ __init__.py633 # â âââ hello634 # â âââ __init__.py635 # â âââ test_hello.py636 # âââ world637 # âââ ns_pkg638 # âââ __init__.py639 # âââ world640 # âââ __init__.py641 # âââ test_world.py642 # NOTE: the different/reversed ordering is intentional here.643 monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path))644 for p in search_path:645 monkeypatch.syspath_prepend(p)646 # mixed module and filenames:647 monkeypatch.chdir("world")648 result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world")649 assert result.ret == 0650 result.stdout.fnmatch_lines(651 [652 "test_hello.py::test_hello*PASSED*",653 "test_hello.py::test_other*PASSED*",654 "ns_pkg/world/test_world.py::test_world*PASSED*",655 "ns_pkg/world/test_world.py::test_other*PASSED*",656 "*4 passed in*",657 ]658 )659 # specify tests within a module660 testdir.chdir()661 result = testdir.runpytest(662 "--pyargs", "-v", "ns_pkg.world.test_world::test_other"663 )664 assert result.ret == 0665 result.stdout.fnmatch_lines(666 ["*test_world.py::test_other*PASSED*", "*1 passed*"]667 )668 def test_invoke_test_and_doctestmodules(self, testdir):669 p = testdir.makepyfile(670 """671 def test():672 pass673 """674 )675 result = testdir.runpytest(str(p) + "::test", "--doctest-modules")676 result.stdout.fnmatch_lines(["*1 passed*"])677 @pytest.mark.skipif(not hasattr(os, "symlink"), reason="requires symlinks")678 def test_cmdline_python_package_symlink(self, testdir, monkeypatch):679 """680 test --pyargs option with packages with path containing symlink can681 have conftest.py in their package (#2985)682 """683 # dummy check that we can actually create symlinks: on Windows `os.symlink` is available,684 # but normal users require special admin privileges to create symlinks.685 if sys.platform == "win32":686 try:687 os.symlink(688 str(testdir.tmpdir.ensure("tmpfile")),689 str(testdir.tmpdir.join("tmpfile2")),690 )691 except OSError as e:692 pytest.skip(six.text_type(e.args[0]))693 monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)694 dirname = "lib"695 d = testdir.mkdir(dirname)696 foo = d.mkdir("foo")697 foo.ensure("__init__.py")698 lib = foo.mkdir("bar")699 lib.ensure("__init__.py")700 lib.join("test_bar.py").write(701 "def test_bar(): pass\ndef test_other(a_fixture):pass"702 )703 lib.join("conftest.py").write(704 "import pytest\n@pytest.fixture\ndef a_fixture():pass"705 )706 d_local = testdir.mkdir("local")707 symlink_location = os.path.join(str(d_local), "lib")708 if six.PY2:709 os.symlink(str(d), symlink_location)710 else:711 os.symlink(str(d), symlink_location, target_is_directory=True)712 # The structure of the test directory is now:713 # .714 # âââ local715 # â âââ lib -> ../lib716 # âââ lib717 # âââ foo718 # âââ __init__.py719 # âââ bar720 # âââ __init__.py721 # âââ conftest.py722 # âââ test_bar.py723 # NOTE: the different/reversed ordering is intentional here.724 search_path = ["lib", os.path.join("local", "lib")]725 monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path))726 for p in search_path:727 monkeypatch.syspath_prepend(p)728 # module picked up in symlink-ed directory:729 # It picks up local/lib/foo/bar (symlink) via sys.path.730 result = testdir.runpytest("--pyargs", "-v", "foo.bar")731 testdir.chdir()732 assert result.ret == 0733 if hasattr(py.path.local, "mksymlinkto"):734 result.stdout.fnmatch_lines(735 [736 "lib/foo/bar/test_bar.py::test_bar PASSED*",737 "lib/foo/bar/test_bar.py::test_other PASSED*",738 "*2 passed*",739 ]...
benchmark.py
Source:benchmark.py
...36def build_from_cfg(cfg, registery):37 obj_name = cfg.pop('name')38 obj = registery.get(obj_name)39 return obj(**cfg)40def prepend_pythonpath(cfg, key1, key2):41 pythonpath = os.environ['PYTHONPATH']42 if cfg[key1][key2].startswith('/'):43 return44 cfg[key1][key2] = os.path.join(pythonpath, cfg[key1][key2])45if __name__ == '__main__':46 assert args.cfg.endswith('yaml')47 with open(args.cfg, 'r') as f:48 cfg = yaml.safe_load(f)49 # prepend PYTHONPATH to each path50 prepend_pythonpath(cfg, key1='Data', key2='parentPath')51 prepend_pythonpath(cfg, key1='Benchmark', key2='parentPath')52 prepend_pythonpath(cfg, key1='Model', key2='modelPath')53 # Download requested data if does not exist54 downloader = Downloader(**cfg['Data'])55 downloader.get()56 # Instantiate benchmarking57 benchmark = Benchmark(**cfg['Benchmark'])58 # Instantiate model59 model = build_from_cfg(cfg=cfg['Model'], registery=MODELS)60 # Run benchmarking...
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!!