Test your first Python application
Choosing the test runner
To explicitly set the required test runner in the project settings, press Control+Alt+S to open the IDE settings and select Tools | Python Integrated Tools, and then select the target test runner from the Default test runner list.
For more information, refer to Testing frameworks.
A quick way to create tests is to have PyCharm stub them out from the class we’d like to test. To do this, we need to open car.py, then right-click the name of the class, point to , and then choose (or just press Control+Shift+T):
A popup appears that suggests creating a new test:
OK, let’s do it. We are going to test whether our car is able to accelerate and brake, so let's select those checkboxes:
A new Python test class is created:
You can run the test by clicking the icon in the gutter next to the class definition. A Run/Debug configuration will be created automatically: However, we can see that the test fails by default:
Now we know that we can run tests, let’s start writing some actual test code.
How to write unit tests is out of scope for this article. If you’re interested in learning about using the `unittest` framework, you can check out their docs.
For our example let’s use these tests:
Now let's run the tests. Right-click the test_сar.py editor tab and choose Run 'Python tests in test_car.py':
This time almost all tests have passed successfully:
Let's look closer at the test code and debug the test that has failed. Select the failed test in the left pane of the Run tool window, then find the line number where the error has occurred in the right pane:
Click the line number in the editor to set the breakpoint:
Next, launch a debugger session. To do that, right-click the editor background at the method
test_initial_average_speed and choose Debug from the context menu:
The Debug tool window opens:
Click the button to go into the class
Car, and then expand the self node in the Debug tool window.
The debugger has highlighted the line that will be executed next (
return self.odometer / self.time), and in the Debug tool window we can see that
self.speed equals 0:
This will lead to an exception on the next step, because division by zero is impossible. Let's fix that by adding some code to the class
self.odometer / self.timein the editor.
Chooseon the main menu or on the context menu, or press Control+Alt+V.
Type the name of the variable, for example
Select the statement
avg_speed = self.odometer / self.time, press Control+Alt+T ( ), and then select if to add a condition to the statement.
PyCharm creates a stub
We shouldn't divide by
self.timeif it equals 0. So, type
self.time != 0to specify the condition.
Then specify that
avg_speedshould equal zero when
self.timeis zero. Here's what you should get:def average_speed(self): if self.time != 0: avg_speed = self.odometer / self.time else: avg_speed = 0 return avg_speed
Switch to test_car.py and run the tests again:
All tests have passed.
Running tests automatically
In the last paragraph, we reran our tests manually after fixing the
Car class. If you'd like to focus on your code, and just see when you've resolved the issue, PyCharm can run the tests for you automatically.
Click on the toolbar and select Rerun Automatically:
Click the button on the Run toolbar.
Now, every time you enter changes in your project files (as it was done earlier), the tests will run without your intervention.