Acquaintance with testing in Python. Part 1

 
3r3-31. All good! 3r? 31304.  
3r? 31304.  
From our table to yours
That is, from our course “Python Developer”, despite the rapidly approaching New Year, we prepared for you an interesting translation about various testing methods in Python. 3r? 31304.  
3r? 31304.  
This guide is for those who have already written a cool application in Python, but have not written for
yet.  
tests. 3r? 31304.  
3r? 31304.  
Testing in Python is an extensive topic with a lot of subtleties, but it is not necessary to complicate things. In a few simple steps you can create simple tests for the application, gradually increasing the complexity based on them. 3r? 31304.  
3r? 31304.  
In this guide, you will learn how to create a basic test, run it, and find all the bugs before users do it! You will learn about the tools available to write and run tests, check the performance of the application, and even look at security issues. 3r? 31304.  
3r? 31304.  
Acquaintance with testing in Python. Part 1
3r? 31304.  
3r? 31304.  
3r31262. Testing Code 3r31263. 3r? 31304.  
3r? 31304.  
You can test the code in different ways. In this tutorial, you will learn from simple to advanced methods. 3r? 31304.  
3r? 31304.  
3r31262. Automated vs. Manual Testing 3r31263. 3r? 31304.  
3r? 31304.  
Good news! Most likely you have already done the test, but have not yet realized this. Remember how you first launched the app and used it? Have you checked the features and experimented with them? This process is called exploratory testing, and is a form of manual testing. 3r? 31304.  
3r? 31304.  
Research testing - testing that is conducted without a plan. During research testing, you explore the application. 3r? 31304.  
3r? 31304.  
To create a complete list of manual tests, it is enough to make a list of all the functions of the application, the various types of input that it accepts, and the expected results. Now, every time you change something in the code, you need to re-check each of the elements of this list. 3r? 31304.  
3r? 31304.  
Sounds bleak, right? 3r? 31304.  
3r? 31304.  
Therefore, we need automatic tests. Automatic testing - the execution of a test plan (parts of an application that require testing, the order in which they are tested and expected results) using a script, and not by human hands. Python already has a set of tools and libraries to help you create automated tests for your application. Consider these tools and libraries in our tutorial. 3r? 31304.  
3r? 31304.  
3r31262. Modular Tests VS. Integration Tests [/b] 3r? 31304.  
3r? 31304.  
The world of testing is full of terms, and now, knowing the difference between manual and automated testing, we will go down a level deeper. 3r? 31304.  
Think about how you can test the headlights of a car? You turn on the headlights (let's call it the testing step), get out of the car yourself, or ask a friend to check that the headlights are lit (and this is a test judgment). Testing multiple components is called integration testing. 3r? 31304.  
3r? 31304.  
Think of all the things that need to work properly for a simple task to produce the correct result. These components are similar to parts of your application: all those classes, functions, modules that you wrote. 3r? 31304.  
3r? 31304.  
The main difficulty of integration testing occurs when the integration test does not give the correct result. It is difficult to assess the problem without being able to isolate the broken part of the system. If the lights are not lit, the bulbs may be broken. Or maybe the battery is dead? Or maybe the problem is in the generator? Or even a failure in the computer machine? 3r? 31304.  
3r? 31304.  
Modern cars themselves will notify you of the failure of light bulbs. This is determined by the unit test. 3r? 31304.  
3r? 31304.  
The unit test (unit test) is a small test that checks the correctness of a separate component. A unit test helps isolate a breakdown and fix it faster. 3r? 31304.  
3r? 31304.  
We talked about two types of tests: 3r3-331304.  
3r? 31304.  
3r31234.  
3r31239. Integration test, verifying the components of the system and their interaction with each other; 3r31240.  
3r31239. A unit test that checks a single component of an application 3r31240.  
3r31239. You can create both tests in Python. To write a test for the built-in function sum (), you need to compare the output of sum () with known values. 3r31240.  
3r31242. 3r? 31304.  
3r? 31304.  
For example, this is how you can verify that the sum of the numbers (? ? 3) is 6:
 
3r? 31304.  
3r31159. assert sum ([1, 2, 3]) == ? "Should be 6" 3r31175. 3r? 31304.  
3r? 31304.  
Values ​​are correct, so nothing will be displayed in the REPL. If the result is 3r31218. sum () 3r31219. incorrect, will be issued Assertionerror with the message “Should be 6” (“Should be 6”). Check the assertion statement again, but now with incorrect values, to get 3r31218. Assertionerror :
 
3r? 31304.  
3r31159. assert sum ([1, 1, 1]) == ? "Should be 6"
Traceback (most recent call last):
File "3r3147.", Line ? in
Assertionerror: Should be 6
3r31175. 3r? 31304.  
3r? 31304.  
In the REPL you will see 3r31218. Assertionerror , since the value of sum () 3r31219. not equal to 6. 3r3-331304.  
Instead of REPL, put this in a new Python file called 3r31218. test_sum.py
and run it again:
 
3r? 31304.  
3r31159. def test_sum ():
assert sum ([1, 2, 3]) == ? "Should be 6"
if __name__ == "__main__":
test_sum ()
print ("Everything passed") 3r31219. 3r31175. 3r? 31304.  
3r? 31304.  
Now you have a written test case (test case), statement and entry point (command line). This can now be done from the command line:
 
3r? 31304.  
3r31159. $ python test_sum.py
Everything passed
3r31175. 3r? 31304.  
3r? 31304.  
You see a successful result, “Everything passed”. 3r? 31304.  
3r31218. sum () 3r31219. in Python, it takes as input any iterable as the first argument. You checked the list. Let's try to test the tuple. Create a new file named 3r31218. test_sum_2.py
with the following code:
 
3r? 31304.  
3r31159. def test_sum ():
assert sum ([1, 2, 3]) == ? "Should be 6"
def test_sum_tuple ():
assert sum ((? ? 2)) == ? "Should be 6"
if __name__ == "__main__":
test_sum ()
test_sum_tuple ()
print ("Everything passed")
3r31219. 3r31175. 3r? 31304.  
Doing 3r31218. test_sum_2.py
, the script will generate an error, since s um () (? ? 2) should be equal to ? not 6. As a result, the script displays an error message, a line of code and a traceback:
 
3r? 31304.  
3r31159. $ python test_sum_2.py
Traceback (most recent call last):
File "test_sum_2.py", line ? in
test_sum_tuple ()
File "test_sum_2.py", line ? in test_sum_tuple
assert sum ((? ? 2)) == ? "Should be 6"
Assertionerror: Should be 6
3r31175. 3r? 31304.  
3r? 31304.  
You can see how an error in the code causes an error in the console with information about where it occurred and what the expected result was. 3r? 31304.  
3r? 31304.  
Such tests are suitable for simple verification, but what if there are more errors than in one? Test runners come to the rescue. The test runner is a special application designed for conducting tests, checking output data and providing tools for debugging and diagnosing tests and applications. 3r? 31304.  
3r? 31304.  
3r31262. Selection of Test Artist 3r31263. 3r? 31304.  
3r? 31304.  
For Python there are many performers of tests available. For example, unittest is built into the standard Python library. In this tutorial, we will use test cases and unittest test executers. The principles of unittest are easily adaptable for other frameworks. We list the most popular test performers: 3r3-31304.  
3r? 31304.  
3r31184.  
3r31239. unittest; 3r31240.  
3r31239. nose or nose2; 3r31240.  
3r31239. pytest. 3r31240.  
3r31192. 3r? 31304.  
3r? 31304.  
It is important to choose a test performer that meets your requirements and experience. 3r? 31304.  
3r? 31304.  
3r31262. unittest 3r31263. 3r? 31304.  
3r? 31304.  
unittest is built into the standard Python library since version 2.1. You will surely come across it in commercial Python applications and open source projects. 3r? 31304.  
In unittest there is a test framework and a test performer. When writing and executing tests you need to follow some important requirements. 3r? 31304.  
3r? 31304.  
unittest requires:
 
3r? 31304.  
3r31184.  
3r31239. Put tests in classes as methods; 3r31240.  
3r31239. Use special approval methods. Class TestCase instead of the usual assert expression. 3r31240.  
3r31192. 3r? 31304.  
3r? 31304.  
To turn a previously written example into a unittest test case, you need:
 
3r? 31304.  
3r31234.  
3r31239. Import unittest from standard library; 3r31240.  
3r31239. Create a class called 3r31218. TestSum 3r31219. which will inherit the class TestCase 3r31219. ; 3r31240.  
3r31239. Convert test functions to methods by adding 3r31218. self
as the first argument; 3r31240.  
3r31239. Modify assertions by adding the use of 3r31218. self.assertEqual () method in class 3r31218. TestCase 3r31219. ; 3r31240.  
3r31239. Change the entry point on the command line to call 3r31218. unittest.main () . 3r31240.  
3r31242. 3r? 31304.  
3r? 31304.  
Following these steps, create a new test_sum_unittest.py file with this code:
 
3r? 31304.  
3r31159. import unittest
class TestSum (unittest.TestCase):
def test_sum (self):
self.assertEqual (sum ([1, 2, 3]), ? "Should be 6")
def test_sum_tuple (self):
self.assertEqual (sum ((? ? 2)), ? "Should be 6")
if __name__ == '__main__':
unittest.main ()
3r31175. 3r? 31304.  
3r? 31304.  
Doing this on the command line will result in one successful completion (indicated by.) And one unsuccessful (indicated by F):
 
3r? 31304.  
3r31159. $ python test_sum_unittest.py
.F
================================================= ====================
FAIL: test_sum_tuple (__main __. TestSum)
-------------------------------------------------- --------------------
Traceback (most recent call last):
File "test_sum_unittest.py", line ? in test_sum_tuple
self.assertEqual (sum ((? ? 2)), ? "Should be 6")
Assertionerror: Should be 6
-------------------------------------------------- --------------------
Ran 2 tests in ???s
FAILED (failures = 1)
3r31219. 3r31175. 3r? 31304.  
Thus, you performed two tests with the help of the unit test performer. 3r? 31304.  
3r? 31304.  
Note: If you are writing test cases for Python 2 and ? be careful. In versions of Python 2.7 and below, unittest is called unittest 2. When you import from unittest, you will get different versions with different functions in Python 2 and Python 3. 3r? 31304.  
3r? 31304.  
To learn more about the unittest, read 3-33-348. unittest documentation . 3r? 31304.  
3r? 31304.  
3r31262. nose [/b] 3r? 31304.  
3r? 31304.  
Over time, after writing hundreds or even thousands of tests for an application, it becomes increasingly difficult to understand and use unittest output data. 3r? 31304.  
3r? 31304.  
Nose is compatible with all tests written with the unittest framework, and can be replaced by a test performer. The development of nose, as an open source application, began to slow down, and nose2 was created. If you start from scratch, it is recommended to use nose2. 3r? 31304.  
3r? 31304.  
To get started with nose? you need to install it from PyPl and run it on the command line. nose2 will try to find all test squeaks with  3r31218. test * .py 3r31219. in the title and all test cases inherited from unittest.TestCase in your current directory:
 
3r? 31304.  
3r31159. $ pip install nose2
$ python -m nose2
.F
================================================= ====================
FAIL: test_sum_tuple (__main __. TestSum)
-------------------------------------------------- --------------------
Traceback (most recent call last):
File "test_sum_unittest.py", line ? in test_sum_tuple
self.assertEqual (sum ((? ? 2)), ? "Should be 6")
Assertionerror: Should be 6
-------------------------------------------------- --------------------
Ran 2 tests in ???s
FAILED (failures = 1)
3r31175. 3r? 31304.  
3r? 31304.  
This is how the test created in r3r31218 is executed. test_sum_unittest.py
, from the performer of nose2 tests. nose2 provides many command line flags for filtering executable tests. To learn more, we recommend that you familiarize yourself with Nose 2 . 3r? 31304.  
3r? 31304.  
3r31262. pytest [/b] 3r? 31304.  
3r? 31304.  
pytest supports unittest test cases. But the real advantage of pytest is its test cases. Pytest test cases - a series of functions in a Python file with test_ at the beginning of the name. 3r? 31304.  
3r? 31304.  
It has other useful features:
 
3r? 31304.  
3r31184.  
3r31239. Support for assert built-in expressions instead of using special self.assert * () methods; 3r31240.  
3r31239. Support filtering test cases; 3r31240.  
3r31239. The ability to restart from the last failed test; 3r31240.  
3r31239. An ecosystem of hundreds of plug-ins that extend functionality. 3r31240.  
3r31192. 3r? 31304.  
3r? 31304.  
An example of a TestSum test case for pytest would look like this:
 
3r? 31304.  
3r31159. def test_sum ():
assert sum ([1, 2, 3]) == ? "Should be 6"
def test_sum_tuple ():
assert sum ((? ? 2)) == ? "Should be 6"
3r31175. 3r? 31304.  
3r? 31304.  
You got rid of TestCase, using classes and command line entry points. 3r? 31304.  
More information can be found at Pytest Documentation Website. . 3r? 31304.  
3r? 31304.  
3r31262. Writing the First Test [/b] 3r? 31304.  
3r? 31304.  
Let's unite everything that we already learned, and instead of the built-in function 3r31218. sum () 3r31219. Let's test a simple implementation with the same requirements. 3r? 31304.  
3r? 31304.  
Create a new folder for the project, inside which create a new folder called my_sum. Inside my_sum, create an empty file called _init_.py . The presence of this file means that the my_sum folder can be imported as a module from the parent directory. 3r? 31304.  
3r? 31304.  
The folder structure will look like this:
 
3r? 31304.  
3r31218. project /3r33031304.  

 
My── my_sum /
 
── __init__.py 3r? 31304.  
3r? 31304.  
Open 3r31218. my_sum /__ init__.py and create a new function called sum () 3r31219. which takes as input iterated (list, tuple, set) and adds values. 3r? 31304.  
3r? 31304.  
3r31159. def sum (arg):
total = 0
for val in arg:
total + = val
Return total
3r31175. 3r? 31304.  
3r? 31304.  
This example creates a variable called total , enumerates all the values ​​in 3r31218. arg 3r31219. and added to 3r31218. total
. Then, at the end of the iteration, the result is returned. 3r? 31304.  
3r? 31304.  
3r31262. Where to Write a Test 3r31263. 3r? 31304.  
3r? 31304.  
You can start writing a test by creating a 3r31218 file. test.py 3r31212. which will contain your first test case. For testing, the file should have the ability to import your application, so put test.py 3r31212. to the folder above the package. The directory tree will look like this:
 
3r? 31304.  
3r31218. project /3r33031304.  

 
My── my_sum /
 
│ └── __init__.py
 
| 3r? 31304.  
Test── test.py
3r? 31304.  
3r? 31304.  
You will notice that as you add new tests, your file becomes more cumbersome and difficult to maintain, so we advise you to create a folder. tests /3r31219. and split the tests into several files. Make sure all file names start with 3r312318. test_ 3r31219. so that test workers understand that Python files contain tests that need to be performed. On large projects, tests are divided into several directories depending on their purpose or use. 3r? 31304.  
3r? 31304.  
Note: And what is your application is a single script? 3r? 31304.  
You can import any script attributes: classes, functions, or variables using the built-in function 3r31218. __import __ ()
. Instead of from my_sum import sum write the following: 3r3649. 3r? 31304.  
3r? 31304.  
3r31159. target = __import __ ("my_sum.py")
sum = target.sum
3r31175. 3r? 31304.  
3r? 31304.  
[i] When using 3r31218. __import __ () you do not have to turn the project folder into a package, and you can specify the file name. This is useful if the file name conflicts with the names of the standard package libraries. For example, if 3r31218. math.py conflicts with the math module.
3r? 31304.  
3r? 31304.  
3r31262. How to Structure the Simple Test [/b] 3r? 31304.  
3r? 31304.  
Before writing tests, you need to solve a few questions: 3r3-331304.  
3r? 31304.  
3r31234.  
3r31239. What do you want to test? 3r31240.  
3r31239. Are you writing a unit test or integration test? 3r31240.  
3r31242. 3r? 31304.  
3r? 31304.  
Now you are testing 3r31218. sum () 3r31219. . For it, you can test different behaviors, for example:
 
3r? 31304.  
3r31184.  
3r31239. Is it possible to sum up the list of integers? 3r31240.  
3r31239. Is it possible to sum up a tuple or a set? 3r31240.  
3r31239. Is it possible to sum up the list of floating point numbers? 3r31240.  
3r31239. What happens if you give a bad value to input: a single integer or a string? 3r31240.  
3r31239. What happens if one of the values ​​is negative? 3r31240.  
3r31192. 3r? 31304.  
3r? 31304.  
The easiest way to test a list of integers. Create the file 3r31218. test.py 3r31212. with the following code:
 
3r? 31304.  
3r31159. 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 ()
3r31175. 3r? 31304.  
3r? 31304.  
The code in this example is
 
3r? 31304.  
3r31184.  
3r31239. Imports sum () 3r31219. from pack 3r31218. my_sum () that you created; 3r31240.  
3r31239. Defines a new test case class called TestSum, inheriting from 3r31218. unittest.TestCase ; 3r31240.  
3r31239. Defines the test method 3r31218. .test_list_int () for testing an integer list. Method 3r31218. .test_list_int () will do the following 3r31240.  
3r31192. :
 
3r? 31304.  
3r31234.  
3r31239. Declare a variable data with a list of the values ​​of 3r31218. (? ? 3) 3r31219. ; 3r31240.  
3r31239. Assigns the value 3r31218. my_sum.sum (data) variable result ; 3r31240.  
3r31239. Determine that the result value is 6 using the method. .assertEqual () on 3r31218. unittest.TestCase classroom. 3r31240.  
3r31242. 3r? 31304.  
3r31184.  
3r31239. Specifies the entry point for the command line that launches the unit test runner unittest .main () . 3r31240.  
3r31192. 3r? 31304.  
3r? 31304.  
If you do not know what self is, or as defined by 3r31218. .assertEqual () , then you can refresh your knowledge of object-oriented programming from 3r3804. Python 3 Object-Oriented Programming 3r3-31307. . 3r? 31304.  
3r? 31304.  
3r31262. How to Write Statements [/b] 3r? 31304.  
3r? 31304.  
The final step in writing a test is to check that the output matches the known values. This is called assertion. There are several general guidelines for writing statements: 3r3-331304.  
3r31184.  
3r31239. Check that the tests are repeatable and run them several times to make sure that they give the same results each time; 3r31240.  
3r31239. Check and confirm the results that relate to your input data - check that the result is indeed the sum of the values ​​in example 3r31218. sum () 3r31219. . 3r31240.  
3r31192. 3r? 31304.  
3r? 31304.  
In unittest, there are many methods for validating the values, types, and existence of variables. Here are some of the most commonly used methods:
 
3r? 31304.  
3r33837.  
.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) .assertIsNot () etc. 3r? 31304.  
3r? 31304.  
3r31262. Side effects [/b] 3r? 31304.  
3r? 31304.  
Writing tests is more difficult than just looking at the return value of a function. Often, code execution changes other parts of the environment: class attributes, filesystem files, values ​​in the database. This is an important part of testing, which is called side effects. Decide whether you are testing a side effect before including it in your list of products.beliefs. 3r? 31304.  
3r? 31304.  
If you find that there are a lot of side effects in the block of code you want to test, then you are breaking 3r3393948. The principle of sole responsibility . Violation of the principle of sole responsibility means that a piece of code does too many things and requires refactoring. Following the principle of sole responsibility is a great way to design code for which it is easy to write simple, repeatable unit tests, and, ultimately, create reliable applications. 3r? 31304.  
3r? 31304.  
3r31262. Run First Test 3r31263. 3r? 31304.  
3r? 31304.  
You have created the first test and now you need to try it out. It is clear that it will be passed, but before creating more complex tests, you need to make sure that even such tests are performed successfully. 3r? 31304.  
3r? 31304.  
3r31262. Running Test Performers 3r31263. 3r? 31304.  
3r? 31304.  
The test runner is a Python application that executes test code, validates assertions, and issues test results in the console. At the end of test.py, add this small code snippet:
 
