Step 3. Testing Your First Python Application
Choosing the test runner
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 editor background, point to , and then choose (or just press Ctrl+Shift+T):
A pop-up appears that suggests to create 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:
If we run these tests (on the context menu), we can see that they fail 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:
import unittest from Car import Car class TestCar(unittest.TestCase): def setUp(self): self.car = Car() class TestInit(TestCar): def test_initial_speed(self): self.assertEqual(self.car.speed, 0) def test_initial_odometer(self): self.assertEqual(self.car.odometer, 0) def test_initial_time(self): self.assertEqual(self.car.time, 0) class TestAccelerate(TestCar): def test_accelerate_from_zero(self): self.car.accelerate() self.assertEqual(self.car.speed, 5) def test_multiple_accelerates(self): for _ in range(3): self.car.accelerate() self.assertEqual(self.car.speed, 15) class TestBrake(TestCar): def test_brake_once(self): self.car.accelerate() self.car.brake() self.assertEqual(self.car.speed, 0) def test_multiple_brakes(self): for _ in range(5): self.car.accelerate() for _ in range(3): self.car.brake() self.assertEqual(self.car.speed, 10) def test_should_not_allow_negative_speed(self): self.car.brake() self.assertEqual(self.car.speed, 0) def test_multiple_brakes_at_zero(self): for _ in range(3): self.car.brake() self.assertEqual(self.car.speed, 0)
Running the test
Now run the test by right-clicking the editor background above the declaration of the class
test_car. This time some of the tests pass successfully:
Debugging the test
Next, let's look deeper into the test code and debug one of the tests that failed. For example, we'll put a breakpoint in the following place:
Next, launch a debugger session. To do that, right-click the editor background at the method
test_should_not_allow_negative_speed and choose Debug on the context menu, or click in the Navigation bar:
We've placed the breakpoint at the
self.car.brake() statement of the
test_should_not_allow_negative_speed method. Let's look at the debugger output:
Click the button to skip the library classes and go into the class
Next, click the same button again, and see the test debug output:
It shows that speed can become negative, which is impossible. It seems that some additional check is required in the code of the class
Change the method
brake as follows:
def brake(self): if self.speed < 5: self.speed = 0 else: self.speed -= 5
Now let's run the test again:
Running tests automatically
In the last paragraph, after fixing our code, we reran our tests by using the icon. 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.
To do that, do two things:
- First, click the button on the Run toolbar.
- Second, click the , select the Set Autotest Delay command, and then choose the delay value.
Then, every time you enter changes in your project files (as it was done earlier), after the specified amount of time, the tests will run without any intervention from you. For example:
So, this brief tutorial is over. Let's repeat what you've done with the help of PyCharm:
- Selected the test runner.
- Created and modified the test code.
- Ran the test.
- Debugged the test.
- Ran it automatically.