Best Python code snippet using pytest
test_findpaths.py
Source:test_findpaths.py
...8 def test_empty_pytest_ini(self, tmp_path: Path) -> None:9 """pytest.ini files are always considered for configuration, even if empty"""10 fn = tmp_path / "pytest.ini"11 fn.write_text("", encoding="utf-8")12 assert load_config_dict_from_file(fn) == {}13 def test_pytest_ini(self, tmp_path: Path) -> None:14 """[pytest] section in pytest.ini files is read correctly"""15 fn = tmp_path / "pytest.ini"16 fn.write_text("[pytest]\nx=1", encoding="utf-8")17 assert load_config_dict_from_file(fn) == {"x": "1"}18 def test_custom_ini(self, tmp_path: Path) -> None:19 """[pytest] section in any .ini file is read correctly"""20 fn = tmp_path / "custom.ini"21 fn.write_text("[pytest]\nx=1", encoding="utf-8")22 assert load_config_dict_from_file(fn) == {"x": "1"}23 def test_custom_ini_without_section(self, tmp_path: Path) -> None:24 """Custom .ini files without [pytest] section are not considered for configuration"""25 fn = tmp_path / "custom.ini"26 fn.write_text("[custom]", encoding="utf-8")27 assert load_config_dict_from_file(fn) is None28 def test_custom_cfg_file(self, tmp_path: Path) -> None:29 """Custom .cfg files without [tool:pytest] section are not considered for configuration"""30 fn = tmp_path / "custom.cfg"31 fn.write_text("[custom]", encoding="utf-8")32 assert load_config_dict_from_file(fn) is None33 def test_valid_cfg_file(self, tmp_path: Path) -> None:34 """Custom .cfg files with [tool:pytest] section are read correctly"""35 fn = tmp_path / "custom.cfg"36 fn.write_text("[tool:pytest]\nx=1", encoding="utf-8")37 assert load_config_dict_from_file(fn) == {"x": "1"}38 def test_unsupported_pytest_section_in_cfg_file(self, tmp_path: Path) -> None:39 """.cfg files with [pytest] section are no longer supported and should fail to alert users"""40 fn = tmp_path / "custom.cfg"41 fn.write_text("[pytest]", encoding="utf-8")42 with pytest.raises(pytest.fail.Exception):43 load_config_dict_from_file(fn)44 def test_invalid_toml_file(self, tmp_path: Path) -> None:45 """.toml files without [tool.pytest.ini_options] are not considered for configuration."""46 fn = tmp_path / "myconfig.toml"47 fn.write_text(48 dedent(49 """50 [build_system]51 x = 152 """53 ),54 encoding="utf-8",55 )56 assert load_config_dict_from_file(fn) is None57 def test_valid_toml_file(self, tmp_path: Path) -> None:58 """.toml files with [tool.pytest.ini_options] are read correctly, including changing59 data types to str/list for compatibility with other configuration options."""60 fn = tmp_path / "myconfig.toml"61 fn.write_text(62 dedent(63 """64 [tool.pytest.ini_options]65 x = 166 y = 20.067 values = ["tests", "integration"]68 name = "foo"69 """70 ),71 encoding="utf-8",72 )73 assert load_config_dict_from_file(fn) == {74 "x": "1",75 "y": "20.0",76 "values": ["tests", "integration"],77 "name": "foo",78 }79class TestCommonAncestor:80 def test_has_ancestor(self, tmp_path: Path) -> None:81 fn1 = tmp_path / "foo" / "bar" / "test_1.py"82 fn1.parent.mkdir(parents=True)83 fn1.touch()84 fn2 = tmp_path / "foo" / "zaz" / "test_2.py"85 fn2.parent.mkdir(parents=True)86 fn2.touch()87 assert get_common_ancestor([fn1, fn2]) == tmp_path / "foo"...
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!!