3r? 31304.  
3r31159. if __name__ == '__main__':
unittest.main ()
3r31175. 3r? 31304.  
3r? 31304.  
This is the command line entry point. If you run this script, run python test.py 3r31212. on the command line, he will call unittest.main () . This launches the test executer, detecting all classes in this file inherited from 3r31218. unittest.TestCase . 3r? 31304.  
3r? 31304.  
This is one of the many ways to run the unittest executor. If you have a single test file called test.py 3r31212. Calling python test.py is a great way to get started. 3r? 31304.  
3r? 31304.  
Another way is to use the unittest command line. Let's try:
 
3r? 31304.  
3r31159. $ python -m unittest test 3r31175. 3r? 31304.  
3r? 31304.  
This will execute the same test module (called 3r31218. Test
) Via the command line. You can add additional parameters to change the output. One of them is -v for verbose. Let's try the following:
 
3r? 31304.  
3r31159. $ python -m unittest -v test
test_list_int (test.TestSum) ok
-------------------------------------------------- --------------------
Ran 1 tests in ???s
3r31219. 3r31175. 3r? 31304.  
We ran one test from test.py and output the results to the console. Verbose mode lists the names of the tests performed and the results of each one. 3r? 31304.  
3r? 31304.  
Instead of providing the name of the module containing the tests, you can request auto-detection using the following: 3r31-3030.  
3r31159.
$ python -m unittest discover
3r31175. 3r? 31304.  
3r? 31304.  
This command will search in the current directory for files with 3r312318. test * .py 3r31219. in the title to test them. 3r? 31304.  
3r? 31304.  
If there are several test files and the pattern name is 3r31218. test * .py 3r31219. , you can pass the directory name with the -s flag and the folder name. 3r? 31304.  
3r? 31304.  
3r31159. $ python -m unittest discover -s tests 3r31175. 3r? 31304.  
3r? 31304.  
unittest will run all tests in a single test plan and produce results. 3r? 31304.  
Finally, if your source code is not in the root directory, but in a subdirectory, for example, in a folder called src /, you can use the -t flag to tell unittest where to run the tests to correctly import the modules:
 
