@@ -79,10 +79,10 @@ Inside those files, test functions usually start with `test_`.
7979Example:
8080
8181``` python
82- def test_count_words_returns_zero_for_empty_text () -> None :
83- result = count_words( " " )
82+ def test_count_characters_includes_whitespace_by_default () -> None :
83+ result = count_characters( " hello world " )
8484
85- assert result == 0
85+ assert result == 11
8686```
8787
8888The function name describes the expected behavior.
@@ -108,15 +108,48 @@ This means:
108108Example:
109109
110110``` python
111- def test_normalize_whitespace_replaces_repeated_spaces () -> None :
112- text = " Python is fun "
111+ def test_count_characters_can_ignore_whitespace () -> None :
112+ result = count_characters( " hello world " , include_whitespace = False )
113113
114+ assert result == 10
115+ ```
116+
117+ This style makes tests easier to read and review.
118+
119+ ## Parametrizing related cases
120+
121+ Sometimes several inputs should follow the same behavior. Copying the entire
122+ test for each input would make the test file repetitive.
123+
124+ pytest can run one test function with several sets of values:
125+
126+ ``` python
127+ import pytest
128+
129+
130+ @pytest.mark.parametrize (
131+ (" text" , " expected" ),
132+ [
133+ (" Python is fun" , " Python is fun" ),
134+ (" Python\t is\n fun" , " Python is fun" ),
135+ (" \t\n " , " " ),
136+ (" Python project " , " Python project" ),
137+ ],
138+ )
139+ def test_normalize_whitespace_handles_common_inputs (text : str , expected : str ) -> None :
114140 result = normalize_whitespace(text)
115141
116- assert result == " Python is fun "
142+ assert result == expected
117143```
118144
119- This style makes tests easier to read and review.
145+ The names ` text ` and ` expected ` match the two values in every tuple. pytest
146+ runs the function once for each tuple and reports every case separately.
147+
148+ This example covers repeated spaces, tabs and newlines, whitespace-only text,
149+ and leading or trailing spaces without duplicating the test logic.
150+
151+ Parametrization is useful when the action and assertion stay the same. Use
152+ separate tests when cases describe different behavior or need different setup.
120153
121154## pytest configuration
122155
0 commit comments