Best Python code snippet using pytest
test_main.py
Source:test_main.py
...103 def invocation_path(self, invocation_dir: py.path.local) -> Path:104 return Path(str(invocation_dir))105 def test_file(self, invocation_dir: py.path.local, invocation_path: Path) -> None:106 """File and parts."""107 assert resolve_collection_argument(invocation_path, "src/pkg/test.py") == (108 invocation_dir / "src/pkg/test.py",109 [],110 )111 assert resolve_collection_argument(invocation_path, "src/pkg/test.py::") == (112 invocation_dir / "src/pkg/test.py",113 [""],114 )115 assert resolve_collection_argument(116 invocation_path, "src/pkg/test.py::foo::bar"117 ) == (invocation_dir / "src/pkg/test.py", ["foo", "bar"])118 assert resolve_collection_argument(119 invocation_path, "src/pkg/test.py::foo::bar::"120 ) == (invocation_dir / "src/pkg/test.py", ["foo", "bar", ""])121 def test_dir(self, invocation_dir: py.path.local, invocation_path: Path) -> None:122 """Directory and parts."""123 assert resolve_collection_argument(invocation_path, "src/pkg") == (124 invocation_dir / "src/pkg",125 [],126 )127 with pytest.raises(128 UsageError, match=r"directory argument cannot contain :: selection parts"129 ):130 resolve_collection_argument(invocation_path, "src/pkg::")131 with pytest.raises(132 UsageError, match=r"directory argument cannot contain :: selection parts"133 ):134 resolve_collection_argument(invocation_path, "src/pkg::foo::bar")135 def test_pypath(self, invocation_dir: py.path.local, invocation_path: Path) -> None:136 """Dotted name and parts."""137 assert resolve_collection_argument(138 invocation_path, "pkg.test", as_pypath=True139 ) == (invocation_dir / "src/pkg/test.py", [])140 assert resolve_collection_argument(141 invocation_path, "pkg.test::foo::bar", as_pypath=True142 ) == (invocation_dir / "src/pkg/test.py", ["foo", "bar"])143 assert resolve_collection_argument(invocation_path, "pkg", as_pypath=True) == (144 invocation_dir / "src/pkg",145 [],146 )147 with pytest.raises(148 UsageError, match=r"package argument cannot contain :: selection parts"149 ):150 resolve_collection_argument(151 invocation_path, "pkg::foo::bar", as_pypath=True152 )153 def test_does_not_exist(self, invocation_path: Path) -> None:154 """Given a file/module that does not exist raises UsageError."""155 with pytest.raises(156 UsageError, match=re.escape("file or directory not found: foobar")157 ):158 resolve_collection_argument(invocation_path, "foobar")159 with pytest.raises(160 UsageError,161 match=re.escape(162 "module or package not found: foobar (missing __init__.py?)"163 ),164 ):165 resolve_collection_argument(invocation_path, "foobar", as_pypath=True)166 def test_absolute_paths_are_resolved_correctly(167 self, invocation_dir: py.path.local, invocation_path: Path168 ) -> None:169 """Absolute paths resolve back to absolute paths."""170 full_path = str(invocation_dir / "src")171 assert resolve_collection_argument(invocation_path, full_path) == (172 py.path.local(os.path.abspath("src")),173 [],174 )175 # ensure full paths given in the command-line without the drive letter resolve176 # to the full path correctly (#7628)177 drive, full_path_without_drive = os.path.splitdrive(full_path)178 assert resolve_collection_argument(179 invocation_path, full_path_without_drive180 ) == (py.path.local(os.path.abspath("src")), [])181def test_module_full_path_without_drive(testdir):182 """Collect and run test using full path except for the drive letter (#7628).183 Passing a full path without a drive letter would trigger a bug in py.path.local184 where it would keep the full path without the drive letter around, instead of resolving185 to the full path, resulting in fixtures node ids not matching against test node ids correctly.186 """187 testdir.makepyfile(188 **{189 "project/conftest.py": """190 import pytest191 @pytest.fixture192 def fix(): return 1...
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!!