Skip to content

Commit 2bcd686

Browse files
authored
Merge pull request #50680 from ShawnKupfer/WB1803
AB#1055059: Testing with Pytest
2 parents 1f5ef3b + e1b8790 commit 2bcd686

File tree

11 files changed

+47
-48
lines changed

11 files changed

+47
-48
lines changed

learn-pr/language/test-python-with-pytest/1-introduction.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ uid: learn.test-python-with-pytest.introduction
33
title: Introduction
44
metadata:
55
title: Introduction to testing with Pytest
6-
description: This module will go through the basics of Pytest, a powerful testing tool for Python. You'll learn writing and running tests, and reporting.
7-
ms.date: 12/08/2023
6+
description: This module will go through the basics of Pytest, a powerful testing tool for Python. You'll learn to write and run tests, as well as reporting.
7+
ms.date: 05/27/2025
88
author: alfredodeza
99
ms.author: alfredodeza
1010
ms.custom:

learn-pr/language/test-python-with-pytest/2-pytest-basics.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Pytest basics
44
metadata:
55
title: Pytest basics
66
description: Find about the basics on testing conventions in Python and Pytest. These conventions will help you organize your tests and get them discovered.
7-
ms.date: 12/08/2023
7+
ms.date: 05/27/2025
88
author: alfredodeza
99
ms.author: alfredodeza
1010
ms.custom:

learn-pr/language/test-python-with-pytest/4-test-classes-and-methods.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Test classes and methods
44
metadata:
55
title: Test classes and methods
66
description: Aside from test functions, you can use test classes and methods. Test classes come with helpers and capabilities that will help you write better tests.
7-
ms.date: 12/08/2023
7+
ms.date: 05/27/2025
88
author: alfredodeza
99
ms.author: alfredodeza
1010
ms.custom:

learn-pr/language/test-python-with-pytest/5-exercise.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Exercise
44
metadata:
55
title: Exercise - Write test classes and method for Pytest
66
description: Practice writing test classes and methods for tests with Pytest
7-
ms.date: 12/08/2023
7+
ms.date: 05/27/2025
88
author: alfredodeza
99
ms.author: alfredodeza
1010
ms.custom:

learn-pr/language/test-python-with-pytest/6-knowledge-check.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Module assessment
44
metadata:
55
title: Module assessment
66
description: Check your knowledge about testing functions and classes with Pytest
7-
ms.date: 12/08/2023
7+
ms.date: 05/27/2025
88
author: alfredodeza
99
ms.author: alfredodeza
1010
ms.custom:
@@ -19,14 +19,14 @@ quiz:
1919
choices:
2020
- content: "It makes tests perform faster."
2121
isCorrect: false
22-
explanation: "Incorrect. Tests dont perform faster when using plain assert statements."
22+
explanation: "Incorrect. Tests don't perform faster when using plain assert statements."
2323
- content: "It allows you to use Python operators for any comparison."
2424
isCorrect: true
2525
explanation: "Correct. By using the plain assert statement, you can take advantage of using Python operators for all comparisons."
2626
- content: "It allows tests to be executed in parallel."
2727
isCorrect: false
2828
explanation: "Incorrect. Using plain assert statements aren't required for parallel testing."
29-
- content: "What is one reason to group tests in a test class?"
29+
- content: "What's one reason to group tests in a test class?"
3030
choices:
3131
- content: "So that tests can benefit from a common setup or cleanup."
3232
isCorrect: true

learn-pr/language/test-python-with-pytest/7-summary.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Summary
44
metadata:
55
title: Summary
66
description: In this module, we went through the basics of testing with Pytest. We covered writing and organizing tests and some examples running Pytest.
7-
ms.date: 12/08/2023
7+
ms.date: 05/27/2025
88
author: alfredodeza
99
ms.author: alfredodeza
1010
ms.custom:
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
Getting started with testing in Python can be overwhelming. Python's standard library does offer some utilities and helpers to write tests, but has some drawbacks that might make it difficult.
1+
Getting started with testing in Python can be overwhelming. Python's standard library offers some utilities and helpers to write tests, but has some drawbacks that might make it difficult.
22

