PyCharm 2018.2 Help

Step 3. Testing Your First Python Application

Remember, in the first tutorial you’ve created your first Python application, and in the second tutorial you’ve debugged it. Now it’s time to do some testing.

Choosing the test runner

If you used nosetest, py.test, or Twisted Trial before, you have to choose unittest. To learn how it's done, see Choosing Your Testing Framework.

py test runner

Creating test

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 Go To, and then choose Test (or just press Ctrl+Shift+T):

py goto test

A pop-up appears that suggests to create a new test:

py create 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:

py create test dialog

A new Python test class is created:

py test class

If we run these tests (Run ‘Unittest in test_car’ on the context menu), we can see that they fail by default:

py tests fail

Now we know that we can run tests, let’s start writing some actual test code.

Writing test

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:

py run unittest

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:

py test breakpoint

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 icons actions startDebugger svg in the Navigation bar:

py test debug

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:

py debug output 0

Click the step into my code button to skip the library classes and go into the class Car:

py debug car brake

Next, click the same step into my code button again, and see the test debug output:

py 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 Car:

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:

py test debug output 1

Running tests automatically

In the last paragraph, after fixing our code, we reran our tests by using the icons toolwindows toolWindowRun svg 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 icon toggleAutoTest button on the Run toolbar.
  • Second, click the icons general gearPlain svg, 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:

py autotest

Summary

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.
Last modified: 14 July 2018

See Also

Language and Framework-Specific Guidelines: