PyCharm 2017.3 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.

/help/img/idea/2017.3/py_test_runner.png

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):

/help/img/idea/2017.3/py_goto_test.png

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

/help/img/idea/2017.3/py_create_new_test.png

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:

/help/img/idea/2017.3/py_create_test_dialog.png

A new Python test class is created:

/help/img/idea/2017.3/py_test_class.png

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

/help/img/idea/2017.3/py_tests_fail.png

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:

/help/img/idea/2017.3/py_run_unittest.png

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:

/help/img/idea/2017.3/py_test_breakpoint.png

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.3/debug.png in the Navigation bar:

/help/img/idea/2017.3/py_test_debug.png

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:

/help/img/idea/2017.3/py_debug_output_0.png

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

/help/img/idea/2017.3/py_debug_car_brake.png

Next, click the same /help/img/idea/2017.3/step_into_my_code.png button again, and see the test debug output:

/help/img/idea/2017.3/py_test_debug_output.png

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:

/help/img/idea/2017.3/py_test_debug_output_1.png

Running tests automatically

In the last paragraph, after fixing our code, we reran our tests by using the /help/img/idea/2017.3/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.3/icon_toggleAutoTest.png button on the Run toolbar.
  • Second, click the /help/img/idea/2017.3/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:

/help/img/idea/2017.3/py_autotest.png

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.

See Also

Procedures:

Language and Framework-Specific Guidelines:

Last modified: 15 September 2017