Config Pytest¶
Overview¶
Using scope module
Scope: sharing fixtures across classes, modules, packages or session Fixtures requiring network access depend on connectivity and are usually time-expensive to create. Extending the previous example, we can add a scope="module" parameter to the @pytest.fixture invocation to cause a smtp_connection fixture function, responsible to create a connection to a preexisting SMTP server, to only be invoked once per test module (the default is to invoke once per test function). Multiple test functions in a test module will thus each receive the same smtp_connection fixture instance, thus saving time. Possible values for scope are: function, class, module, package or session.
The next example puts the fixture function into a separate conftest.py file so that tests from multiple test modules in the directory can access the fixture function:
content of conftest.py¶
import pytest import smtplib
@pytest.fixture(scope="module") def smtp_connection(): return smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
content of test_module.py¶
def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg assert 0 # for demo purposes
def test_noop(smtp_connection): response, msg = smtp_connection.noop() assert response == 250 assert 0 # for demo purposes
Professional Testing with Python What Participants Say About This Course I think overall the course was held excellently. I learnt a lot and learnt how to use the different techniques and ideas.
—Vinay Mathew, course participant
Great opportunity to learn directly from the author of py.test.
—a course participant
Impressive & abundant, clear information. It’s been a privilege!
—a course participant
Target Audience People with a Python programming background, interested in best practices of testing Python libraries and applications. This course can be combined with introductory courses (see Recommended Module Combinations) to achieve appropriate Python skills.
Motivation Testing is essential for code quality. Tests need to be automated and regularly run to benefit from them. Frameworks such as pytest are based on many years of exercise and help you apply best practices to your tests. Apart from integrating with existing unittest/nose test suites, pytest offers some unique features for writing and organizing your test code.
Testing and releasing your code for many different Python versions and implementations can be hard. The tox project is an effective tool to integrate testing and deployment. It works with and connects to established tools including those for Continuous Integration.
Applying the Python philosophy that simple things should be simple and complex tasks should be possible, pytest and tox make it easy to get started but at the same time provide powerful features for professional software development.
Course Content In distinct steps we introduce terms and tools for testing in Python. Each step consists of an input, discussion and self-practice phase.
The main topics of the course are:
About testing: Why and how to write tests
About pytest: Why pytest, popularity, history overview
The basics: Fundamental pytest features, test discovery and plain asserts
Configuration: Typical directory structure, configs, options
Marks: Marking/grouping tests, skipping tests
Parametrization: Running tests against sets of input/output data
Fixtures: Providing test data, setting up objects, modularity
Built-in fixtures: Temporary files, patching, capturing output, test information
Fixtures advanced: Caching, cleanup/teardown, implicit fixtures, parametrizing
Migrating to pytest: Running existing testsuites, incremental rewriting, tooling
Mocking: Dealing with dependencies which are in our way, alternatives, monkeypatch and unittest.mock, mocking libraries and helpers
Plugin tour: Coverage, distributed testing, output improvements, alternative test syntax, testing C libraries, plugin overview
Property-based testing: Using hypothesis to generate test data
Writing plugins: Extending pytest via custom hooks, domain-specific languages
Open space: Discussions of your choice, postponed issues from the course
Optional tox basics: Packaging, running tests against different Python versions
tox configuration: Defining environments
tox extra: Running tools via tox, integrating into Continuous Integration (CI)
Each bullet point will be covered with two to three short units with exercises.
Course Style This is a hands-on course with lots of student activities. The presentation parts are usually short and include real-life examples. Students will create their own tests from the very beginning. Exercises are an important part of the training. Students are encouraged to bring their own problems to be discussed during the course.
Exercises The participants can follow all steps directly on their computers. There are exercises at the end of each unit providing ample opportunity to apply the freshly learned knowledge.
Software We use our online programing system that contains all needed software. There is no need to install any additional software. A modern internet browser and a decent internet connection will be enough.
Hardware for Open In-Person Trainings For open trainings at our teaching center you can use your own laptop. Alternatively, we provide teaching computers. Please let us know if you need one in your registration form.
Course Material Every participant receives comprehensive materials in PDF format that cover the whole course content as well as all source code.
Recommended Module Combinations You should have intermediate Python experience or attend the course Python for Programmers before. You might be also interested in the course Advanced Python.
File Ini set up for
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths = tests
console_output_style = progress
; Timeout in second
faulthandler_timeout= 60
; Enable log display during test run
log_cli = True
log_cli_level = INFO
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_file = logs/pytest-logs.txt
; One or more Glob-style file patterns determining which python files are considered as test modules.
; Search for multiple glob patterns by adding a space between patterns:
python_files = test_*.py check_*.py example_*.py
Reference¶
(e.g. writing test functions, parametrize, or what a fixture is) and want to learn how to take the next step to improve your test suites.
If you're already familiar with things like fixture caching scopes, autouse, or using the built-in tmp_path/monkeypatch/... fixtures: There will probably be some slides about concepts you already know, but there are also various little hidden tricks and gems I'll be showing.
TutorialTesting Description We'll cover things like:
Recommended pytest settings for more strictness What's xfail and why is it useful? How to mark an entire test file or single parameters Ways to deal with parametrize IDs and syntax Useful built-in pytest fixtures Caching for fixtures Using fixtures implicitly Advanced fixture and parametrization topics How to customize fixtures behavior based on markers or custom CLI arguments Patching, mocking, and alternatives Various useful plugins, and how to write your own Short intro to property-based testing with Hypothesis
https://python-academy.com/courses/python_course_testing.html
pytest --lf
pytest --ff
https://docs.pytest.org/en/7.4.x/how-to/cache.html#cache