33
Pytest is one of the most popular testing tools and frameworks for Python. Although Pytest can help with highly complex testing scenarios, it doesn't force its capabilities when creating tests. You can write simple tests and still benefit from the fast and featureful test runner and useful reporting.
44

55
A crucial aspect of Pytest is that it makes writing tests easier. You can write a test function with no dependencies or configuration and run the test right away.
66

77
Here, we cover some of the basics needed to get started with Pytest so you can take your test suite to the next level.
88

9-
## What you will learn
9+
## What you'll learn
1010

11-
By the end of this module, you're able to write tests with Pytest, interpret its rich failure reporting, and take advantage of its featureful test runner. You should feel comfortable working with both test functions and classes, and be capable of determining when to use either.
11+
By the end of this module, you'll be able to write tests with Pytest, interpret its rich failure reporting, and take advantage of its featureful test runner. You should feel comfortable working with both test functions and classes, and be capable of determining when to use either.
1212

1313
This knowledge allows you to:
1414

@@ -18,4 +18,4 @@ This knowledge allows you to:
1818

1919
## What is the main goal?
2020

21-
You should feel comfortable working with Pytest and writing tests for Pytest, allowing you to become a more efficient engineer by helping you write more and better tests.
21+
You should feel comfortable working with Pytest and writing tests for Pytest, which allows you to become a more efficient engineer by helping you write more and better tests.

learn-pr/language/test-python-with-pytest/includes/2-pytest-basics.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
Let's get started testing with Pytest. As we mentioned in the previous unit, Pytest is highly configurable and can handle complex test suites. But it doesn't require much to start writing tests. In fact, the easier a framework allows you to write tests, the better.
1+
Let's get started testing with Pytest. As we mentioned in the previous unit, Pytest is highly configurable and can handle complex test suites, but it doesn't require much to start writing tests. In fact, the easier a framework allows you to write tests, the better.
22

33
By the end of this section you should have everything you need to start writing your first tests and run them with Pytest.
44

55
## Conventions
66

77
Before diving into writing tests, we must cover some of the testing conventions that Pytest relies on.
88

9-
There aren't hard rules about test files, test directories, or general testing layouts in Python. By knowing these rules, you can take advantage of automatic test discovery and execution without the need for any extra configuration.
9+
There aren't many hard rules about test files, test directories, or general testing layouts in Python. By knowing these rules, you can take advantage of automatic test discovery and execution without the need for any extra configuration.
1010

1111
### Tests directory and test files
1212

1313
The main directory for tests is the *tests* directory. You can place this directory at the root level of the project, but it's also not unusual to see it alongside code modules.
1414

1515
> [!NOTE]
16-
> In this module we'll default to using *tests* at the root of a project.
16+
> In this module, we'll default to using *tests* at the root of a project.
1717
1818
Let's see how the root of a small Python project named `jformat` looks:
1919

@@ -38,7 +38,7 @@ The *tests* directory is at the root of the project with a single test file. In
3838
3939
### Test functions
4040

41-
One of the strong arguments for using Pytest is that it allows you to write test functions. Similar to test files, test functions must be prefixed with `test_`. The `test_` prefix ensures that Pytest collects the test and executes it.
41+
A strong argument for using Pytest is that it allows you to write test functions. Similar to test files, test functions must be prefixed with `test_`. The `test_` prefix ensures that Pytest collects the test and executes it.
4242

4343
Here's what a simple test function looks like:
4444

