Here we’ll see how PyCharm helps creating and running Python unit tests.
Python programming and writing Python unit tests is out of the scope of this tutorial.
Make sure that at least one Python interpreter (versions from 2.4 to 3.3) is properly installed on your computer.
On the main menu, choose File | New Project.
In the Create New Project dialog box, specify the project name (let it be TestSamples), select the desired project type (here this is Empty project), and the Python interpreter you want to use.
After you click OK, select the window to work with your new project in. In the Open Project dialog box, let us select the first option – open the new project in a separate window:
Press Alt+Insert, and then choose Python file from the pop-up window:
In the New Python file dialog box, type the new file name:
The new file opens in its tab in the editor, with the __author __ and __project__ variables already defined. Let’s create a simple script that will solve a quadratic equation. Type the following code:
Right-click the class name, and choose Go to | Test on the context menu, or just press Ctrl+Shift+T:
In the Create test dialog box, just accept the suggested directory and names, and select the check box next to the suggested test_demo function:
The test case opens in the editor:
As you see, the created test case meets the requirements to the Python unit testing framework – it imports TestCase class from the unittest module, and the test function names begin with the string “test”.
However, this test case is a kind of common place and requires some fine tuning. Let us first of all import the class we’re going to test. To do that, add import statement:
Use basic code completion while typing this statement: on pressing Ctrl+Space, PyCharm suggests suitable module and class names:
When this statement is complete, it is shown grayed out, which means that import is not used so far.
Next, let’s create a test method that will assert throwing an exception, if the discriminant of a quadratic equation is negative. Add the following code to our test case:
So, the resulting code is:
Our test case contains two test methods: test_negative_discr and test_demo. The latter always fails.
Note that import statement is not gray any more – we’ve used the imported Solver class in the method test_negative_discr.
Try a different approach. Press Alt+Insert, choose Python file from the pop-up window, then, in the New Python file dialog box, select Python unit test from the Kind menu, and type the name of new test:
PyCharm create a test case with some initial code, and opens it in the editor:
This is again nothing but a stub. So, we’ll import the class to be tested, and add a test method. The resulting code is:
For executing our test case, PyCharm suggests a dedicated temporary run/debug configuration. It is quite ready to be used it “as is”: just press Ctrl+Shift+F10, or right-click somewhere within the class, and choose Run unittests in test_solver on the context menu.
The results are shown in the Run tool window: