How to use load_config_dict_from_file method in Pytest

Best Python code snippet using pytest

test_findpaths.py

Source: test_findpaths.py Github

copy

Full Screen

...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"...

Full Screen

Full Screen

StackOverFlow community discussions

Questions
Discussion

Can I make the pytest doctest module ignore a file?

Python2: Get longest Common Prefix path

How do I check if a string represents a number (float or int)?

How to pass multiple arguments in pytest using command line?

How to link PyCharm with PySpark?

Python order Dict with a pre-defined order

Is there a way to specify which pytest tests to run from a file?

What are metaclasses in Python?

Closing a file so I can delete it on Windows in Python?

Eclipse (with Pydev) keeps throwing SyntaxError

As MasterAndrey has mentioned, pytest_ignore_collect should do the trick. Important to note that you should put conftest.py to root folder (the one you run tests from).
Example:

import sys

def pytest_ignore_collect(path):
    if sys.version_info[0] > 2:
        if str(path).endswith("__py2.py"):
            return True
    else:
        if str(path).endswith("__py3.py"):
            return True

Since pytest v4.3.0 there is also --ignore-glob flag which allows to ignore by pattern. Example: pytest --doctest-modules --ignore-glob="*__py3.py" dir/

https://stackoverflow.com/questions/41358778/can-i-make-the-pytest-doctest-module-ignore-a-file

Blogs

Check out the latest blogs from LambdaTest on this topic:

How To Use Assertions In TestNG With Selenium

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on WebDriverIO Tutorial.

LambdaTest Now Live With An Online Selenium Grid For Automated Cross Browser Testing

It has been around a year since we went live with the first iteration of LambdaTest Platform. We started off our product offering manual cross browser testing solutions and kept expanding our platform. We were asked many feature requests, and we implemented quite a lot of them. However, the biggest demand was to bring automation testing to the platform. Today we deliver on this feature.

Selenium with Python Tutorial: Adding Extensions in Firefox for Testing

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Python Tutorial.

How To Handle Internationalization In Selenium WebDriver?

There are many software products that are built for a global audience. In my tenure as a developer, I have worked on multiple web (website or web app) projects that supported different languages. Though the Selenium framework was used for automation testing, using Internationalization in Selenium WebDriver Tutorial posed a huge challenge.

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