@@ -48,7 +48,7 @@ def test_main():
4848
```
4949

5050
> [!NOTE]
51-
> If you are familiar with `unittest`, it might be surprising to see the use of `assert` in the test function. We cover plain asserts in more detail later, but with Pytest, you get rich failure reporting with plain asserts.
51+
> If you're familiar with `unittest`, it might be surprising to see the use of `assert` in the test function. We cover plain asserts in more detail later, but with Pytest, you get rich failure reporting with plain asserts.
5252
5353
### Test classes and test methods
5454

@@ -70,15 +70,15 @@ class TestUser:
7070

7171
## Run tests
7272

73-
Pytest is both a test framework and a test runner. The test runner is an executable in the command line that at a high level can:
73+
Pytest is both a test framework and a test runner. The test runner is an executable in the command line that (at a high level) can:
7474

7575
- Perform the collection of tests by finding all test files, test classes, and test functions for a test run.
7676
- Initiate a test run by executing all tests.
77-
- Keep track of failures, errors, and passing tests.
77+
- Keep track of failures, errors, and passing tests.
7878
- Provide rich reporting at the end of a test run.
7979

8080
> [!NOTE]
81-
> Since Pytest is an external library it *must* be installed in order to use it.
81+
> Because Pytest is an external library, it *must* be installed in order to use it.
8282
8383
Given these contents in a *test_main.py* file, we can see how Pytest behaves running the tests:
8484

@@ -107,9 +107,9 @@ Behind the scenes, Pytest collects the example test in the test file without any
107107

108108
### The powerful assert statement
109109

110-
So far, our test examples are all using the plain `assert` call. Usually, in Python, the `assert` statement isn't used for tests because it lacks proper reporting when the assertion fails. Pytest, however, doesn't have this limitation. Behind the scenes, Pytest is enabling the statement to perform rich comparisons without forcing the user to write more code or configure anything.
110+
So far, our test examples are all using the plain `assert` call. Usually, in Python, the `assert` statement isn't used for tests, because it lacks proper reporting when the assertion fails. Pytest, however, doesn't have this limitation. Behind the scenes, Pytest enables the statement to perform rich comparisons without forcing the user to write more code or configure anything.
111111

112-
By using the plain `assert` statement, you can make use of Python's operators. For example: `>`, `<`, `!=`, `>=`, or `<=`. All of Python's operators are valid. This capability might be the single most crucial feature of Pytest: you aren't required to learn new syntax to write assertions.
112+
By using the plain `assert` statement, you can make use of Python's operators; for example, `>`, `<`, `!=`, `>=`, or `<=`. All of Python's operators are valid. This capability might be the single most crucial feature of Pytest: you aren't required to learn new syntax to write assertions.
113113

114114
Let's see how that translates when dealing with common comparisons with Python objects. In this case, let's go through the failure report when comparing long strings:
115115

@@ -130,7 +130,7 @@ E ? ^ +
130130
test_main.py:4: AssertionError
131131
```
132132

133-
Pytest shows useful context around the failure. An incorrect casing at the beginning of the string, and an extra character in a word. But beyond strings, Pytest can help with other objects and data structures. For example, here's how it behaves with lists:
133+
Pytest shows useful context around the failure: an incorrect casing at the beginning of the string and an extra character in a word. But beyond strings, Pytest can help with other objects and data structures. For example, here's how it behaves with lists:
134134

