Manuals / Python / Chapter 7

C · Testing · intermediate · Week 3 · Chapter 7 of 13

pytest fundamentals

Arrange-act-assert. test_ prefix. assert directly. pytest discovers tests/ automatically.

Path progress
49%

Step 1 of 3

First tests

test_is_valid_email_true(), test_is_valid_email_false(). No unittest boilerplate.

First testsDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →First testsdrag · tap →Try thisdrag · tap →Follow the dashed drag · tap →

Example

# tests/test_helpers.py
from src.helpers import is_valid_email

def test_valid_email():
    assert is_valid_email("a@b.com") is True

def test_invalid_email():
    assert is_valid_email("nope") is False

Do this now

5 pytest tests for helpers from earlier chapters. pytest -v.

Was this step clear?
Chapter learning outcomes
  • pytest discovery
  • assert style
  • Running pytest
  • Test naming

Clear these before you leave

Side quest

Config tests

Test load_config with missing file raises or returns default.