{
  "id": 417447,
  "title": "Pytest Built-in Fixtures",
  "url": "https://urgent.news/2026/08/09/pytest-built-in-fixtures",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-09T22:28:21.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/echen4628/pytest-built-in-fixtures-29h6"
  },
  "original_language": "en",
  "account": "Pytest is a testing framework that offers built-in fixtures to streamline the testing process. These fixtures enable the reuse of setup code, making tests more efficient and maintainable. Two of the most commonly used built-in fixtures are tmp_path and monkeypatch.\n\ntmp_path provides a unique temporary directory for each test, which can be accessed via the tmp_path object. This is particularly useful for tests that require a specific directory structure or file operations. For example, a test function can simply use tmp_path as the argument, and pytest will handle the creation and cleanup of the temporary directory.\n\nOn the other hand, monkeypatch is a versatile fixture that allows for the manipulation of objects, environment variables, and other system settings. It offers a range of methods to set and delete attributes of objects, modify mapping items, set and delete environment variables, and more. With monkeypatch, developers can easily simulate various scenarios, such as modifying environment variables or altering object attributes, within the scope of a single test function. This is especially helpful when testing code that relies on specific configurations or system settings.\n\nThe source material highlights the benefits of using pytest's built-in fixtures, such as improved test organization, reduced duplication, and enhanced maintainability. By leveraging these fixtures, developers can focus on the core logic of their tests rather than reinventing the wheel for common setup tasks. The article concludes by recommending that when faced with a testing challenge in pytest, it's worth exploring the available built-in fixtures before resorting to custom solutions.",
  "summary": "As I've been onboarding to my new position, I've been reviewing some of my coworker's code. I learned that pytest 1) has some built-in fixtures, and 2) they are really useful! First, briefly, what are fixtures? When writing pytest tests, it's pretty frequent to write fixtures for reusability. They are used to set up tests. For example, a common fixture is: @pytest.fixture () def dataset_path ():…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}