Urgent.News

What's breaking now, across thousands of outlets.

Tech

Pytest Built-in Fixtures

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 ():…

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.

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

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

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

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Apple tests Chinese memory chips

Apple is testing memory chips from China’s CXMT across its product lines, including iPhones and MacBooks, as global tech reckons with an AI-induced memory shortage.

Ghidra internals: Where are my logs ?

Ghidra uses the Apache Log4j logging library to store a lot of execution messages. Finding these logs can be quite tricky, especially if you launch Ghidra from a shell (using ghidraRun ) because no…

Kernel prepatch 7.2-rc7

The 7.2-rc7 kernel prepatch is out for testing. It is still bigger than Linus would like, but he said nonetheless: " I don't currently see any value in delaying the 7.2 release, so I would expect that…

More from Sunday 9 August →