Best Python code snippet using pytest
test_conftest.py
Source:test_conftest.py
...173 # /symlinktests -> /real/app/tests (running at symlinktests should fail)174 # /symlink -> /real (running at /symlink should work)175 real = testdir.tmpdir.mkdir("real")176 realtests = real.mkdir("app").mkdir("tests")177 symlink_or_skip(realtests, testdir.tmpdir.join("symlinktests"))178 symlink_or_skip(real, testdir.tmpdir.join("symlink"))179 testdir.makepyfile(180 **{181 "real/app/tests/test_foo.py": "def test1(fixture): pass",182 "real/conftest.py": textwrap.dedent(183 """184 import pytest185 print("conftest_loaded")186 @pytest.fixture187 def fixture():188 print("fixture_used")189 """190 ),191 }192 )193 # Should fail because conftest cannot be found from the link structure.194 result = testdir.runpytest("-vs", "symlinktests")195 result.stdout.fnmatch_lines(["*fixture 'fixture' not found*"])196 assert result.ret == ExitCode.TESTS_FAILED197 # Should not cause "ValueError: Plugin already registered" (#4174).198 result = testdir.runpytest("-vs", "symlink")199 assert result.ret == ExitCode.OK200def test_conftest_symlink_files(testdir):201 """Symlinked conftest.py are found when pytest is executed in a directory with symlinked202 files."""203 real = testdir.tmpdir.mkdir("real")204 source = {205 "app/test_foo.py": "def test1(fixture): pass",206 "app/__init__.py": "",207 "app/conftest.py": textwrap.dedent(208 """209 import pytest210 print("conftest_loaded")211 @pytest.fixture212 def fixture():213 print("fixture_used")214 """215 ),216 }217 testdir.makepyfile(**{"real/%s" % k: v for k, v in source.items()})218 # Create a build directory that contains symlinks to actual files219 # but doesn't symlink actual directories.220 build = testdir.tmpdir.mkdir("build")221 build.mkdir("app")222 for f in source:223 symlink_or_skip(real.join(f), build.join(f))224 build.chdir()225 result = testdir.runpytest("-vs", "app/test_foo.py")226 result.stdout.fnmatch_lines(["*conftest_loaded*", "PASSED"])227 assert result.ret == ExitCode.OK228@pytest.mark.skipif(229 os.path.normcase("x") != os.path.normcase("X"),230 reason="only relevant for case insensitive file systems",231)232def test_conftest_badcase(testdir):233 """Check conftest.py loading when directory casing is wrong (#5792)."""234 testdir.tmpdir.mkdir("JenkinsRoot").mkdir("test")235 source = {"setup.py": "", "test/__init__.py": "", "test/conftest.py": ""}236 testdir.makepyfile(**{"JenkinsRoot/%s" % k: v for k, v in source.items()})237 testdir.tmpdir.join("jenkinsroot/test").chdir()...
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!!