Pytest is currently the most popular Testing Framework for the very popular Python Programming Language
if you haven’t done so already install the latest Python from “https://www.python.org/downloads/”
PyCharm is the best IDE for Python. Having a good IDE makes you more productive as a developer, tester or integrator. PyCharm has a paid version, but the “Community version” will probably have all the functions you need for now. Install the “Community Version” for this tutorial. PyCharm is available for most Operating Systems like Windows, Linux and Mac-OS. If you have it installed already, please go to the PyCharm website “https://www.jetbrains.com/pycharm/” for instructions to install it. Then continue on with this tutorial.
From the File menu.Create a PyCharm project called pytestProject.
The Pytest default project with a default file
Use PyCharm install to Pytest for this Project and others later on.
In PyCharm change default test runner to pytest by
File>Setting->Preferences>Tools>Python Integrated Tools
Chose your testing framework
Change Default the test runner from unittest to pytest
Create a pytest test
From the main menu. File > New choose Python file then type in Car.py and return
Notice you created an empty file called Car.py in the pytestProject
Enter this Code into the Car.py file
https://gist.github.com/allenabc/656db8897c2f65188774683a1ebc2933
Next from the File menu > New > Directory, name it test
~/PycharmProjects/pytestProject/test
Next create a pytest test
Open the Car.py file and scroll down to the line with def brake()
Highlight the line, right click, click on Go To when opens another Pop Up
Scroll all the way down to “Test” click on Test and another two line popup will appear
Choose the blue Create New Test button
This Dialog will appear
Following tests should be separate from source, we want the tests in the test folder so click the small folder icon to put the tests in the test directory. Click OK and a file called test_car.py file is created in the test directory with two lines. Replace those lines with this code.
from Car import Car
def test_car_brake():
car = Car(50)
car.brake()
assert car.speed == 45
To begin with we import the code for Car. Next, the test initializes the Car to run at 50 MPH. We can tell from the source code when we press the brake once the speed will drop 5 MPH. The assert expects the speed to be reduced to 45 MPH. The test should pass.
The test can be run inside PyCharm. Notice the test_car_brake function has a green arrow. Press the arrow or type CTL-Shift-f10. The logging screen pops up with results
…
test_Car.py::test_car_brake PASSED [100%]
…
To prove the negative, edit the test_car.py file and change 45 to 40 to see if it fails
…
test_Car.py:3 (test_car_brake)
45 != 40
Expected :40
Actual :45
…
PyTest generates logging to show the test fails and why.
Exploring Test Fixtures
Some older testing frameworks like xunit, had functions called setup and teardown. Before and/or after each test, a function need would execute to create an environment to start or end the test. Assuming all the individual tests needed the same setup, the code didn’t have to be repeated for every test in a series.
PyTest has a similar feature called a Fixture.
Please replace all the code in test_Car.py with this gist code.
https://gist.github.com/allenabc/1353d0f68708409bce6a87c33cea0781
Notice the decorator used to setup every test
@pytest.fixture decorator points to a two line function that initializes my_car to 50 MPH to start each test. We know they are tests because they start with test_. The two tests are called with the test fixture my_car(). my_car initializes the car to a speed of 50 MPH.
Exploring Test parameterization.
Many tests require a series of parameters.
This decorator @pytest.mark.parametrize feeds every element of the array to a test.
Replace the code of test_Car.py with this code.
https://gist.github.com/allenabc/7eaedbb34cd05e15345b13c06bf668e1
speed_data = {45, 50, 55, 100} is a list of elements to be converted to test parameters
Since speed_data has four values, the test is going to be run four times. Since the speed of the car is always set to the same speed (50), at best only one of four tests will pass.
You have done it. You installed Python, PyCharm. Started a new Project. Entered some sample code. Created a test directory and added your first test. You had PyCharm run the test and log the results.