失效链接处理 |
Python Testing with pytest PDF 下载
本站整理下载:
相关截图:
![]()
主要内容:
Chapter 1
Getting Started with pytest
This is a test:
ch1/test_one.py
def test_passing():
assert (1, 2, 3) == (1, 2, 3)
This is what it looks like when it’s run:
$ cd /path/to/code/ch1
$ pytest test_one.py
===================== test session starts ======================
collected 1 items
test_one.py .
=================== 1 passed in 0.01 seconds ===================
The dot after test_one.py means that one test was run and it passed. If you need more
information, you can use -v or --verbose:
$ pytest -v test_one.py
===================== test session starts ======================
collected 1 items
test_one.py::test_passing PASSED
=================== 1 passed in 0.01 seconds ===================
If you have a color terminal, the PASSED and bottom line are green. It’s nice.
This is a failing test:
ch1/test_two.py
def test_failing():
assert (1, 2, 3) == (3, 2, 1)
The way pytest shows you test failures is one of the many reasons developers love pytest. Let’s
watch this fail:
$ pytest test_two.py
(1)
===================== test session starts ======================
collected 1 items
test_two.py F
=========================== FAILURES ===========================
_________________________ test_failing _________________________
def test_failing():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Use -v to get the full diff
test_two.py:2: AssertionError
=================== 1 failed in 0.04 seconds ===================
Cool. The failing test, test_failing, gets its own section to show us why it failed. And pytest tells
us exactly what the first failure is: index 0 is a mismatch. Much of this is in red to make it really
stand out (if you’ve got a color terminal). That’s already a lot of information, but there’s a line
that says Use -v to get the full diff. Let’s do that:
$ pytest -v test_two.py
===================== test session starts ======================
collected 1 items
test_two.py::test_failing FAILED
=========================== FAILURES ===========================
_________________________ test_failing _________________________
def test_failing():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Full diff:
E - (1, 2, 3)
E ? ^ ^
E + (3, 2, 1)
E ? ^ ^
test_two.py:2: AssertionError
=================== 1 failed in 0.04 seconds ===================
(2)
Wow. pytest adds little carets (^) to show us exactly what’s different.
If you’re already impressed with how easy it is to write, read, and run tests with pytest, and how
easy it is to read the output to see where the tests fail, well, you ain’t seen nothing yet. There’s
lots more where that came from. Stick around and let me show you why I think pytest is the
absolute best test framework available.
In the rest of this chapter, you’ll install pytest, look at different ways to run it, and run through
some of the most often used command-line options. In future chapters, you’ll learn how to write
test functions that maximize the power of pytest, how to pull setup code into setup and teardown
sections called fixtures, and how to use fixtures and plugins to really supercharge your software
testing.
But first, I have an apology. I’m sorry that the test, assert (1, 2, 3) == (3, 2, 1), is so boring.
Snore. No one would write a test like that in real life. Software tests are comprised of code that
tests other software that you aren’t always positive will work. And (1, 2, 3) == (1, 2, 3) will
always work. That’s why we won’t use overly silly tests like this in the rest of the book. We’ll
look at tests for a real software project. We’ll use an example project called Tasks that needs
some test code. Hopefully it’s simple enough to be easy to understand, but not so simple as to be
boring.
Another great use of software tests is to test your assumptions about how the software under test
works, which can include testing your understanding of third-party modules and packages, and
even builtin Python data structures. The Tasks project uses a structure called Task, which is
based on the namedtuple factory method, which is part of the standard library. The Task structure
is used as a data structure to pass information between the UI and the API. For the rest of this
chapter, I’ll use Task to demonstrate running pytest and using some frequently used command-
line options.
|