135135
```text
136136
________________________________ test_lists ________________________________
@@ -193,7 +193,7 @@ E ...Full output truncated (12 lines hidden), use '-vv' to show
193193

194194
In this test, there are two failures in the dictionary. One is that the `"street"` value is different, and the other one is that `"number"` doesn't match.
195195

196-
Pytest is accurately detecting these differences (even though it's one failure in a single test). Since the dictionaries contain many items, Pytest omits the identical parts and only shows relevant content. Let's see what happens if we make use of the suggested `-vv` flag to increase the verbosity in the output:
196+
Pytest accurately detects these differences (even though it's one failure in a single test). Because the dictionaries contain many items, Pytest omits the identical parts and only shows relevant content. Let's see what happens if we make use of the suggested `-vv` flag to increase the verbosity in the output:
197197

198198
```text
199199
____________________________ test_dictionaries _____________________________
@@ -224,4 +224,4 @@ E 'zipcode': 30877,
224224
E }
225225
```
226226

227-
By running `pytest -vv`, the reporting increases the amount of detail and provides a granular comparison. Not only does this report detect and show the failure, but it allows you to quickly make changes to remediate the problem.
227+
By running `pytest -vv`, the reporting increases the amount of detail and provides a granular comparison. Not only does this report detect and show the failure, but it allows you to quickly make changes to remediate the problem.

learn-pr/language/test-python-with-pytest/includes/4-test-classes-and-methods.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
In addition to writing test functions, Pytest allows you to use classes. As we already mentioned, there's no need for inheritance and the test classes follow a few simple rules. Using classes gives you more flexibility and reusability. As you see next, Pytest keeps out of the way and avoids forcing you to write tests in a certain way.
1+
In addition to writing test functions, Pytest allows you to use classes. As we already mentioned, there's no need for inheritance, and the test classes follow a few simple rules. Using classes gives you more flexibility and reusability. As you see next, Pytest keeps out of the way and avoids forcing you to write tests in a certain way.
22

33
Just like functions, you can still write assertions using the `assert` statement.
44

@@ -39,7 +39,7 @@ class TestIsDone:
3939
```
4040

4141
> [!CAUTION]
42-
> The test methods are using the */tmp* path for a temporary test file because it's easier to use for the example. However, if you need to use temporary files, consider using a library like `tempfile` that can create (and remove) them safely for you. Not every system has a */tmp* directory and that location might not be temporary depending on the operating system.
42+
> The test methods use the */tmp* path for a temporary test file because it's easier to use for the example. However, if you need to use temporary files, consider using a library like `tempfile` that can create (and remove) them safely for you. Not every system has a */tmp* directory, and that location might not be temporary depending on the operating system.
4343
4444
Running the tests with the `-v` flag to increase verbosity shows the tests passing:
4545

@@ -61,12 +61,12 @@ Although the tests are passing, they look repetitive and they're also leaving fi
6161

6262
## Helper methods
6363

64-
In a test class, there are a few methods you can use to setup and teardown test execution. Pytest executes them automatically if they're defined. To use these methods, you should know that they have a specific order and behavior.
64+
In a test class, there are a few methods you can use to set up and tear down test execution. Pytest executes them automatically if they're defined. To use these methods, you should know that they have a specific order and behavior.
6565

66-
- `setup`: Executes once before each test in a class
67-
- `teardown`: Executes once after each test in a class
68-
- `setup_class`: Executes once before all tests in a class
69-
- `teardown_class`: Executes once after all tests in a class
66+
- `setup`: Executes once before each test in a class.
67+
- `teardown`: Executes once after each test in a class.
68+
- `setup_class`: Executes once before all tests in a class.
69+
- `teardown_class`: Executes once after all tests in a class.
7070

7171
When tests require similar (or identical) resources to work, it's useful to write setup methods. Ideally, a test shouldn't leave resources when it completes, so teardown methods can help in test cleanup in those situations.
7272

@@ -122,7 +122,7 @@ class TestIsDone:
122122

123123
### Custom helper methods
124124

125-
You can create custom helper methods in a class. These methods must not be prefixed with the name `test` and can't be named as the setup or cleanup methods. In the `TestIsDone` class, we could automate the creation of the temporary file in a custom helper. That custom helper method might look like this example:
125+
You can create custom helper methods in a class. These methods must not be prefixed with the name `test` and can't be named as the setup or cleanup methods. In the `TestIsDone` class, we could automate creating the temporary file in a custom helper. That custom helper method might look like this example:
126126

127127
```python
128128
def write_tmp_file(self, content):

0 commit comments

Comments
 (0)