3r? 31304.  
3r31159. $ python -m unittest discover -s tests -t src 3r31175. 3r? 31304.  
3r? 31304.  
unittest will find all files test * .py 3r31219. in the 3r31218 directory. src /3r31219. inside 3r31218. tests and then execute them. 3r? 31304.  
3r? 31304.  
3r31262. Understanding the Results of Testing 3r3-331304.  
3r31263. 3r? 31304.  
It was a very simple example where everything went well, so let's try to understand the output of the failed test. 3r? 31304.  
3r? 31304.  
3r31218. sum () 3r31219. must accept other lists of numeric type, for example, fractions. 3r? 31304.  
3r? 31304.  
Top of the code in the file 3r31218. test.py 3r31212. Add an expression to import the type Fraction from module 3r31218. fractions
standard library. 3r? 31304.  
3r? 31304.  
3r31159. from fractions import Fraction 3r31175. 3r? 31304.  
3r? 31304.  
Now we add a test with an assertion, expecting an incorrect value. In our case, we expect that the sum of ¼, ¼ and ⅖ will be equal to 1:
 
3r? 31304.  
3r31159. 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 be 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 ()
3r31219. 3r31175. 3r? 31304.  
If you run the tests again with python -m unittest test, get the following:
 
3r? 31304.  
3r31159. $ python -m unittest test
F.
================================================= ====================
FAIL: test_list_fraction (test.TestSum)
-------------------------------------------------- --------------------
Traceback (most recent call last):
File "test.py", line 2? in test_list_fraction
self.assertEqual (result, 1)
Assertionerror: Fraction (? 10)! = 1
-------------------------------------------------- --------------------
Ran 2 tests in ???s
FAILED (failures = 1)
3r31175. 3r? 31304.  
3r? 31304.  
In this output, you see the following:
 
