PyCharm 2017.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 or py.test before, you have to choose unittest. To learn how it's done, see Choosing Your Testing Framework.


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, then right-click the editor background, point to Go To, and then choose Test (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 check boxes:


A new Python test class is created:


If we run these tests (Run ‘Unittest in test_car’ 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.

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): = Car() class TestInit(TestCar): def test_initial_speed(self): self.assertEqual(, 0) def test_initial_odometer(self): self.assertEqual(, 0) def test_initial_time(self): self.assertEqual(, 0) class TestAccelerate(TestCar): def test_accelerate_from_zero(self): self.assertEqual(, 5) def test_multiple_accelerates(self): for _ in range(3): self.assertEqual(, 15) class TestBrake(TestCar): def test_brake_once(self): self.assertEqual(, 0) def test_multiple_brakes(self): for _ in range(5): for _ in range(3): self.assertEqual(, 10) def test_should_not_allow_negative_speed(self): self.assertEqual(, 0) def test_multiple_brakes_at_zero(self): for _ in range(3): self.assertEqual(, 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 /help/img/idea/2017.2/debug.png in the Navigation bar:


We've placed the breakpoint at the statement of the test_should_not_allow_negative_speed method. Let's look at the debugger output:


Click the /help/img/idea/2017.2/step_into_my_code.png button to skip the library classes and go into the class Car:


Next, click the same /help/img/idea/2017.2/step_into_my_code.png 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 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:


Running tests automatically

In the last paragraph, after fixing our code, we reran our tests by using the /help/img/idea/2017.2/run.png 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 /help/img/idea/2017.2/icon_toggleAutoTest.png button on the Run toolbar.
  • Second, click the /help/img/idea/2017.2/cogwheel_blue_with_arrow.png, 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.
Last modified: 26 October 2017

See Also


Language and Framework-Specific Guidelines: