Mocking an Attribute

Mocking an Attribute

Preview unavailable

You must log in or sign up to view this lesson.

LoginSign up

The Complete pytest Course - original layout

Buy nowLearn more

Intro and Source Code

  • Welcome + Introduction
  • Source Code

Part 1: Primary Power

  • Part 1: Primary Power
  • Part 1: Primary Power

Ch 1: Getting Started with pytest

  • Intro
  • Installing pytest
  • Our first tests
  • Running pytest
  • Test discovery
  • Test outcomes

Ch 2: Writing Test Function

  • Intro
  • Installing cards
  • Playing with cards
  • The cards.Card dataclass
  • Writing Knowledge-Building Tests
  • Running Tests
  • Check Your Setup
  • Bonus: Running Tests in PyCharm
  • Using assert Statements
  • Failing with pytest.fail() and exceptions
  • Writing Assertion Helper Functions
  • Testing for Expected Exceptions
  • Structuring Test Functions
  • Grouping Tests with Classes
  • Running a Subset of Tests
  • Bonus: Virtual Environment functions create/activate/exit for bash/zsh

Ch 3: pytest Fixtures

  • Intro
  • Visualizing Fixtures
  • Testing the cards API
  • Using Fixtures for Setup and Teardown
  • Tracing Fixture Execution with --setup-show and -s
  • Seeing print output with -s
  • Specifying Fixture Scope
  • Sharing Fixtures through conftest.py
  • Finding Where Fixtures are Defined
  • Intermission 1
  • Using Multiple Fixture Levels
  • Using Multiple Fixtures per Test
  • Intermission 2
  • Deciding Fixture Scope Dynamically
  • Using autouse Fixtures
  • Renaming Fixtures

Ch 4: Builtin Fixtures

  • Intro
  • Using tmp_path and tmp_path_factory
  • Using capsys
  • Output capture with Typer
  • Disabling output capture with capsys
  • The problem with a live db
  • Looking at monkeypatch docs
  • Cards internals
  • Patching get_path
  • Patching pathlib.Path.home
  • Patching an environment variable
  • Design for Testability

Ch 5: Parametrization

  • Intro
  • Running "cards finish" on the Command Line
  • Looking at the "finish()" API Function
  • Testing Without Parametrize
  • Combining Tests with a Loop
  • Parametrizing Functions
  • Only Parametrizing Meaningful Changes
  • Parametrizing Fixtures
  • What is a Hook Function
  • Parametrizing with pytest_generate_tests
  • Using Keywords to Select Test Cases

Ch 6: Markers

  • Intro
  • Skipping Tests with pytst.mark.skip
  • Skipping Tests Conditionally with pytst.mark.skipif
  • Expecting Tests to Fail with pytest.mark.xfail
  • Bad Reasons to Use xfail
  • Selecting Tests with Custom Markers
  • Marking Files, Classes, and Parameters
  • Running marked tests with -m
  • Using and, or, not, and Parentheses with Markers and -m
  • Being Strict with Markers
  • Listing Markers with --markers
  • Combining Markers with Fixtures

Part 2: Working with Projects

  • Part 2: Working with Projcts

Ch 7: Test Strategy

  • Intro
  • Determining Test Scope
  • Considering Software Architecture
  • Evaluating the Features to Test
  • Creating Test Cases
  • Writing a Test Strategy
  • Cards Example: Scope, Architecture, and Prioritizing Features
  • Cards Example: Creating Test Cases
  • Cards Example: Writing a Test Strategy
  • Cards Example: From Test Cases to Test Code
  • Cards Example: Running the Tests

Ch 8: Configuration Files

  • Intro
  • Determining a Root Directory
  • Saving Settings and Flags in pytest.ini
  • Using tox.ini, pyproject.toml, or setup.cfg in place of pytest.ini
  • Avoiding Test File Name Collision with __init__.py

Ch 9: Coverage

  • Intro
  • Measuring Code Coverage with `pytest-cov`
  • Measuring Code Coverage with `coverage` Directly
  • Specifying Multiple Equivalent Source Paths in `.coveragerc`
  • Generating HTML Reports
  • Excluding Code from Coverage
  • Adding the Missing Test Cases
  • Beware of Coverage-Driven Development
  • Running Coverage on Tests
  • Running Coverage on a Directory
  • Running Coverage on a Single File Script

Ch 10: Mocking

  • Intro
  • What Are We Mocking? Isolating the Cards CLI
  • Testing with Typer + shlex + cards_cli
  • Mocking an Attribute
  • Mocking a Class and Methods
  • Using a Fixture for Mocking
  • Using autospec to Keep Mocks and API Synchronized
  • Making Sure Functions Are Called Correctly
  • Creating Error Conditions
  • Running Coverage + Coverage Exclusion
  • Read the Docs
  • Mocks Don't Test Behavior
  • Testing at Multiple Layers to Avoid Mocking
  • Using Plugins to Assist Mocking

Ch 11: tox and Continuous Integration

  • Intro
  • Setting Up tox
  • Running tox
  • Testing Multiple Python Versions
  • Running tox Environments in Parallel
  • Adding a Coverage Report to tox
  • Specifying a Minimum Coverage Level
  • Passing pytest Parameters Through tox
  • Combining pytest.ini and .coveragerc into tox.ini
  • CI: Running tox with GitHub Actions

Ch 12: Testing Scripts and Applications

  • Intro
  • Testing a Python Script
  • Testing an Importable Python Script
  • Separating Code into src and tests Directories
  • Testing a requirements.txt-based Application

Ch 13: Debugging Test Failures

  • Intro
  • Setup
  • Writing Tests First
  • First Draft Implementation
  • Debugging with pytest Flags
  • Debugging with pdb
  • Combining tox and pdb
  • Refactoring
  • Note on Code Sample

Part 3: Booster Rockets

  • Part 3: Booster Rockets

Ch 14: Third-Party Plugins

  • Exploring the Diversity of pytest Plugins
  • Finding Plugins
  • pytest-repeat: Repeating Tests
  • pytest-xdist: Running Tests in Parallel
  • pytest-randomly: Randomizing Test Order

Ch 15: Building Plugins

  • Intro
  • Building a Local conftest Plugin
  • Creating an Installable Plugin
  • Testing Plugins with pytester
  • Testing Multiple Python Versions with tox
  • Using a src Project Layout
  • Publishing Plugins and Further Information

Ch 16: Advanced Parametrization

  • Intro
  • Creating Custom Identifiers
  • Parametrizing with Dynamic Values
  • Using Multiple Parameters
  • Using Indirect Parametrization
  • Selecting a Subset of Fixture Parameters
  • Creating an Optional Indirect Fixture