3r? 31304.  
3r31184.  
3r31239. The first line shows the results of all tests: one failed (F), one passed (.); 3r31240.  
3r31239. FAIL shows some details of the failed test: 3r312340.  
3r31192. 3r? 31304.  
3r31234.  
3r31239. The name of the test method ( Test_list_fraction ); 3r31240.  
3r31239. Test module ( Test ) And test case ( TestSum ); 3r31240.  
3r31239. Treysbek line with an error; 3r31240.  
3r31239. Details of the statement with the expected result (1) and the actual result (Fraction (? 10)) 3r31240.  
3r31242. 3r? 31304.  
Remember, you can add additional information to the test output with the -v flag to the command 3r31218. python -m unittest
. 3r? 31304.  
3r? 31304.  
3r31262. Run tests from PyCharm [/b] 3r? 31304.  
3r? 31304.  
If you are using a PyCharm IDE, you can run unittest or pytest by following these steps:
 
3r? 31304.  
3r31234.  
3r31239. In the Project tool window, select the tests directory. 3r31240.  
3r31239. In the context menu, select the command start unittest. For example, tests Unittests in my Tests… ’. 3r31240.  
3r31242. 3r? 31304.  
3r? 31304.  
This will execute unittest in a test window and display the results in PyCharm:
 
