Blog

4 minutes read
To count test cases written with pytest, you can use the -k option with a specific expression to match your test cases. The -k option allows you to run only the test cases that match the given expression. You can use this option with the pytest command followed by the test case expression to filter and count the number of test cases.
4 minutes read
In pytest, you can patch globally by using the autouse parameter in the pytest.fixture decorator. By setting autouse=True, the fixture will be used automatically without needing to explicitly request it in tests. This allows you to patch a function or object globally for all tests that need it. This can be especially useful for mocking external dependencies or setting up common test data across multiple test cases.
2 minutes read
In pytest, you can use override parameters of a fixture by using the pytest.mark.parametrize decorator along with the pytest.fixture decorator. This allows you to specify the values for the parameters of a fixture for a specific test case. By using this technique, you can override the default values of fixture parameters and customize them for individual tests. This can be particularly useful when you need to run a test with different input values or configurations.
4 minutes read
To run only unmarked tests in pytest, you can use the "-m" flag followed by the expression "!MARKER". This will filter out all tests with the specified marker and only run the unmarked tests. For example, to run only unmarked tests you can use the command:pytest -m "!MARKER"How to skip marked tests in pytest?In pytest, you can skip tests by using the @pytest.mark.skip decorator. Here's an example of how to skip a test in pytest: import pytest @pytest.mark.
3 minutes read
To pass arguments in pytest by command line, you can simply add the arguments to the pytest command when running your tests. For example, you can specify the markers to run specific tests, the verbosity level, or any other pytest options that you want to include.To pass arguments, you can simply add them after the pytest command when running your tests. For example, if you want to specify a particular marker to run specific tests, you can use the "-m" option followed by the marker name.
2 minutes read
Pytest typically searches for test modules and files in the current directory and its subdirectories. It looks for Python files with names starting with 'test_' or ending with '_test', as well as directories named 'test' containing test files. Pytest also allows specifying specific files or directories to test using command-line options and configuration files.
4 minutes read
To use a coroutine as a pytest fixture, you first need to define a fixture function using the pytest.fixture decorator. Within this function, you can define a coroutine using the async def keyword. You can then use the asyncio library to await the coroutine inside the fixture function.When writing tests that require the coroutine fixture, you can simply pass the fixture function name as an argument to the test function.
3 minutes read
To raise an exception in pytest, you can use the pytest.raises() context manager. Within this context manager, you can execute the code that you expect to raise an exception. Pytest will then capture the exception and allow you to assert on its type, message, or any other relevant information. This is a useful way to test that your code behaves as expected in error conditions. You can also use the with pytest.
3 minutes read
To add stdin interaction with pytest, you can use the monkeypatch fixture provided by pytest. This fixture allows you to modify the behavior of stdin during your test. By using monkeypatch.setattr() method, you can set the stdin to the desired input value when the test runs. This way, you can simulate user input from the command line while running your pytest tests. This is particularly useful when testing functions that rely on user input from stdin.What is stdin interaction testing in pytest.
6 minutes read
To set a default per test timeout in pytest, you can use the @pytest.mark.timeout decorator to specify a timeout value for individual test functions. Alternatively, you can use the pytest_addoption hook to define a command-line option for setting a default timeout value for all tests. By using these techniques, you can easily configure the maximum amount of time that each test is allowed to run before being considered as failed due to timeout.