Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using Python’s assert to Debug and Test Your Code
Python’s assert statement allows you to write sanity checks in your code. These checks are known as assertions, and you can use them to test if certain assumptions remain true while you’re developing your code. If any of your assertions turn false, then you have a bug in your code.
Assertions are a convenient tool for documenting, debugging, and testing code during development. Once you’ve debugged and tested your code with the help of assertions, then you can turn them off to optimize the code for production. Assertions will help you make your code more efficient, robust, and reliable.
In this tutorial, you’ll learn:
- What assertions are and when to use them
- How Python’s
assertstatement works - How
assertcan help you document, debug, and test your code - How assertions can be disabled to improve performance in production
- What common pitfalls you might face when using
assertstatements
To get the most out of this tutorial, you should have previous knowledge of expressions and operators, functions, conditional statements, and exceptions. Having a basic understanding of documenting, debugging, and testing Python code is also a plus.
Getting to Know Assertions in Python
Python implements a feature called assertions that’s pretty useful during the development of your applications and projects. You’ll find this feature in several other languages too, such as C and Java, and it comes in handy for documenting, debugging, and testing your code.
If you’re looking for a tool to strengthen your debugging and testing process, then assertions are for you. In this section, you’ll learn the basics of assertions, including what they are, what they’re good for, and when you shouldn’t use them in your code.
What Are Assertions?
In Python, assertions are statements that you can use to set sanity checks during the development process. Assertions allow you to test the correctness of your code by checking if some specific conditions remain true, which can come in handy while you’re debugging code.
The assertion condition should always be true unless you have a bug in your program. If the condition turns out to be false, then the assertion raises an exception and terminates the execution of your program.
With assertions, you can set checks to make sure that invariants within your code stay invariant. By doing so, you can check assumptions like preconditions and postconditions. For example, you can test conditions along the lines of This argument is not None or This return value is a string. These kinds of checks can help you catch errors as soon as possible when you’re developing a program.
What Are Assertions Good For?
Assertions are mainly for debugging. They’ll help you ensure that you don’t introduce new bugs while adding features and fixing other bugs in your code. However, they can have other interesting use cases within your development process. These use cases include documenting and testing your code.
The primary role of assertions is to trigger the alarms when a bug appears in a program. In this context, assertions mean Make sure that this condition remains true. Otherwise, throw an error.
In practice, you can use assertions to check preconditions and postconditions in your programs at development time. For example, programmers often place assertions at the beginning of functions to check if the input is valid (preconditions). Programmers also place assertions before functions’ return values to check if the output is valid (postconditions).
Assertions make it clear that you want to check if a given condition is and remains true. In Python, they can also include an optional message to unambiguously describe the error or problem at hand. That’s why they’re also an efficient tool for documenting code. In this context, their main advantage is their ability to take concrete action instead of being passive, as comments and docstrings are.
Finally, assertions are also ideal for writing test cases in your code. You can write concise and to-the-point test cases because assertions provide a quick way to check if a given condition is met or not, which defines if the test passes or not.
You’ll learn more about these common use cases of assertions later in this tutorial. Now you’ll learn the basics of when you shouldn’t use assertions.
When Not to Use Assertions?
In general, you shouldn’t use assertions for data processing or data validation, because you can disable assertions in your production code, which ends up removing all your assertion-based processing and validation code. Using assertions for data processing and validation is a common pitfall, as you’ll learn in Understanding Common Pitfalls of assert later in this tutorial.
Additionally, assertions aren’t an error-handling tool. The ultimate purpose of assertions isn’t to handle errors in production but to notify you during development so that you can fix them. In this regard, you shouldn’t write code that catches assertion errors using a regular try … except statement.
Understanding Python’s assert Statements
Now you know what assertions are, what they’re good for, and when you shouldn’t use them in your code. It’s time to learn the basics of writing your own assertions. First, note that Python implements assertions as a statement with the assert keyword rather than as a function. This behavior can be a common source of confusion and issues, as you’ll learn later in this tutorial.
In this section, you’ll learn the basics of using the assert statement to introduce assertions in your code. You’ll study the syntax of the assert statement. Most importantly, you’ll understand how this statement works in Python. Finally, you’ll also learn the basics of the AssertionError exception.
The Syntax of the assert Statement
An assert statement consists of the assert keyword, the expression or condition to test, and an optional message. The condition is supposed to always be true. If the assertion condition is true, then nothing happens, and your program continues its normal execution. On the other hand, if the condition becomes false, then assert halts the program by raising an AssertionError.
In Python, assert is a simple statement with the following syntax:
assert expression[, assertion_message]
Here, expression can be any valid Python expression or object, which is then tested for truthiness. If expression is false, then the statement throws an AssertionError. The assertion_message parameter is optional but encouraged. It can hold a string describing the issue that the statement is supposed to catch.
Here’s how this statement works in practice:
>>>
>>> number = 42
>>> assert number > 0
>>> number = -42
>>> assert number > 0
Traceback (most recent call last):
...
AssertionError
With a truthy expression, the assertion succeeds, and nothing happens. In that case, your program continues its normal execution. In contrast, a falsy expression makes the assertion fail, raising an AssertionError and breaking the program’s execution.
To make your assert statements clear to other developers, you should add a descriptive assertion message:
>>>
>>> number = 42
>>> assert number > 0, f"number greater than 0 expected, got: {number}"
>>> number = -42
>>> assert number > 0, f"number greater than 0 expected, got: {number}"
Traceback (most recent call last):
...
AssertionError: number greater than 0 expected, got: -42
The message in this assertion clearly states which condition should be true and what is making that condition fail. Note that the assertion_message argument to assert is optional. However, it can help you better understand the condition under test and figure out the problem that you’re facing.
So, whenever you use assert, it’s a good idea to use a descriptive assertion message for the traceback of the AssertionError exception.
An important point regarding the assert syntax is that this statement doesn’t require a pair of parentheses to group the expression and the optional message. In Python, assert is a statement instead of a function. Using a pair of parentheses can lead to unexpected behaviors.
For example, an assertion like the following will raise a SyntaxWarning:
>>>
>>> number = 42
>>> assert(number > 0, f"number greater than 0 expected, got: {number}")
<stdin>:1: SyntaxWarning: assertion is always true, perhaps remove parentheses?
This warning has to do with non-empty tuples always being truthy in Python. In this example, the parentheses turn the assertion expression and message into a two-item tuple, which always evaluates to true.
Fortunately, recent versions of Python throw a SyntaxWarning to alert you of this misleading syntax. However, in older versions of the language, an assert statement like the one above will always succeed.
This issue often appears when you’re using long expressions or messages that take more than a single line. In these cases, the parentheses are the natural way to format the code, and you may end up with something like the following:
number = 42
assert (
number > 0 and isinstance(number, int),
f"number greater than 0 expected, got: {number}"
)
Using a pair of parentheses to split a long line into multiple lines is a common formatting practice in Python code. However, in the context of an assert statement, the parentheses turn the assertion expression and message into a two-item tuple.
In practice, if you want to split a long assertion into several lines, then you can use the backslash character () for explicit line joining:
number = 42
assert number > 0 and isinstance(number, int),
f"number greater than 0 expected, got: {number}"
The backslash at the end of first line of this assertion joins the assertion’s two physical lines into a single logical line. This way, you can have appropriate line length without the risk of a warning or a logical error in your code.
There’s an edge case of this parentheses-related issue. If you provide only the assertion expression in parentheses, then assert will work just fine:
>>>
>>> number = 42
>>> assert(number > 0)
>>> number = -42
>>> assert(number > 0)
Traceback (most recent call last):
...
AssertionError
Why is this happening? To create a single-item tuple, you need to place a comma after the item itself. In the code above, the parentheses by themselves don’t create a tuple. That’s why the interpreter ignores the parentheses, and assert works as expected.
Even though the parentheses seem to work in the scenario described in the above example, it’s not a recommended practice. You can end up shooting yourself in the foot.
The AssertionError Exception
If the condition of an assert statement evaluates to false, then assert raises an AssertionError. If you provide the optional assertion message, then this message is internally used as an argument to the AssertionError class. Either way, the raised exception breaks your program’s execution.
Most of the time, you won’t raise AssertionError exceptions explicitly in your code. The assert statement takes care of raising this exception when the assertion condition fails. Additionally, you shouldn’t attempt to handle errors by writing code that catches the AssertionError exception, as you’ll learn later in this tutorial.
Finally, AssertionError is a built-in exception that inherits from the Exception class and is considered a concrete exception that should be raised instead of subclassed.
That’s it! Now you know the basics of the assert statement. You’ve learned the statement’s syntax, how assert works in practice, and also what the main characteristics of the AssertionError exception are. It’s time to move forward and explore some effective and common ways to write assertions in Python.
Exploring Common Assertion Formats
When it comes to writing the assert statement, you’ll find several assertion formats that are common in Python code. Being aware of these formats will allow you to write better assertions.
The following examples showcase a few of these common assertion formats, starting with assertions that compare objects:
>>>
>>> # Comparison assertions
>>> assert 3 > 2
>>> assert 3 == 2
Traceback (most recent call last):
...
AssertionError
>>> assert 3 > 2 and 5 < 10
>>> assert 3 == 2 or 5 > 10
Traceback (most recent call last):
...
AssertionError
Comparison assertions are intended to test conditions that compare two or more objects using comparison operators. These assertions can also include compound expressions based on Boolean operators.
Another common assertion format is related to membership tests:
>>>
>>> # Membership assertions
>>> numbers = [1, 2, 3, 4, 5]
>>> assert 4 in numbers
>>> assert 10 in numbers
Traceback (most recent call last):
...
AssertionError
Membership assertions allow you to check if a given item is present in a specific collection, such as a list, tuple, set, dictionary, or the like. These assertions use the membership operators, in and not in, to perform the required check.
The assertion format in the example below is related to an object’s identity:
>>>
>>> # Identity assertions
>>> x = 1
>>> y = x
>>> null = None
>>> assert x is y
>>> assert x is not y
Traceback (most recent call last):
...
AssertionError
>>> assert null is None
>>> assert null is not None
Traceback (most recent call last):
...
AssertionError
Identity assertions provide a way to test for an object’s identity. In this case, the assertion expression uses the identity operators, is and is not.
Finally, you’ll learn how to check the data type of your objects in the context of an assertion:
>>>
>>> # Type check assertions
>>> number = 42
>>> assert isinstance(number, int)
>>> number = 42.0
>>> assert isinstance(number, int)
Traceback (most recent call last):
...
AssertionError
Type check assertions commonly involve using the built-in isinstance() function to make sure that a given object is an instance of a certain class or classes.
Even though these are some of the most common assertion formats that you’ll find in Python code, there are many other possibilities. For example, you can use the built-in all() and any() functions to write assertions that check for the truth value of items in an iterable:
>>>
>>> assert all([True, True, True])
>>> assert all([True, False, True])
Traceback (most recent call last):
...
AssertionError
>>> assert any([False, True, False])
>>> assert any([False, False, False])
Traceback (most recent call last):
...
AssertionError
The all() assertions check if all the items in an input iterable are truthy, while the any() examples check if any item in the input iterable is truthy.
Your imagination is the only limit for writing useful assertions. You can write assertions using predicate or Boolean-valued functions, regular Python objects, comparison expressions, Boolean expressions, or general Python expressions. Your assertion will depend on what specific condition you need to check at a given moment.
Now you know some of the most common assertion formats that you can use in your code. It’s time to learn about specific use cases of assertions. In the following section, you’ll learn how to use assertions to document, debug, and test your code.
Documenting Your Code With Assertions
The assert statement is an effective way to document code. For example, if you want to state that a specific condition should always be true in your code, then assert condition can be better and more effective than a comment or a docstring, as you’ll learn in a moment.
To understand why assertions can be a handy documenting tool, say that you have a function that takes a server name and a tuple of port numbers. The function will iterate over the port numbers trying to connect to the target server. For your function to work correctly, the tuple of ports shouldn’t be empty:
def get_response(server, ports=(443, 80)):
# The ports argument expects a non-empty tuple
for port in ports:
if server.connect(port):
return server.get()
return None
If someone accidentally calls get_response() with an empty tuple, then the for loop will never run, and the function will return None even if the server is available. To alert programmers to this buggy call, you can use a comment, like you did in the example above. However, using an assert statement can be more effective:
def get_response(server, ports=(443, 80)):
assert len(ports) > 0, f"ports expected a non-empty tuple, got {ports}"
for port in ports:
if server.connect(port):
return server.get()
return None
The advantage of an assert statement over a comment is that when the condition isn’t true, assert immediately raises an AssertionError. After that, your code stops running, so it avoids abnormal behaviors and points you directly to the specific problem.
So, using assertions in situations like the one described above is an effective and powerful way to document your intentions and avoid hard-to-find bugs due to accidental errors or malicious actors.
Debugging Your Code With Assertions
At its core, the assert statement is a debugging aid for testing conditions that should remain true during your code’s normal execution. For assertions to work as a debugging tool, you should write them so that a failure indicates a bug in your code.
In this section, you’ll learn how to use the assert statement to assist you while debugging your code at development time.
An Example of Debugging With Assertions
You’ll typically use assertions to debug your code during development. The idea is to make sure that specific conditions are and remain true. If an asserted condition becomes false, then you immediately know that you have a bug.
As an example, say that you have the following Circle class:
# circle.py
import math
class Circle:
def __init__(self, radius):
if radius < 0:
raise ValueError("positive radius expected")
self.radius = radius
def area(self):
assert self.radius >= 0, "positive radius expected"
return math.pi * self.radius ** 2
The class’s initializer, .__init__(), takes radius as an argument and makes sure that the input value is a positive number. This check prevents circles with a negative radius.
The .area() method computes the circle’s area. However, before doing that, the method uses an assert statement to guarantee that .radius remains a positive number. Why would you add this check? Well, suppose that you’re working on a team, and one of your coworkers needs to add the following method to Circle:
class Circle:
# ...
def correct_radius(self, correction_coefficient):
self.radius *= correction_coefficient
This method takes a correction coefficient and applies it to the current value of .radius. However, the method doesn’t validate the coefficient, introducing a subtle bug. Can you spot it? Say that the user provides a negative correction coefficient by accident:
>>>
>>> from circle import Circle
>>> tire = Circle(42)
>>> tire.area()
5541.769440932395
>>> tire.correct_radius(-1.02)
>>> tire.radius
-42.84
>>> tire.area()
Traceback (most recent call last):
...
AssertionError: positive radius expected
The first call to .area() works correctly because the initial radius is positive. But the second call to .area() breaks your code with an AssertionError. Why? This happens because the call to .correct_radius() turns the radius into a negative number, which uncovers a bug: the function doesn’t properly check for valid input.
In this example, your assert statement works as a watchdog for situations in which the radius could take invalid values. The AssertionError immediately points you to the specific problem: .radius has unexpectedly changed to a negative number. You have to figure out how this unexpected change happened and then fix your code before it goes into production.
A Few Considerations on Debugging With Assertions
Developers often use assert statements to state preconditions, just like you did in the above example, where .area() checks for a valid .radius right before doing any computation. Developers also use assertions to state postconditions. For example, you can check if a function’s return value is valid, right before returning the value to the caller.
In general, the conditions that you check with an assert statement should be true, unless you or another developer in your team introduces a bug in the code. In other words, these conditions should never be false. Their purpose is to quickly flag if someone introduces a bug. In this regard, assertions are early alerts in your code. These alerts are meant to be useful during development.
If one of these conditions fails, then the program will crash with an AssertionError, telling you exactly which condition isn’t succeeding. This behavior will help you track down and fix bugs more quickly.
To properly use assertions as a debugging tool, you shouldn’t use try … except blocks that catch and handle AssertionError exceptions. If an assertion fails, then your program should crash because a condition that was supposed to be true became false. You shouldn’t change this intended behavior by catching the exception with a try … except block.
A proper use of assertions is to inform developers about unrecoverable errors in a program. Assertions shouldn’t signal an expected error, like a FileNotFoundError, where a user can take a corrective action and try again.
The goal of assertion should be to uncover programmers’ errors rather than users’ errors. Assertions are useful during the development process, not during production. By the time you release your code, it should be (mostly) free of bugs and shouldn’t require the assertions to work correctly.
Finally, once your code is ready for production, you don’t have to explicitly remove assertions. You can just disable them, as you’ll learn in the following section.
Disabling Assertions in Production for Performance
Now say that you’ve come to the end of your development cycle. Your code has been extensively reviewed and tested. All your assertions pass, and your code is ready for a new release. At this point, you can optimize the code for production by disabling the assertions that you added during development. Why should you optimize your code this way?
Assertions are great during development, but in production, they can affect the code’s performance. For example, a codebase with many assertions running all the time can be slower than the same code without assertions. Assertions take time to run, and they consume memory, so it’s advisable to disable them in production.
Now, how can you actually disable your assertions? Well, you have two options:
- Run Python with the
-Oor-OOoptions. - Set the
PYTHONOPTIMIZEenvironment variable to an appropriate value.
In this section, you’ll learn how to disable your assertions by using these two techniques. Before doing this, you’ll get to know the built-in __debug__ constant, which is the internal mechanism that Python uses to disable assertions.
Understanding the __debug__ Built-in Constant
Python has a built-in constant called __debug__. This constant is closely related to the assert statement. Python’s __debug__ is a Boolean constant, which defaults to True. It’s a constant because you can’t change its value once your Python interpreter is running:
>>>
>>> import builtins
>>> "__debug__" in dir(builtins)
True
>>> __debug__
True
>>> __debug__ = False
File "<stdin>", line 1
SyntaxError: cannot assign to __debug__
In this code snippet, you first confirm that __debug__ is a Python built-in that’s always available for you. True is the default value of __debug__, and there’s no way to change this value once your Python interpreter is running.
The value of __debug__ depends on which mode Python runs in, normal or optimized:
| Mode | Value of __debug__ |
|---|---|
| Normal (or debug) | True |
| Optimized | False |
Normal mode is typically the mode that you use during development, while optimized mode is what you should use in production. Now, what does __debug__ have to do with assertions? In Python, the assert statement is equivalent to the following conditional:
if __debug__:
if not expression:
raise AssertionError(assertion_message)
# Equivalent to
assert expression, assertion_message
If __debug__ is true, then the code under the outer if statement runs. The inner if statement checks expression for truthiness and raises an AssertionError only if the expression is not true. This is the default or normal Python mode, in which all your assertions are enabled because __debug__ is True.
On the other hand, if __debug__ is False, then the code under the outer if statement doesn’t run, meaning that your assertions will be disabled. In this case, Python is running in optimized mode.
Normal or debug mode allows you to have assertions in place as you develop and test the code. Once your current development cycle is complete, then you can switch to optimized mode and disable the assertions to get your code ready for production.
To activate optimized mode and disable your assertions, you can either start up the Python interpreter with the –O or -OO option, or set the system variable PYTHONOPTIMIZE to an appropriate value. You’ll learn how to do both operations in the following sections.
Running Python With the -O or -OO Options
You can disable all your assert statements by having the __debug__ constant set to False. To accomplish this task, you can use Python’s -O or -OO command-line options to run the interpreter in optimized mode.
The -O option internally sets __debug__ to False. This change removes the assert statements and any code that you’ve explicitly introduced under a conditional targeting __debug__. The -OO option does the same as -O and also discards docstrings.
Running Python with the -O or -OO command-line option makes your compiled bytecode smaller. Additionally, if you have several assertions or if __debug__: conditionals, then these command-line options can also make your code faster.
Now, what effect does this optimization have on your assertions? It disables them. For an example, open your command line or terminal within the directory containing the circle.py file and run an interactive session with the python -O command. Once there, run the following code:
>>>
>>> # Running Python in optimized mode
>>> __debug__
False
>>> from circle import Circle
>>> # Normal use of Circle
>>> ring = Circle(42)
>>> ring.correct_radius(1.02)
>>> ring.radius
42.84
>>> ring.area()
5765.656926346065
>>> # Invalid use of Circle
>>> ring = Circle(42)
>>> ring.correct_radius(-1.02)
>>> ring.radius
-42.84
>>> ring.area()
5765.656926346065
Because the -O option disables your assertions by setting __debug__ to False, your Circle class now accepts a negative radius, as the final example showcases. This behavior is completely wrong because you can’t have a circle with a negative radius. Additionaly, the circle’s area is computed using the wrong radius as an input.
The potential to disable assertions in optimized mode is the main reason why you must not use assert statements to validate input data but as an aid to your debugging and testing process.
A Pythonic solution for the Circle class would be to turn the .radius attribute into a managed attribute using the @property decorator. This way, you perform the .radius validation every time the attribute changes:
# circle.py
import math
class Circle:
def __init__(self, radius):
self.radius = radius
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
if value < 0:
raise ValueError("positive radius expected")
self._radius = value
def area(self):
return math.pi * self.radius ** 2
def correct_radius(self, correction_coefficient):
self.radius *= correction_coefficient
Now .radius is a managed attribute that provides setter and getter methods using the @property decorator. You’ve moved the validation code from .__init__() to the setter method, which is called whenever the class changes the value of .radius.
Now, the updated Circle works as expected if you run the code in optimized mode:
>>>
>>> # Running Python in optimized mode
>>> __debug__
False
>>> from circle import Circle
>>> # Normal use of Circle
>>> ring = Circle(42)
>>> ring.correct_radius(1.02)
>>> ring.radius
42.84
>>> ring.area()
5765.656926346065
>>> # Invalid use of Circle
>>> ring = Circle(42)
>>> ring.correct_radius(-1.02)
Traceback (most recent call last):
...
ValueError: positive radius expected
Circle always validates the value of .radius before assignment, and your class works correctly, raising a ValueError for negative values of .radius. That’s it! You’ve fixed the bug with an elegant solution.
An interesting side effect of running Python in optimized mode is that code under an explicit if __debug__: condition is also disabled. Consider the following script:
# demo.py
print(f"{__debug__ = }")
if __debug__:
print("Running in Normal mode!")
else:
print("Running in Optimized mode!")
This script explicitly checks the value of __debug__ in an if … else statement. The code in the if code block will run only if __debug__ is True. In contrast, if __debug__ is False, then the code in the else block will run.
Now try running the script in normal and optimized mode to check its behavior in each mode:
$ python demo.py
__debug__ = True
Running in Normal mode!
$ python -O demo.py
__debug__ = False
Running in Optimized mode!
When you execute the script in normal mode, the code under the if __debug__: condition runs because __debug__ is True in this mode. On the other hand, when you execute the script in optimized mode with the -O option, __debug__ changes to False, and the code under the else block runs.
Python’s -O command-line option removes assertions from the resulting compiled bytecode. Python’s -OO option performs the same kind of optimization as -O, with the addition of removing docstrings from your bytecode.
Because both options set __debug__ to False, any code under an explicit if __debug__: conditional also stops working. This behavior provides a powerful mechanism to introduce debugging-only code in your Python projects during their development stages.
Now you know the basics of using Python’s -O and -OO options to disable your assertions in production code. However, running Python with either of these options every time you need to run your production code seems repetitive and may be error-prone. To automate the process, you can use the PYTHONOPTIMIZE environment variable.
Setting the PYTHONOPTIMIZE Environment Variable
You can also run Python in optimized mode with disabled assertions by setting the PYTHONOPTIMIZE environment variable to an appropriate value. For example, setting this variable to a non-empty string is equivalent to running Python with the -O option.
To try PYTHONOPTIMIZE out, fire up your command line and run the following command:
- Windows
- Linux + macOS
C:> set PYTHONOPTIMIZE="1"
$ export PYTHONOPTIMIZE="1"
Once you’ve set PYTHONOPTIMIZE to a non-empty string, you can launch your Python interpreter with the bare-bones python command. This command will automatically run Python in optimized mode.
Now go ahead and run the following code from the directory containing your circle.py file:
>>>
>>> from circle import Circle
>>> # Normal use of Circle
>>> ring = Circle(42)
>>> ring.correct_radius(1.02)
>>> ring.radius
42.84
>>> # Invalid use of Circle
>>> ring = Circle(42)
>>> ring.correct_radius(-1.02)
>>> ring.radius
-42.84
Again, your assertions are off, and the Circle class accepts negative radius values. You’re running Python in optimized mode again.
Another possibility is to set PYTHONOPTIMIZE to an integer value, n, which is equivalent to running Python using the -O option n times. In other words, you’re using n levels of optimization:
- Windows
- Linux + macOS
C:> set PYTHONOPTIMIZE=1 # Equivalent to python -O
C:> set PYTHONOPTIMIZE=2 # Equivalent to python -OO
$ export PYTHONOPTIMIZE=1 # Equivalent to python -O
$ export PYTHONOPTIMIZE=2 # Equivalent to python -OO
You can use any integer number to set PYTHONOPTIMIZE. However, Python only implements two levels of optimization. Using a number greater than 2 has no real effect on your compiled bytecode. Additionally, setting PYTHONOPTIMIZE to 0 will cause the interpreter to run in normal mode.
Running Python in Optimized Mode
When you run Python, the interpreter compiles any imported module to bytecode on the fly. The compiled bytecode will live in a directory called __pycache__/, which is placed in the directory containing the module that provided the imported code.
Inside __pycache__/, you’ll find a .pyc file named after your original module plus the interpreter’s name and version. The name of the .pyc file will also include the optimization level used to compile the code.
For example, when you import code from circle.py, the Python 3.10 interpreter generates the following files, depending on the optimization level:
| File Name | Command | PYTHONOPTIMIZE |
|---|---|---|
circle.cpython-310.pyc |
python circle.py |
0 |
circle.cpython-310.opt-1.pyc |
python -O circle.py |
1 |
circle.cpython-310.opt-2.pyc |
python -OO circle.py |
2 |
The name of each file in this table includes the original module’s name (circle), the interpreter that generated the code (cpython-310), and the optimization level (opt-x). The table also summarizes the corresponding commands and values for the PYTHONOPTIMIZE variable. PEP 488 provides more context on this naming format for .pyc files.
The main results of running Python in the first level of optimization is that the interpreter sets __debug__ to False and removes the assertions from the resulting compiled bytecode. These optimizations make the code smaller and potentially faster than the same code running in normal mode.
The second level of optimization does the same as the first level. It also removes all the docstrings from the compiled code, which results in an even smaller compiled bytecode.
Testing Your Code With Assertions
Testing is another field in the development process where assertions are useful. Testing boils down to comparing an observed value with an expected one to check if they’re equal or not. This kind of check perfectly fits into assertions.
Assertions must check for conditions that should typically be true, unless you have a bug in your code. This idea is another important concept behind testing.
The pytest third-party library is a popular testing framework in Python. At its core, you’ll find the assert statement, which you can use to write most of your test cases in pytest.
Here are a few examples of writing test cases using assert statements. The examples below take advantage of some built-in functions, which provide the testing material:
# test_samples.py
def test_sum():
assert sum([1, 2, 3]) == 6
def test_len():
assert len([1, 2, 3]) > 0
def test_reversed():
assert list(reversed([1, 2, 3])) == [3, 2, 1]
def test_membership():
assert 3 in [1, 2, 3]
def test_isinstance():
assert isinstance([1, 2, 3], list)
def test_all():
assert all([True, True, True])
def test_any():
assert any([False, True, False])
def test_always_fail():
assert pow(10, 2) == 42
All these test cases use the assert statement. Most of them are written using the assertion formats that you learned before. They all showcase how you’d write real-world test cases to check different pieces of your code with pytest.
Now, why does pytest favor plain assert statements in test cases over a custom API, which is what other testing frameworks prefer? There are a couple of remarkable advantages behind this choice:
- The
assertstatement allowspytestto lower the entry barrier and somewhat flatten the learning curve because its users can take advantage of Python syntax that they already know. - The users of
pytestdon’t need to import anything from the library to start writing test cases. They only need to start importing things if their test cases get complicated, demanding more advanced features.
These advantages make working with pytest a pleasant experience for beginners and people coming from other testing frameworks with custom APIs.
For example, the standard-library unittest module provides an API consisting of a list of .assert*() methods that work pretty much like assert statements. This kind of API can be difficult to learn and memorize for developers starting with the framework.
You can use pytest to run all the test case examples above. First, you need to install the library by issuing the python -m pip install pytest command. Then you can execute pytest test_samples.py from the command-line. This latter command will display an output similar to the following:
========================== test session starts =========================
platform linux -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /home/user/python-assert
collected 8 items
test_samples.py .......F [100%]
========================== FAILURES =====================================
__________________________ test_always_fail _____________________________
def test_always_fail():
> assert pow(10, 2) == 42
E assert 100 == 42
E + where 100 = pow(10, 2)
test_samples.py:25: AssertionError
========================== short test summary info ======================
FAILED test_samples.py::test_always_fail - assert 100 == 42
========================== 1 failed, 7 passed in 0.21s ==================
The first highlighted line in this output tells you that pytest discovered and ran eight test cases. The second highlighted line shows that seven out of eight tests passed successfully. That’s why you get seven green dots and an F.
A remarkable feature to note is that pytest integrates nicely with the assert statement. The library can display error reports with detailed information about the failing assertions and why they’re failing. As an example, check out the the lines starting with the E letter in the above output. They display error messages.
Those lines clearly uncover the root cause of the failure. In this example, pow(10, 2) returns 100 instead of 42, which is intentionally wrong. You can use pytest.raises() to handle code that is expected to fail.
Understanding Common Pitfalls of assert
Even though assertions are such a great and useful tool, they have some downsides. Like any other tool, assertions can be misused. You’ve learned that you should use assertions mainly for debugging and testing code during development. In contrast, you shouldn’t rely on assertions to provide functionality in production code, which is one of the main drivers of pitfalls with assertions.
In particular, you may run into pitfalls if you use assertions for:
- Processing and validating data
- Handling errors
- Running operations with side effects
Another common source of issues with assertions is that keeping them enabled in production can negatively impact your code’s performance.
Finally, Python has assertions enabled by default, which can confuse developers coming from other languages. In the following sections, you’ll learn about all these possible pitfalls of assertions. You’ll also learn how to avoid them in your own Python code.
Using assert for Data Processing and Validation
You shouldn’t use assert statements to verify the user’s input or any other input data from external sources. That’s because production code typically disables assertions, which will remove all the verification.
For example, suppose you’re building an online store with Python, and you need to add functionality to accept discount coupons. You end up writing the following function:
# store.py
# Code under development
def price_with_discount(product, discount):
assert 0 < discount < 1, "discount expects a value between 0 and 1"
new_price = int(product["price"] * (1 - discount))
return new_price
Notice the assert statement in the first line of price_with_discount()? It’s there to guarantee that the discounted price won’t be equal to or lower than zero dollars. The assertion also ensures that the new price won’t be higher than the product’s original price.
Now consider the example of a pair of shoes at twenty-five percent off:
>>>
>>> from store import price_with_discount
>>> shoes = {"name": "Fancy Shoes", "price": 14900}
>>> # 25% off -> $111.75
>>> price_with_discount(shoes, 0.25)
11175
All right, price_with_discount() works nicely! It takes the product as a dictionary, applies the intended discount to the current price, and returns the new price. Now, try to apply some invalid discounts:
>>>
>>> # 200% off
>>> price_with_discount(shoes, 2.0)
Traceback (most recent call last):
...
AssertionError: discount expects a value between 0 and 1
>>> # 100% off
>>> price_with_discount(shoes, 1)
Traceback (most recent call last):
...
AssertionError: discount expects a value between 0 and 1
Applying an invalid discount raises an AssertionError that points out the violated condition. If you ever encounter this error while developing and testing your online store, then it shouldn’t be hard to figure out what happened by looking at the traceback.
The real problem with the example above comes if the end user can make direct calls to price_with_discount() in production code with disabled assertions. In this situation, the function won’t check the input value for discount, possibly accepting wrong values and breaking the correctness of your discount functionality.
In general, you can write assert statements to process, validate, or verify data during development. However, if those operations remain valid in production code, then make sure to replace them with an if statement or a try … except block.
Here’s a new version of price_with_discount() that uses a conditional instead of an assertion:
# store.py
# Code in production
def price_with_discount(product, discount):
if 0 < discount < 1:
new_price = int(product["price"] * (1 - discount))
return new_price
raise ValueError("discount expects a value between 0 and 1")
In this new implementation of price_with_discount(), you replace the assert statement with an explicit conditional statement. The function now applies the discount only if the input value is between 0 and 1. Otherwise, it raises a ValueError, signaling the problem.
Now you can wrap up any calls to this function in a try … except block that catches the ValueError and sends an informative message to the users so that they can take action accordingly.
The moral of this example is that you shouldn’t rely on the assert statement for data processing or data validation, because this statement is typically turned off in production code.
Handling Errors With assert
Another important pitfall with assertions is that sometimes developers use them as a quick form of error handling. As a result, if the production code removes assertions, then important error checks are also removed from the code. So, keep in mind that assertions aren’t a replacement for good error handling.
Here’s an example of using assertions for error handling:
# Bad practice
def square(x):
assert x >= 0, "only positive numbers are allowed"
return x ** 2
try:
square(-2)
except AssertionError as error:
print(error)
If you execute this code in production with disabled assertions, then square() will never run the assert statement and raise an AssertionError. In this situation, the try … except block is superfluous and nonfunctional.
What can you do to fix this example? Try updating square() to use an if statement and a ValueError:
# Best practice
def square(x):
if x < 0:
raise ValueError("only positive numbers are allowed")
return x ** 2
try:
square(-2)
except ValueError as error:
print(error)
Now square() deals with the condition by using an explicit if statement that can’t be disabled in production code. Your try … except block now handles a ValueError, which is a more appropriate exception in this example.
Don’t ever catch AssertionError exceptions in your code, because that would silence failing assertions, which is a clear sign of misused assertions. Instead, catch concrete exceptions that are clearly related to the errors that you’re handling and let your assertions fail.
Use assertions only to check errors that shouldn’t happen during the normal execution of your programs unless you have a bug. Remember that assertions can be disabled.
Running assert on Expressions With Side Effects
Another subtle pitfall with the assert statement appears when you use this statement to check operations, functions, or expressions that have some kind of side effect. In other words, these operations modify the state of objects outside the operation’s scope.
In those situations, the side effect takes place every time your code runs the assertion, which might silently change your program’s global state and behavior.
Consider the following toy example, in which a function modifies the value of a global variable as a side effect:
>>>
>>> sample = [42, 27, 40, 38]
>>> def popped(sample, index=-1):
... item = sample.pop(index)
... return item
...
>>> assert sample[-1] == popped(sample)
>>> assert sample[1] == popped(sample, 1)
>>> sample
[42, 40]
In this example, popped() returns item at a given index in the input sample of data, with the side effect of also removing said item.
Using assertions to make sure that your function is returning the correct item can seem appropriate. However, this will cause the function’s internal side effect to run in every assertion, modifying the original content of sample.
To prevent unexpected behaviors like the one in the above example, use assertion expressions that don’t cause side effects. For example, you can use pure functions that just take input arguments and return the corresponding output without modifying the state of objects from other scopes and namespaces.
Impacting Performance With assert
Too many assertions in production can impact your code’s performance. This issue becomes critical when the asserted conditions involve too much logic, such as long compound conditions, long-running predicate functions, and classes implying a costly instantiation process.
Assertions can impact your code’s performance in two main ways. They will:
- Take time to execute
- Use extra memory
An assert statement that checks for a None value can be relatively inexpensive. However, more complex assertions, especially those running heavy code, can measurably slow down your code. Assertions also consume memory to store their own code and any required data.
To avoid performance issues in production code, you should use Python’s -O or -OO command-line options or set the PYTHONOPTIMIZE environment variable according to your needs. Either strategy will optimize your code by generating an assertions-free compiled bytecode, which will run more quickly and take up less memory.
Additionally, to prevent performance issues during development, your assertions should be fairly slim and to the point.
Having assert Statements Enabled by Default
In Python, assertions are enabled by default. When the interpreter runs in normal mode, the __debug__ variable is True, and your assertions are enabled. This behavior makes sense because you typically develop, debug, and test your code in normal mode.
If you want to disable your assertions, then you need to do it explicitly. You can either run the Python interpreter with the -o or -OO options, or set the PYTHONOPTIMIZE environment variable to a proper value.
In contrast, other programming languages have assertions disabled by default. For example, if you’re coming into Python from Java, you may assume that your assertions won’t run unless you explicitly turn them on. This assumption can be a common source of confusion for Python beginners, so keep it in mind.
Conclusion
Now you know how to use Python’s assert statement to set sanity checks throughout your code and make sure that certain conditions are and remain true. When any of these conditions fail, you have a clear indication of what’s happening. This way, you can quickly debug and fix your code.
The assert statement is pretty handy when you need to document, debug, and test your code during the development stages. In this tutorial, you learned how to use assertions in your code and how they can make your debugging and testing process more efficient and straightforward.
In this tutorial, you learned:
- What assertions are and when to use them
- How Python’s
assertstatement works - How
assertis handy for documenting, debugging, and testing code - How assertions can be disabled to improve performance in production
- What common pitfalls you can face when using
assertstatements
With this knowledge on the assert statement, you can now write robust, reliable, and less buggy code, which can take you to the next level as a developer.
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using Python’s assert to Debug and Test Your Code
Рассмотрим популярные инструменты для анализа кода Python и подробно расскажем об их специфике и основных принципах работы.
Автор: Валерий Шагур, teacher assistance на курсе Программирование на Python
Высокая стоимость ошибок в программных продуктах предъявляет повышенные
требования к качеству кода. Каким критериям должен соответствовать хороший код?
Отсутствие ошибок, расширяемость, поддерживаемость, читаемость и наличие документации. Недостаточное внимание к любому из этих критериев может привести к появлению новых ошибок или снизить вероятность обнаружения уже существующих. Небрежно написанный или чересчур запутанный код, отсутствие документации напрямую влияют на время исправления найденного бага, ведь разработчику приходится заново вникать в код. Даже такие, казалось бы, незначительные вещи как неправильные имена переменных или отсутствие форматирования могут сильно влиять на читаемость и понимание кода.
Командная работа над проектом еще больше повышает требования к качеству кода, поэтому важным условием продуктивной работы команды становится описание формальных требований к написанию кода. Это могут быть соглашения, принятые в языке программирования, на котором ведется разработка, или собственное (внутрикорпоративное) руководство по стилю. Выработанные требования к оформлению кода не исключают появления «разночтений» среди разработчиков и временных затрат на их обсуждение. Кроме этого, соблюдение выработанных требований ложится на плечи программистов в виде дополнительной нагрузки. Все это привело к появлению инструментов для проверки кода на наличие стилистических и логических ошибок. О таких инструментах для языка программирования Python мы и поговорим в этой статье.
Анализаторы и автоматическое форматирование кода
Весь инструментарий, доступный разработчикам Python, можно условно разделить на две группы по способу реагирования на ошибки. Первая группа сообщает о найденных ошибках, перекладывая задачу по их исправлению на программиста. Вторая — предлагает пользователю вариант исправленного кода или автоматически вносит изменения.
И первая, и вторая группы включают в себя как простые утилиты командной строки для решения узкоспециализированных задач (например, проверка docstring или сортировка импортов), так и богатые по возможностям библиотеки, объединяющие в себе более простые утилиты. Средства анализа кода из первой группы принято называть линтерами (linter). Название происходит от lint — статического анализатора для языка программирования Си и со временем ставшего нарицательным. Программы второй группы называют форматировщиками (formatter).
Даже при поверхностном сравнении этих групп видны особенности работы с ними. При применении линтеров программисту, во-первых, необходимо писать код с оглядкой, дабы позже не исправлять найденные ошибки. И во вторых, принимать решение по поводу обнаруженных ошибок — какие требуют исправления, а какие можно проигнорировать. Форматировщики, напротив, автоматизируют процесс исправления ошибок, оставляя программисту возможность осуществлять контроль.
Часть 1
- pycodestyle
- pydocstyle
- pyflakes
- pylint
- vulture
Часть 2
- flake8
- prospector
- pylama
- autopep8
- yapf
- black
Соглашения принятые в статье и общие замечания
Прежде чем приступить к обзору программ, мы хотели бы обратить ваше внимание на несколько важных моментов.
Версия Python: во всех примерах, приведенных в статье, будет использоваться третья версия языка программирования Python.
Установка всех программ в обзоре практически однотипна и сводится к использованию пакетного менеджера pip.
$ python3.6 -m pip install --upgrade <package_name>
Некоторые из библиотек имеют готовые бинарные пакеты в репозиториях дистрибутивов linux или возможность установки с использованием git. Тем не менее для большей определенности и возможности повторения примеров из статьи, установка будет производится с помощью pip.
Об ошибках: стоит упомянуть, что говоря об ошибках, обнаруживаемых анализаторами кода, как правило, имеют в виду два типа ошибок. К первому относятся ошибки стиля (неправильные отступы, длинные строки), ко второму — ошибки в логике программы и ошибки синтаксиса языка программирования (опечатки при написании названий стандартных функций, неиспользуемые импорты, дублирование кода). Существуют и другие виды ошибок, например — оставленные в коде пароли или высокая цикломатическая сложность.
Тестовый скрипт: для примеров использования программ мы создали простенький по содержанию файл example.py. Мы сознательно не стали делать его более разнообразным по наличию в нем ошибок. Во-первых, добавление листингов с выводом некоторых анализаторов в таком случае сильно “раздуло” бы статью. Во-вторых, у нас не было цели детально показать различия в “отлове” тех или иных ошибок для каждой из утилит.
Содержание файла example.py:
import os
import notexistmodule
def Function(num,num_two):
return num
class MyClass:
"""class MyClass """
def __init__(self,var):
self.var=var
def out(var):
print(var)
if __name__ == "__main__":
my_class = MyClass("var")
my_class.out("var")
notexistmodule.func(5)
В коде допущено несколько ошибок:
- импорт неиспользуемого модуля os,
- импорт не существующего модуля notexistmodule,
- имя функции начинается с заглавной буквы,
- лишние аргументы в определении функции,
- отсутствие self первым аргументом в методе класса,
- неверное форматирование.
Руководства по стилям: для тех, кто впервые сталкивается с темой оформления кода, в качестве знакомства предлагаем прочитать официальные руководства по стилю для языка Python PEP8 и PEP257. В качестве примера внутрикорпоративных соглашений можно рассмотреть Google Python Style Guide — https://github.com/google/styleguide/blob/gh-pages/pyguide.md
Pycodestyle
Pycodestyle — простая консольная утилита для анализа кода Python, а именно для проверки кода на соответствие PEP8. Один из старейших анализаторов кода, до 2016 года носил название pep8, но был переименован по просьбе создателя языка Python Гвидо ван Россума.
Запустим проверку на нашем коде:
$ python3 -m pycodestyle example.py example.py:4:1: E302 expected 2 blank lines, found 1 example.py:4:17: E231 missing whitespace after ',' example.py:7:1: E302 expected 2 blank lines, found 1 example.py:10:22: E231 missing whitespace after ',' example.py:11:17: E225 missing whitespace around operator
Лаконичный вывод показывает нам строки, в которых, по мнению анализатора, есть нарушение соглашений PEP8. Формат вывода прост и содержит только необходимую информацию:
<имя файла>: <номер строки> :<положение символа>: <код и короткая расшифровка ошибки>
Возможности программы по проверке соглашений ограничены: нет проверок на правильность именования, проверка документации сводится к проверки длины docstring. Тем не менее функционал программы нельзя назвать “спартанским”, он позволяет настроить необходимый уровень проверок и получить различную информацию о результатах анализа. Запуск с ключом —statistics -qq выводит статистику по ошибкам:
$ python3 -m pycodestyle --statistics -qq example.py 1 E225 missing whitespace around operator 2 E231 missing whitespace after ',' 2 E302 expected 2 blank lines, found 1
Более наглядный вывод можно получить при использовании ключа —show-source. После каждого сообщения об ошибке будет выведена строка исходного кода, в которой содержится ошибка.
$ python3 -m pycodestyle --show-source example.py example.py:4:1: E302 expected 2 blank lines, found 1 def Function(num,num_two): ^ example.py:4:17: E231 missing whitespace after ',' def Function(num,num_two): ^ example.py:7:1: E302 expected 2 blank lines, found 1 class MyClass: ^ example.py:10:22: E231 missing whitespace after ',' def __init__(self,var): ^ example.py:11:17: E225 missing whitespace around operator self.var=var ^
Если есть необходимость посмотреть, какие из соглашений PEP8 были нарушены, используйте ключ — show-pep8. Программа выведет список всех проверок с выдержками из PEP8 для случаев нарушений. При обработке файлов внутри директорий предусмотрена возможность фильтрации по шаблону. Pycodestyle позволяет сохранять настройки поиска в конфигурационных файлах как глобально, так и на уровне проекта.
Pydocstyle
Утилиту pydocstyle мы уже упоминали в статье Работа с документацией в Python: поиск информации и соглашения. Pydocstyle проверяет наличие docstring у модулей, классов, функций и их соответствие официальному соглашению PEP257.
$ python3 -m pydocstyle example.py example.py:1 at module level: D100: Missing docstring in public module example.py:4 in public function `Function`: D103: Missing docstring in public function example.py:7 in public class `MyClass`: D400: First line should end with a period (not 's') example.py:7 in public class `MyClass`: D210: No whitespaces allowed surrounding docstring text example.py:10 in public method `__init__`: D107: Missing docstring in __init__ example.py:13 in public method `out`: D102: Missing docstring in public method
Как мы видим из листинга, программа указала нам на отсутствие документации в определениях функции, методов класса и ошибки оформления в docstring класса. Вывод можно сделать более информативным, если использовать ключи —explain и —source при вызове программы. Функционал pydocstyle практически идентичен описанному выше для pycodestyle, различия касаются лишь названий ключей.
Pyflakes
В отличие от уже рассмотренных инструментов для анализа кода Python pyflakes не делает проверок стиля. Цель этого анализатора кода — поиск логических и синтаксических ошибок. Разработчики pyflakes сделали упор на скорость работы программы, безопасность и простоту. Несмотря на то, что данная утилита не импортирует проверяемый файл, она прекрасно справляется c поиском синтаксических ошибок и делает это быстро. С другой стороны, такой подход сильно сужает область проверок.
Функциональность pyflakes — “нулевая”, все что он умеет делать — это выводить результаты анализа в консоль:
$ python3 -m pyflakes example.py example.py:1: 'os' imported but unused
В нашем тестовом скрипте, он нашел только импорт не используемого модуля os. Вы можете самостоятельно поэкспериментировать с запуском программы и передачей ей в качестве параметра командной строки Python файла, содержащего синтаксические ошибки. Данная утилита имеет еще одну особенность — если вы используете обе версии Python, вам придется установить отдельные утилиты для каждой из версий.
Pylint
До сих пор мы рассматривали утилиты, которые проводили проверки на наличие либо стилистических, либо логических ошибок. Следующий в обзоре статический инструмент для анализа кода Python — Pylint, который совместил в себе обе возможности. Этот мощный, гибко настраиваемый инструмент для анализа кода Python отличается большим количеством проверок и разнообразием отчетов. Это один из самых “придирчивых” и “многословных” анализаторов кода. Анализ нашего тестового скрипта выдает весьма обширный отчет, состоящий из списка найденных в ходе анализа недочетов, статистических отчетов, представленных в виде таблиц, и общей оценки кода:
$ python3.6 -m pylint --reports=y text example.py
************* Module text
/home/ququshka77/.local/lib/python3.6/site-packages/pylint/reporters/text.py:79:22: W0212: Access to a protected member _splitstrip of a client class (protected-access)
************* Module example
example.py:4:16: C0326: Exactly one space required after comma
def Function(num,num_two):
^ (bad-whitespace)
example.py:10:21: C0326: Exactly one space required after comma
def __init__(self,var):
^ (bad-whitespace)
example.py:11:16: C0326: Exactly one space required around assignment
self.var=var
^ (bad-whitespace)
example.py:1:0: C0111: Missing module docstring (missing-docstring)
example.py:2:0: E0401: Unable to import 'notexistmodule' (import-error)
example.py:4:0: C0103: Function name "Function" doesn't conform to snake_case naming style (invalid-name)
example.py:4:0: C0111: Missing function docstring (missing-docstring)
example.py:4:17: W0613: Unused argument 'num_two' (unused-argument)
example.py:13:4: C0111: Missing method docstring (missing-docstring)
example.py:13:4: E0213: Method should have "self" as first argument (no-self-argument)
example.py:7:0: R0903: Too few public methods (1/2) (too-few-public-methods)
example.py:18:4: C0103: Constant name "my_class" doesn't conform to UPPER_CASE naming style (invalid-name)
example.py:19:4: E1121: Too many positional arguments for method call (too-many-function-args)
example.py:1:0: W0611: Unused import os (unused-import)
Report
======
112 statements analysed.
Statistics by type
+----------+----------+---------------+-------------+-------------------+---------------+
|type |number |old number |difference |%documented |%badname |
+======+======+========+========+===========+========+
|module |2 |2 |= |50.00 |0.00 |
+-----------+----------+---------------+-------------+-------------------+---------------+
|class |5 |5 |= |100.00 |0.00 |
+-----------+----------+---------------+-------------+-------------------+---------------+
|method |11 |11 |= |90.91 |0.00 |
+-----------+----------+---------------+-------------+-------------------+---------------+
|function |4 |4 |= |75.00 |25.00 |
+-----------+----------+---------------+-------------+-------------------+---------------+
External dependencies
::
pylint
-interfaces (text)
-reporters (text)
| -ureports
| -text_writer (text)
-utils (text)
Raw metrics
+-------------+----------+-------+-----------+-------------+
|type |number |% |previous |difference |
+=======+======+=====+=====+========+
|code |128 |48.30 |128 |= |
+-------------+----------+--------+-----------+------------+
|docstring |84 |31.70 |84 |= |
+-------------+----------+--------+-----------+------------+
|comment |16 |6.04 |16 |= |
+-------------+----------+--------+-----------+------------+
|empty |37 |13.96 |37 |= |
+-------------+----------+--------+-----------+------------+
Duplication
+-------------------------------+------+------------+-------------+
| |now |previous |difference |
+=================+=====+======+========+
|nb duplicated lines |0 |0 |= |
+-------------------------------+-------+------------+------------+
|percent duplicated lines |0.000 |0.000 |= |
+-------------------------------+-------+------------+------------+
Messages by category
+--------------+----------+-----------+-------------+
|type |number |previous |difference |
+========+======+======+========+
|convention |8 |8 |= |
+--------------+----------+-----------+-------------+
|refactor |1 |1 |= |
+--------------+-----------+----------+-------------+
|warning |3 |3 |= |
+--------------+-----------+----------+-------------+
|error |3 |3 |= |
+--------------+-----------+----------+-------------+
% errors / warnings by module
+-----------+--------+-----------+----------+--------------+
|module |error |warning |refactor |convention |
+======+=====+======+======+========+
|example |100.00 |66.67 |100.00 |100.00 |
+-----------+---------+----------+-----------+-------------+
|text |0.00 |33.33 |0.00 |0.00 |
+-----------+---------+----------+-----------+-------------+
Messages
+-----------------------------+----------------+
|message id |occurrences |
+=================+=========+
|missing-docstring |3 |
+-----------------------------+----------------+
|bad-whitespace |3 |
+------------------------------+---------------+
|invalid-name |2 |
+------------------------------+---------------+
|unused-import |1 |
+------------------------------+---------------+
|unused-argument |1 |
+------------------------------+---------------+
|too-many-function-args |1 |
+------------------------------+---------------+
|too-few-public-methods |1 |
+------------------------------+---------------+
|protected-access |1 |
+------------------------------+---------------+
|no-self-argument |1 |
+------------------------------+---------------+
|import-error |1 |
+------------------------------+---------------+
------------------------------------------------------------------------------------------
Your code has been rated at 7.59/10 (previous run: 7.59/10, +0.00)
Программа имеет свою внутреннюю маркировку проблемных мест в коде:
[R]efactor — требуется рефакторинг,
[C]onvention — нарушено следование стилистике и соглашениям,
[W]arning — потенциальная ошибка,
[E]rror — ошибка,
[F]atal — ошибка, которая препятствует дальнейшей работе программы.
Для вывода подробного отчета мы использовали ключ командной строки —reports=y.
Более гибко настроить вывод команды позволяют разнообразные ключи командной строки. Настройки можно сохранять в файле настроек rcfile. Мы не будем приводить подробное описание ключей и настроек, для этого есть официальная документация — https://pylint.readthedocs.io/en/latest/index.html#, остановимся лишь на наиболее интересных, с нашей точки зрения, возможностях утилиты:
— Генерация файла настроек (—generate-rcfile). Позволяет не писать конфигурационный файл с нуля. В созданном rcfile содержатся все текущие настройки с подробными комментариями к ним, вам остается только отредактировать его под собственные требования.
— Отключение вывода в коде. При редактировании кода есть возможность вставить блокирующие вывод сообщений комментарии. Чтобы продемонстрировать это, в определение функции в файле примера example.py добавим строку:
# pylint: disable=unused-argument
и запустим pylint. Из результатов проверки “исчезло” сообщение:
example.py:4:17: W0613: Unused argument 'num_two' (unused-argument)
— Создание отчетов в формате json (—output-format=json). Полезно, если необходимо сохранение или дальнейшая обработка результатов работы линтера. Вы также можете создать собственный формат вывода данных.
— Параллельный запуск (-j 4). Запуск в нескольких параллельных потоках на многоядерных процессорах сокращает время проверки.
— Встроенная документация. Вызов программы с ключом —help-msg=<key> выведет справку по ключевому слову key. В качестве ключевого слова может быть код сообщения (например: E0401) или символическое имя сообщения (например: import-error). Ниже приведен листинг получения справки по ключу import-error:
$ python3.6 -m pylint --help-msg=import-error :import-error (E0401): *Unable to import %s* Used when pylint has been unable to import a module. This message belongs to the imports checker.
— Система оценки сохраняет последний результат и при последующих запусках показывает изменения, что позволяет количественно оценить прогресс исправлений.
— Плагины — отличная возможность изменять поведение pylint. Их применение может оказаться полезным в случаях, когда pylint неправильно обрабатывает код и есть “ложные” срабатывания, или когда требуется отличный от стандартного формат вывода результатов.
Vulture
Vulture — небольшая утилита для поиска “мертвого” кода в программах Python. Она использует модуль ast стандартной библиотеки и создает абстрактные синтаксические деревья для всех файлов исходного кода в проекте. Далее осуществляется поиск всех объектов, которые были определены, но не используются. Vulture полезно применять для очистки и нахождения ошибок в больших базовых кодах.
Продолжение следует
Во второй части мы продолжим разговор об инструментах для анализа кода Python. Будут рассмотрены линтеры, представляющие собой наборы утилит. Также мы посмотрим, какие программы можно использовать для автоматического форматирования кода.
Еще статьи по Python
- 26 полезных возможностей Python: букварь разработки от А до Z;
- ТОП-15 трюков в Python 3, делающих код понятнее и быстрее;
- Новый Python: 7 возможностей, которые вам понравятся;
- Крупнейшая подборка Python-каналов на Youtube;
- Изучение Python: ТОП-10 вопросов разной направленности.
How do you test that a Python function throws an exception?
How does one write a test that fails only if a function doesn’t throw
an expected exception?
Short Answer:
Use the self.assertRaises method as a context manager:
def test_1_cannot_add_int_and_str(self):
with self.assertRaises(TypeError):
1 + '1'
Demonstration
The best practice approach is fairly easy to demonstrate in a Python shell.
The unittest library
In Python 2.7 or 3:
import unittest
In Python 2.6, you can install a backport of 2.7’s unittest library, called unittest2, and just alias that as unittest:
import unittest2 as unittest
Example tests
Now, paste into your Python shell the following test of Python’s type-safety:
class MyTestCase(unittest.TestCase):
def test_1_cannot_add_int_and_str(self):
with self.assertRaises(TypeError):
1 + '1'
def test_2_cannot_add_int_and_str(self):
import operator
self.assertRaises(TypeError, operator.add, 1, '1')
Test one uses assertRaises as a context manager, which ensures that the error is properly caught and cleaned up, while recorded.
We could also write it without the context manager, see test two. The first argument would be the error type you expect to raise, the second argument, the function you are testing, and the remaining args and keyword args will be passed to that function.
I think it’s far more simple, readable, and maintainable to just to use the context manager.
Running the tests
To run the tests:
unittest.main(exit=False)
In Python 2.6, you’ll probably need the following:
unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromTestCase(MyTestCase))
And your terminal should output the following:
..
----------------------------------------------------------------------
Ran 2 tests in 0.007s
OK
<unittest2.runner.TextTestResult run=2 errors=0 failures=0>
And we see that as we expect, attempting to add a 1 and a '1' result in a TypeError.
For more verbose output, try this:
unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(MyTestCase))
Анализ кода в Python может быть трудной темой, но очень полезной в тех случаях, когда вам нужно повысить производительность вашей программы. Существует несколько анализаторов кода для Python, которые вы можете использовать для проверки своего кода и выяснить, соответствует ли он стандартам. Самым популярным можно назвать pylint. Он очень удобен в настойках и подключениях. Он также проверяет ваш код на соответствие с PEP8, официальным руководством по стилю ядра Python, а также ищет программные ошибки. Обратите внимание на то, что pylint проверяет ваш код на большую часть стандартов PEP8, но не на все. Также мы уделим наше внимание тому, чтобы научиться работать с другим анализатором кода, а именно pyflakes.
Начнем с pylint
Пакет pylint не входит в Python, так что вам нужно будет посетить PyPI (Python Package Index), или непосредственно сайт пакета для загрузки. Вы можете использовать следующую команду, которая сделает всю работу за вас:
Если все идет по плану, то pylint установится, и мы сможем пойти дальше.
Анализ вашего кода
После установки pylint вы можете запустить его в командной строке, без каких либо аргументов, что бы увидеть, какие опции он принимает. Если это не сработало, можете прописать полный путь, вот так:
|
c:Python34Scriptspylint |
Теперь нам нужен какой-нибудь код для анализа. Вот часть кода, которая содержит четыре ошибки. Сохраните её в файле под названием crummy_code.py:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import sys class CarClass: «»»»»» def __init__(self, color, make, model, year): «»»Constructor»»» self.color = color self.make = make self.model = model self.year = year if «Windows» in platform.platform(): print(«You’re using Windows!») self.weight = self.getWeight(1, 2, 3) def getWeight(this): «»»»»» return «2000 lbs» |
Можете увидеть ошибки не запуская код? Давайте посмотрим, может ли pylint найти их!
Есть вопросы по Python?
На нашем форуме вы можете задать любой вопрос и получить ответ от всего нашего сообщества!
Telegram Чат & Канал
Вступите в наш дружный чат по Python и начните общение с единомышленниками! Станьте частью большого сообщества!
Паблик VK
Одно из самых больших сообществ по Python в социальной сети ВК. Видео уроки и книги для вас!
После запуска этой команды вы увидите большую выдачу на вашем экране. Вот частичный пример:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
c:py101>c:Python34Scriptspylint crummy_code.py No config file found, using default configuration ************* Module crummy_code C: 2, 0: Trailing whitespace (trailing-whitespace) C: 5, 0: Trailing whitespace (trailing-whitespace) C: 12, 0: Trailing whitespace (trailing-whitespace) C: 15, 0: Trailing whitespace (trailing-whitespace) C: 17, 0: Trailing whitespace (trailing-whitespace) C: 1, 0: Missing module docstring (missing-docstring) C: 3, 0: Empty class docstring (empty-docstring) C: 3, 0: Old-style class defined. (old-style-class) E: 13,24: Undefined variable ‘platform’ (undefined-variable) E: 16,36: Too many positional arguments for function call (too-many-function-args) C: 18, 4: Invalid method name «getWeight» (invalid-name) C: 18, 4: Empty method docstring (empty-docstring) E: 18, 4: Method should have «self» as first argument (no-self-argument) R: 18, 4: Method could be a function (no-self-use) R: 3, 0: Too few public methods (1/2) (too-few-public-methods) W: 1, 0: Unused import sys (unused-import) |
Давайте немного притормозим и разберемся. Сначала нам нужно понять, что означают буквы:
- С – конвенция (convention)
- R – рефакторинг (refactor)
- W – предупреждение (warning)
- E – ошибка (error)
Наш pylint нашел 3 ошибки, 4 проблемы с конвенцией, 2 строки, которые нуждаются в рефакторинге и одно предупреждение. Предупреждение и 3 ошибки – это как раз то, что я искал. Мы попытаемся исправить этот код и устранить ряд проблем. Для начала мы наведем порядок в импортах, и изменить функцию getWeight на get_weight, в связи с тем, что camelCase не используется в названиях методов. Нам также нужно исправить вызов get_weight, чтобы он передавал правильное количество аргументов и исправить его, чтобы “self” выступал в качестве первого аргумента. Взглянем на новый код:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# crummy_code_fixed.py import platform class CarClass: «»»»»» def __init__(self, color, make, model, year): «»»Constructor»»» self.color = color self.make = make self.model = model self.year = year if «Windows» in platform.platform(): print(«You’re using Windows!») self.weight = self.get_weight(3) def get_weight(self, this): «»»»»» return «2000 lbs» |
Давайте запустим новый код с pylint и посмотрим, насколько успешно мы провели работу. Для краткости, мы еще раз рассмотрим первую часть:
|
c:py101>c:Python34Scriptspylint crummy_code_fixed.py No config file found, using default configuration ************* Module crummy_code_fixed C: 1,0: Missing docstring C: 4,0: CarClass: Empty docstring C: 21,4: CarClass.get_weight: Empty docstring W: 21,25: CarClass.get_weight: Unused argument ‘this’ R: 21,4: CarClass.get_weight: Method could be a function R: 4,0: CarClass: Too few public methods (1/2) |
Как мы видим, это очень помогло. Если мы добавим docstrings, мы можем снизить количество ошибок вдвое. Теперь мы готовы перейти к pyflakes!
Работаем с pyflakes
Проект pyflakes это часть чего-то, что называется Divmod Project. Pyflakes на самом деле не выполняет проверяемый код также, как и pylint. Вы можете установить pyflakes при помощи pip, easy_install, или из другого источника.
Данный сервис может предложить Вам персональные условия при заказе классов на посты и фото в Одноклассники. Приобретайте необходимый ресурс не только со скидками, но и с возможностью подобрать наилучшее качество и скорость поступления.
Мы начнем с запуска pyflakes в изначальной версии той же части кода, которую мы использовали для проверки pylint. Вот и он:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import sys class CarClass: «»»»»» def __init__(self, color, make, model, year): «»»Constructor»»» self.color = color self.make = make self.model = model self.year = year if «Windows» in platform.platform(): print(«You’re using Windows!») self.weight = self.getWeight(1, 2, 3) def getWeight(this): «»»»»» return «2000 lbs» |
Как мы отмечали в предыдущем разделе, в этом поломанном коде четыре ошибки, три из которых препятствуют работе программы. Давайте посмотрим, что же pyflakes может найти. Попытайтесь запустить данную команду и на выходе вы должны получить следующее:
|
c:py101>c:Python34Scriptspyflakes.exe crummy_code.py crummy_code.py:1: ‘sys’ imported but unused crummy_code.py:13: undefined name ‘platform’ |
Несмотря на суперски быструю скорость возврата выдачи, pyflakes не нашел все ошибки. Вызов метода getWeight передает слишком много аргументов, также метод getWeight сам по себе определен некорректно, так как у него нет аргумента self. Что-же, вы, собственно, можете называть первый аргумент так, как вам угодно, но в конвенции он всегда называется self. Если вы исправили код, оперируя тем, что вам сказал pyflakes, код не заработает, несмотря на это.
Подведем итоги
Следующим шагом должна быть попытка запуска pylint и pyflakes в вашем собственном коде, либо же в пакете Python, вроде SQLAlchemy, после чего следует изучить полученные в выдаче данные. Вы можете многое узнать о своем коде, используя данные инструменты. pylint интегрирован с Wingware, Editra, и PyDev. Некоторые предупреждения pylint могут показаться вам раздражительными, или не особо уместными. Существует несколько способов избавиться от таких моментов, как предупреждения об устаревании, через опции командной строки. Вы также можете использовать -generate-rcfile для создания примера файла config, который поможет вам контролировать работу pylint. Обратите внимание на то, что pylint и pyflakes не импортируют ваш код, так что вам не нужно беспокоиться о нежелательных побочных эффектах.
Являюсь администратором нескольких порталов по обучению языков программирования Python, Golang и Kotlin. В составе небольшой команды единомышленников, мы занимаемся популяризацией языков программирования на русскоязычную аудиторию. Большая часть статей была адаптирована нами на русский язык и распространяется бесплатно.
E-mail: vasile.buldumac@ati.utm.md
Образование
Universitatea Tehnică a Moldovei (utm.md)
- 2014 — 2018 Технический Университет Молдовы, ИТ-Инженер. Тема дипломной работы «Автоматизация покупки и продажи криптовалюты используя технический анализ»
- 2018 — 2020 Технический Университет Молдовы, Магистр, Магистерская диссертация «Идентификация человека в киберпространстве по фотографии лица»
Время на прочтение
14 мин
Количество просмотров 82K
Всем доброго!
От нашего стола к вашему…
То есть от нашего курса «Разработчик Python», несмотря на стремительно приближающий Новый год, мы подготовили вам интересный перевод о различных методах тестирования в Python.
Это руководство для тех, кто уже написал классное приложение на Python, но еще не писал для
них тесты.
Тестирование в Python — обширная тема с кучей тонкостей, но не обязательно все усложнять. В несколько простых шагов можно создать простые тесты для приложения, постепенно наращивая сложность на их основе.
В этом руководстве вы узнаете, как создать базовый тест, выполнить его и найти все баги, до того как это сделают пользователи! Вы узнаете о доступных инструментах для написания и выполнения тестов, проверите производительность приложения и даже посмотрите на проблемы безопасности.
Тестирование Кода
Тестировать код можно разными способами. В этом руководстве вы познакомитесь с методами от наиболее простых до продвинутых.
Автоматизированное vs. Ручное Тестирование
Хорошие новости! Скорее всего вы уже сделали тест, но еще не осознали этого. Помните, как вы впервые запустили приложение и воспользовались им? Вы проверили функции и поэкспериментировали с ними? Такой процесс называется исследовательским тестированием, и он является формой ручного тестирования.
Исследовательское тестирование — тестирование, которое проводится без плана. Во время исследовательского тестирования вы исследуете приложение.
Чтобы создать полный список мануальных тестов, достаточно составить перечень всех функций приложения, различных типов ввода, которые оно принимает, и ожидаемые результаты. Теперь, каждый раз когда вы меняете что-то в коде, нужно заново проверять каждый из элементов этого списка.
Звучит безрадостно, верно?
Поэтому нужны автоматические тесты. Автоматическое тестирование — исполнение плана тестирования (части приложения, требующие тестирования, порядок их тестирования и ожидаемые результаты) с помощью скрипта, а не руками человека. В Python уже есть набор инструментов и библиотек, которые помогут создать автоматизированные тесты для вашего приложения. Рассмотрим эти инструменты и библиотеки в нашем туториале.
Модульные Тесты VS. Интеграционные Тесты
Мир тестирования полон терминов, и теперь, зная разницу между ручным и автоматизированным тестированием, опустимся на уровень глубже.
Подумайте, как можно протестировать фары машины? Вы включаете фары (назовем это шагом тестирования), выходите из машины сами или просите друга, чтобы проверить, что фары зажглись (а это — тестовое суждение). Тестирование нескольких компонентов называется интеграционным тестированием.
Подумайте о всех вещах, которые должны правильно работать, чтобы простая задача выдала корректный результат. Эти компоненты похожи на части вашего приложения: все те классы, функции, модули, что вы написали.
Главная сложность интеграционного тестирования возникает, когда интеграционный тест не дает правильный результат. Сложно оценить проблему, не имея возможности изолировать сломанную часть системы. Если фары не зажглись, возможно лампочки сломаны. Или может аккумулятор разряжен? А может проблема в генераторе? Или вообще сбой в компьютере машины?
Современные машины сами оповестят вас о поломке лампочек. Определяется это с помощью модульного теста.
Модульный тест (юнит-тест) — небольшой тест, проверяющий корректность работы отдельного компонента. Модульный тест помогает изолировать поломку и быстрее устранить ее.
Мы поговорили о двух видах тестов:
- Интеграционный тест, проверяющий компоненты системы и их взаимодействие друг с другом;
- Модульный тест, проверяющий отдельный компонент приложения.
- Вы можете создать оба теста на Python. Чтобы написать тест для встроенной функции sum(), нужно сравнить выходные данные sum() с известными значениями.
Например, вот так можно проверить что сумма чисел (1, 2, 3) равна 6:
>>> assert sum([1, 2, 3]) == 6, "Should be 6"
Значения правильные, поэтому в REPL ничего не будет выведено. Если результат sum() некорректный, будет выдана AssertionError с сообщением “Should be 6” (“Должно быть 6”). Проверим оператор утверждения еще раз, но теперь с некорректными значениями, чтобы получить AssertionError:
>>> assert sum([1, 1, 1]) == 6, "Should be 6"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: Should be 6
В REPL вы увидете AssertionError, так как значение sum() не равно 6.
Вместо REPL, положите это в новый Python-файл с названием test_sum.py и выполните его снова:
def test_sum():
assert sum([1, 2, 3]) == 6, "Should be 6"
if __name__ == "__main__":
test_sum()
print("Everything passed")
Теперь у вас есть написанный тест-кейс (тестовый случай), утверждение и точка входа (командной строки). Теперь это можно выполнить в командной строке:
$ python test_sum.py
Everything passed
Вы видите успешный результат, “Everything passed” (“Все пройдено”).
sum() в Python принимает на вход любой итерируемый в качестве первого аргумента. Вы проверили список. Попробуем протестировать кортеж. Создадим новый файл с названием test_sum_2.py со следующим кодом:
def test_sum():
assert sum([1, 2, 3]) == 6, "Should be 6"
def test_sum_tuple():
assert sum((1, 2, 2)) == 6, "Should be 6"
if __name__ == "__main__":
test_sum()
test_sum_tuple()
print("Everything passed")
Выполнив test_sum_2.py, скрипт выдаст ошибку, так как sum() (1, 2, 2) должен быть равен 5, а не 6. В результате скрипт выдает сообщение об ошибке, строку кода и трейсбек:
$ python test_sum_2.py
Traceback (most recent call last):
File "test_sum_2.py", line 9, in <module>
test_sum_tuple()
File "test_sum_2.py", line 5, in test_sum_tuple
assert sum((1, 2, 2)) == 6, "Should be 6"
AssertionError: Should be 6
Можно увидеть, как ошибка в коде вызывает ошибку в консоли с информацией, где она произошла, и каким был ожидаемый результат.
Такие тесты подойдут для простой проверки, но что если ошибки есть больше, чем в одном? На помощь приходят исполнители тестов (test runners). Исполнитель тестов — особое приложение, спроектированное для проведение тестов, проверки данных вывода и предоставления инструментов для отладки и диагностики тестов и приложений.
Выбор Исполнителя Тестов
Для Python доступно множество исполнителей тестов. Например, в стандартную библиотеку Python встроен unittest. В этом руководстве, будем использовать тест-кейсы и исполнители тестов unittest. Принципы работы unittest легко адаптируются для других фреймворков. Перечислим самые популярные исполнители тестов:
- unittest;
- nose или nose2;
- pytest.
Важно выбрать исполнитель тестов, соответствующий вашим требованиям и опытности.
unittest
unittest встроен в стандартную библиотеку Python, начиная с версии 2.1. Вы наверняка столкнетесь с ним в коммерческих приложениях Python и проектах с открытым исходным кодом.
В unittest есть тестовый фреймворк и исполнитель тестов. При написании и исполнении тестов нужно соблюдать некоторые важные требования.
unittest требует:
- Помещать тесты в классы, как методы;
- Использовать специальные методы утверждения. Класс TestCase вместо обычного встроенного выражения assert.
Чтобы превратить ранее написанный пример в тест-кейс unittest, необходимо:
- Импортировать unittest из стандартной библиотеки;
- Создать класс под названием
TestSum, который будет наследовать классTestCase; - Сконвертировать тестовые функции в методы, добавив
selfв качестве первого аргумента; - Изменить утверждения, добавив использование
self.assertEqual()метода в классеTestCase; - Изменить точку входа в командной строке на вызов
unittest.main().
Следуя этим шагам, создайте новый файл test_sum_unittest.py со таким кодом:
import unittest
class TestSum(unittest.TestCase):
def test_sum(self):
self.assertEqual(sum([1, 2, 3]), 6, "Should be 6")
def test_sum_tuple(self):
self.assertEqual(sum((1, 2, 2)), 6, "Should be 6")
if __name__ == '__main__':
unittest.main()
Выполнив это в командной строке, вы получите одно удачное завершение (обозначенное .) и одно неудачное (обозначенное F):
$ python test_sum_unittest.py
.F
======================================================================
FAIL: test_sum_tuple (__main__.TestSum)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_sum_unittest.py", line 9, in test_sum_tuple
self.assertEqual(sum((1, 2, 2)), 6, "Should be 6")
AssertionError: Should be 6
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
Таким образом, вы выполнили два теста с помощью исполнителя тестов unittest.
Примечание: Если вы пишете тест-кейсы для Python 2 и 3 — будьте осторожны. В версиях Python 2.7 и ниже unittest называется unittest 2. При импорте из unittest вы получите разные версии с разными функциями в Python 2 и Python 3.
Чтобы узнать больше о unittest’ах почитайте unittest документацию.
nose
Со временем, после написания сотни, а то и тысячи тестов для приложения, становится все сложнее понимать и использовать данные вывода unittest.
nose совместим со всеми тестами, написанными с unittest фреймворком, и может заменить его тестовый исполнитель. Разработка nose, как приложения с открытым исходным кодом, стала тормозиться, и был создан nose2. Если вы начинаете с нуля, рекомендуется использовать именно nose2.
Для начала работы с nose2 нужно установить его из PyPl и запустить в командной строке. nose2 попытается найти все тестовые скрипы с test*.py в названии и все тест-кейсы, унаследованные из unittest.TestCase в вашей текущей директории:
$ pip install nose2
$ python -m nose2
.F
======================================================================
FAIL: test_sum_tuple (__main__.TestSum)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_sum_unittest.py", line 9, in test_sum_tuple
self.assertEqual(sum((1, 2, 2)), 6, "Should be 6")
AssertionError: Should be 6
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
Так выполняется тест, созданный в test_sum_unittest.py, из исполнителя тестов nose2. nose2 предоставляет множество флагов командной строки для фильтрации исполняемых тестов. Чтобы узнать больше, советуем ознакомиться с документацией Nose 2.
pytest
pytest поддерживает выполнение тест-кейсов unittest. Но настоящее преимущество pytest — его тест-кейсы. Тест-кейсы pytest — серия функций в Python-файле с test_ в начале названия.
Есть в нем и другие полезные функции:
- Поддержка встроенных выражений assert вместо использования специальных self.assert*() методов;
- Поддержка фильтрации тест-кейсов;
- Возможность повторного запуска с последнего проваленного теста;
- Экосистема из сотен плагинов, расширяющих функциональность.
Пример тест-кейса TestSum для pytest будет выглядеть следующим образом:
def test_sum():
assert sum([1, 2, 3]) == 6, "Should be 6"
def test_sum_tuple():
assert sum((1, 2, 2)) == 6, "Should be 6"
Вы избавились от TestCase, использования классов и точек входа командной строки.
Больше информации можно найти на Сайте Документации Pytest.
Написание Первого Теста
Объединим все, что мы уже узнали, и вместо встроенной функции sum() протестируем простую реализацию с теми же требованиями.
Создайте новую папку для проекта, внутри которой создайте новую папку с названием my_sum. Внутри my_sum создайте пустой файл с названием _init_.py. Наличие этого файла значит, что папка my_sum может быть импортирована в виде модуля из родительской директории.
Структура папок будет выглядеть так:
project/
│
└── my_sum/
└── __init__.py
Откройте my_sum/__init__.py и создайте новую функцию с названием sum(), которая берет на вход итерируемые (список, кортеж, множество) и складывает значения.
def sum(arg):
total = 0
for val in arg:
total += val
return total
В этом примере создается переменная под названием total, перебираются все значения в arg и добавляются к total. Затем, по завершении итерации, результат возвращается.
Где Писать Тест
Начать написание теста можно с создания файла test.py, в котором будет содержаться ваш первый тест-кейс. Для тестирования у файла должна быть возможность импортировать ваше приложение, поэтому положите test.py в папку над пакетом. Дерево каталогов будет выглядеть следующим образом:
project/
│
├── my_sum/
│ └── __init__.py
|
└── test.py
Вы заметите, что по мере добавления новых тестов, ваш файл становится все более громоздким и сложным для поддержки, поэтому советуем создать папку tests/ и разделить тесты на несколько файлов. Убедитесь, что названия всех файлов начинаются с test_, чтобы исполнители тестов понимали, что файлы Python содержат тесты, которые нужно выполнить. На больших проектах тесты делят на несколько директорий в зависимости от их назначения или использования.
Примечание: А что есть ваше приложение представляет собой один скрипт?
Вы можете импортировать любые атрибуты скрипта: классы, функции или переменные, с помощью встроенной функции __import__(). Вместо from my_sum import sum напишите следующее:
target = __import__("my_sum.py")
sum = target.sum
При использовании __import__() вам не придется превращать папку проекта в пакет, и вы сможете указать имя файла. Это полезно, если имя файла конфликтует с названиями стандартных библиотек пакетов. Например, если math.py конфликтует с math модулем.
Как Структурировать Простой Тест
Перед написанием тестов, нужно решить несколько вопросов:
- Что вы хотите протестировать?
- Вы пишете модульный тест или интеграционный тест?
Сейчас вы тестируете sum(). Для него можно проверить разные поведения, например:
- Можно ли суммировать список целых чисел?
- Можно ли суммировать кортеж или множество?
- Можно ли суммировать список чисел с плавающей точкой?
- Что будет, если дать на вход плохое значение: одно целое число или строку?
- Что будет, если одно из значений отрицательное?
Проще всего тестировать список целых чисел. Создайте файл test.py со следующим кодом:
import unittest
from my_sum import sum
class TestSum(unittest.TestCase):
def test_list_int(self):
"""
Test that it can sum a list of integers
"""
data = [1, 2, 3]
result = sum(data)
self.assertEqual(result, 6)
if __name__ == '__main__':
unittest.main()
Код в этом примере:
- Импортирует
sum()из пакетаmy_sum(), который вы создали; - Определяет новый класс тест-кейса под названием TestSum, наследующий
unittest.TestCase; - Определяет тестовый метод
.test_list_int()для тестирования целочисленного списка. Метод.test_list_int()сделает следующее
:
- Объявит переменную
dataсо списком значений(1, 2, 3); - Присвоит значение
my_sum.sum(data)переменнойresult; - Определит, что значение result равно 6 с помощью метода
.assertEqual()наunittest.TestCaseклассе.
- Определяет точку входа командной строки, которая запускает исполнителя теста unittest
.main().
Если вы не знаете, что такое self, или как определяется .assertEqual(), то можете освежить знания по объектно-ориентированному программированию с Python 3 Object-Oriented Programming.
Как Писать Утверждения
Последний шаг в написании теста — проверка соответствия выходных данных известным значениям. Это называют утверждением (assertion). Существует несколько общих рекомендаций по написанию утверждений:
- Проверьте, что тесты повторяемы и запустите их несколько раз, чтобы убедиться, что каждый раз они дают одни и те же результаты;
- Проверьте и подтвердите результаты, которые относятся к вашим входным данным — проверьте, что результат действительно является суммой значений в примере
sum().
В unittest есть множество методов для подтверждения значений, типов и существования переменных. Вот некоторые из наиболее часто используемых методов:
| Метод | Эквивалент |
|---|---|
| .assertEqual(a, b) | a == b |
| .assertTrue(x) | bool(x) is True |
| .assertFalse(x) | bool(x) is False |
| .assertIs(a, b) | a is b |
| .assertIsNone(x) | x is None |
| .assertIn(a, b) | a in b |
| .assertIsInstance(a, b) | isinstance(a, b) |
У .assertIs(), .assertIsNone(), .assertIn(), and .assertIsInstance() есть противоположные методы, называемые .assertIsNot() и тд.
Побочные эффекты
Писать тесты сложнее, чем просто смотреть на возвращаемое значение функции. Зачастую, выполнение кода меняет другие части окружения: атрибуты класса, файлы файловой системы, значения в базе данных. Это важная часть тестирования, которая называется побочные эффекты. Решите, тестируете ли вы побочный эффект до того, как включить его в список своих утверждений.
Если вы обнаружили, что в блоке кода, который вы хотите протестировать, много побочных эффектов, значит вы нарушаете Принцип Единственной Ответственности. Нарушение принципа единственной ответственности означает, что фрагмент кода делает слишком много вещей и требует рефакторинга. Следование принципу единственной ответственности — отличный способ проектирования кода, для которого не составит труда писать простые повторяемые модульные тесты, и, в конечном счете, создания надежных приложений.
Запуск Первого Теста
Вы создали первый тест и теперь нужно попробовать выполнить его. Понятно, что он будет пройден, но перед созданием более сложных тестов, нужно убедиться, что даже такие тесты выполняются успешно.
Запуск Исполнителей Тестов
Исполнитель тестов — приложение Python, которое выполняет тестовый код, проверяет утверждения и выдает результаты тестирования в консоли. В конец test.py добавьте этот небольшой фрагмент кода:
if __name__ == '__main__':
unittest.main()
Это точка входа командной строки. Если вы выполните этот скрипт, запустив python test.py в командной строке, он вызовет unittest.main(). Это запускает исполнителя тестов, обнаруживая все классы в этом файле, наследуемые из unittest.TestCase.
Это один из многих способов запуска исполнителя тестов unittest. Если у вас есть единственный тестовый файл с названием test.py, вызов python test.py — отличный способ начать работу.
Другой способ — использовать командную строку unittest. Попробуем:
$ python -m unittest test
Это исполнит тот же самый тестовый модуль (под названием test) через командную строку. Можно добавить дополнительные параметры для изменения выходных данных. Один из них -v для многословности (verbose). Попробуем следующее:
$ python -m unittest -v test
test_list_int (test.TestSum) ... ok
----------------------------------------------------------------------
Ran 1 tests in 0.000s
Мы исполнили один тест из test.py и вывели результаты в консоль. Многословный режим перечислил имена выполненных тестов и результаты каждого из них.
Вместо предоставления имени модуля, содержащего тесты, можно запросить авто-обнаружение при помощи следующего:
$ python -m unittest discover
Эта команда будет искать в текущей директории файлы с test*.py в названии, чтобы протестировать их.
При наличии нескольких тестовых файлов и соблюдении шаблона наименования test*.py, можно передать имя директории при помощи -s флага и названия папки.
$ python -m unittest discover -s tests
unittest запустит все тесты в едином тестовом плане и выдаст результаты.
Наконец, если ваш исходный код находится не в корневом каталоге, а в подкаталоге, например в папке с названием src/, можно с помощью -t флага сообщить unittest, где выполнять тесты, для корректного импорта модулей:
$ python -m unittest discover -s tests -t src
unittest найдет все файлы test*.py в директории src/ внутри tests, а затем выполнит их.
Понимание Результатов Тестирование
Это был очень простой пример, где все прошло успешно, поэтому попробуем понять выходные данные проваленного теста.
sum() должен принимать на вход другие списки числового типа, например дроби.
К началу кода в файле test.py добавьте выражение для импорта типа Fraction из модуля fractions стандартной библиотеки.
from fractions import Fraction
Теперь добавим тест с утверждением, ожидая некорректное значение. В нашем случае, ожидаем, что сумма ¼, ¼ и ⅖ будет равна 1:
import unittest
from my_sum import sum
class TestSum(unittest.TestCase):
def test_list_int(self):
"""
Test that it can sum a list of integers
"""
data = [1, 2, 3]
result = sum(data)
self.assertEqual(result, 6)
def test_list_fraction(self):
"""
Test that it can sum a list of fractions
"""
data = [Fraction(1, 4), Fraction(1, 4), Fraction(2, 5)]
result = sum(data)
self.assertEqual(result, 1)
if __name__ == '__main__':
unittest.main()
Если вы запустите тесты повторно с python -m unittest test, получите следующее:
$ python -m unittest test
F.
======================================================================
FAIL: test_list_fraction (test.TestSum)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 21, in test_list_fraction
self.assertEqual(result, 1)
AssertionError: Fraction(9, 10) != 1
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
В этих выходных данных вы видите следующее:
- В первой строке указаны результаты выполнения всех тестов: один проваленный (F), один пройденный (.);
- FAIL показывает некоторые детали проваленного теста:
- Название тестового метода (
test_list_fraction); - Тестовый модуль (
test) и тест-кейс (TestSum); - Трейсбек строки с ошибкой;
- Детали утверждения с ожидаемым результатом (1) и фактическим результатом (Fraction(9, 10))
Помните, можно добавить дополнительную информацию к выходным данным теста с помощью флага -v к команде python -m unittest.
Запуск тестов из PyCharm
Если вы используете PyCharm IDE, то можете запустить unittest или pytest, выполнив следующие шаги:
- В окне Project tool, выберите директорию tests.
- В контекстном меню выберите команду запуска unittest. Например, ‘Unittests in my Tests…’.
Это выполнит unittest в тестовом окне и выдаст результаты в PyCharm:
Больше информации доступно на сайте PyCharm.
Запуск Тестов из Visual Studio Code
Если вы пользуетесь Microsoft Visual Studio Code IDE, поддержка unittest, nose и pytest уже встроена в плагин Python.
Если он у вас установлен, можно настроить конфигурацию тестов, открыв Command Palette по Ctrl+Shift+P и написав “Python test”. Вы увидите список вариантов:
Выберите Debug All Unit Tests, после чего VSCode отправит запрос для настройки тестового фреймворка. Кликните по шестеренке для выбора исполнителя тестов (unittest) и домашней директории (.).
По завершении настройки, вы увидите статус тестов в нижней части экрана и сможете быстро получить доступ к тестовым логам и повторно запустить тесты, кликнув по иконкам:
Видим, что тесты выполняются, но некоторые из них провалены.
THE END
В следующей части статьи мы рассмотрим тесты для фреймворков, таких как Django и Flask.
Ждём ваши вопросы и комментарии тут и, как всегда, можно зайти к Станиславу на день открытых дверей.
Вторая часть