3r? 31304.  
3r31251. 3r? 31304.  
3r? 31304.  
More information is available at Site PyCharm . 3r? 31304.  
3r? 31304.  
3r31262. Running Tests from Visual Studio Code [/b] 3r? 31304.  
3r? 31304.  
If you are using Microsoft Visual Studio Code IDE, unittest, nose, and pytest support is already built into the Python plugin. 3r? 31304.  
3r? 31304.  
If you have it installed, you can customize the test configuration by opening Command Palette with Ctrl + Shift + P and writing “Python test”. You will see a list of options:
 
3r? 31304.  
3r31276. 3r? 31304.  
3r? 31304.  
Select Debug All Unit Tests, after which VSCode will send a request to set up a test framework. Click on the gear to select the unit test performer and home directory (.). 3r? 31304.  
3r? 31304.  
After the setup is completed, you will see the status of the tests at the bottom of the screen and you can quickly access the test logs and rerun the tests by clicking on the icons: 3r331304.  
3r? 31304.  
3r31289. 3r? 31304.  
3r? 31304.  
We see that tests are performed, but some of them failed. 3r? 31304.  
3r? 31304.  
THE END
 
3r? 31304.  
In the next part of the article we will look at tests for frameworks such as Django and Flask. 3r? 31304.  
3r? 31304.  
We are waiting for your questions and comments here and, as always, you can go to Stanislav at Open Day . 3r31315.
3r31312. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () ();
3r31315.
+ 0 -

Add comment