You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: learn-pr/language/test-python-with-pytest/2-pytest-basics.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Pytest basics
4
4
metadata:
5
5
title: Pytest basics
6
6
description: Find about the basics on testing conventions in Python and Pytest. These conventions will help you organize your tests and get them discovered.
Copy file name to clipboardExpand all lines: learn-pr/language/test-python-with-pytest/4-test-classes-and-methods.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Test classes and methods
4
4
metadata:
5
5
title: Test classes and methods
6
6
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.
Copy file name to clipboardExpand all lines: learn-pr/language/test-python-with-pytest/7-summary.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Summary
4
4
metadata:
5
5
title: Summary
6
6
description: In this module, we went through the basics of testing with Pytest. We covered writing and organizing tests and some examples running Pytest.
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.
2
2
3
3
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.
4
4
5
5
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.
6
6
7
7
Here, we cover some of the basics needed to get started with Pytest so you can take your test suite to the next level.
8
8
9
-
## What you will learn
9
+
## What you'll learn
10
10
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.
12
12
13
13
This knowledge allows you to:
14
14
@@ -18,4 +18,4 @@ This knowledge allows you to:
18
18
19
19
## What is the main goal?
20
20
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.
Copy file name to clipboardExpand all lines: learn-pr/language/test-python-with-pytest/includes/2-pytest-basics.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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.
2
2
3
3
By the end of this section you should have everything you need to start writing your first tests and run them with Pytest.
4
4
5
5
## Conventions
6
6
7
7
Before diving into writing tests, we must cover some of the testing conventions that Pytest relies on.
8
8
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.
10
10
11
11
### Tests directory and test files
12
12
13
13
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.
14
14
15
15
> [!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.
17
17
18
18
Let's see how the root of a small Python project named `jformat` looks:
19
19
@@ -38,7 +38,7 @@ The *tests* directory is at the root of the project with a single test file. In
38
38
39
39
### Test functions
40
40
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.
42
42
43
43
Here's what a simple test function looks like:
44
44
@@ -48,7 +48,7 @@ def test_main():
48
48
```
49
49
50
50
> [!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.
52
52
53
53
### Test classes and test methods
54
54
@@ -70,15 +70,15 @@ class TestUser:
70
70
71
71
## Run tests
72
72
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:
74
74
75
75
- Perform the collection of tests by finding all test files, test classes, and test functions for a test run.
76
76
- 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.
78
78
- Provide rich reporting at the end of a test run.
79
79
80
80
> [!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.
82
82
83
83
Given these contents in a *test_main.py* file, we can see how Pytest behaves running the tests:
84
84
@@ -107,9 +107,9 @@ Behind the scenes, Pytest collects the example test in the test file without any
107
107
108
108
### The powerful assert statement
109
109
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.
111
111
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.
113
113
114
114
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:
115
115
@@ -130,7 +130,7 @@ E ? ^ +
130
130
test_main.py:4: AssertionError
131
131
```
132
132
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:
@@ -193,7 +193,7 @@ E ...Full output truncated (12 lines hidden), use '-vv' to show
193
193
194
194
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.
195
195
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:
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.
Copy file name to clipboardExpand all lines: learn-pr/language/test-python-with-pytest/includes/4-test-classes-and-methods.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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.
2
2
3
3
Just like functions, you can still write assertions using the `assert` statement.
4
4
@@ -39,7 +39,7 @@ class TestIsDone:
39
39
```
40
40
41
41
> [!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.
43
43
44
44
Running the tests with the `-v` flag to increase verbosity shows the tests passing:
45
45
@@ -61,12 +61,12 @@ Although the tests are passing, they look repetitive and they're also leaving fi
61
61
62
62
## Helper methods
63
63
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.
65
65
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.
70
70
71
71
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.
72
72
@@ -122,7 +122,7 @@ class TestIsDone:
122
122
123
123
### Custom helper methods
124
124
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:
0 commit comments