Test-Driven Development with Twisted
Here we'll take the tutorial "Test-Driven Development with Twisted", located at this address, and try to repeat it using PyCharm.
Creating project, packages and Python files
On the main menu, choose New Project dialog box, choose to create a pure Python project. Let's call it
Create an empty directory called calculus containing an empty __init__.py file. To do that, choose on the main menu, and then choose Python Package from the New pop-up menu.
Next, create a Python file. To do that, again point to Python File in the pop-up. This file is called base_1.py and it opens for editing immediately. Enter the following code:on the main menu, and choose
Next, right-click the package calculus and create a Python package again - this time it should be called test.
You must install the Twisted package to execute tests. Besides that, if you are working on Windows, also install the package pypiwin32.
It's most easy to install the required packages in PyCharm: to do it, go to Settings/Preferences dialog and click the Project Interpreter page. On this page, in the Packages toolbar section, click and select the required packages in the Available Packages dialog box. Read the section Install, uninstall, and upgrade packages for details.
Selecting the required test runner
Again In the Settings/Preferences dialog Ctrl+Alt+S, under the Tools node, click the Python Integrated Tools page.
On this page, select the test runner Twisted Trial from the list in the Default test runner field.
Apply changes and close the Settings/Preferences dialog.
Creating a failing test
Right-click the file base_1.py next to the class declaration, and press Ctrl+Shift+T or choose on the context menu. In the Create Test Dialog you'll have to make some changes to the default settings:
First, click the browse button next to the Target directory field, and choose test package.
Second, in the Test file name field, enter the name test_base_1.py, and in the Test class name field, enter the name
TestBase_1. Finally, select all methods - thus the tests for all the methods of the tested class will be included.
You should get the following code:
This is a working class, but we are writing tests for Twisted... Let's import the package. You'll end up with the following code:
As the idea of TDD (Test-Driven Development) is writing tests before the code, let's make the actual tests:
By the way, look at the Project tool window - it shows the structure of the your project. At the moment, you have 4 files:
Running the failing tests
Now let's run this test. To do that, open for editing the test file test_base_1.py, right-click the editor background and on the context menu choose Run 'Twisted Trial for test_base_1.CalculationTestCase'.
As expected, all tests failed:
Note that in the Project tool window appears one more directory - _trial_temp. It contains the log file:
Making tests pass
Let's change the class being tested and make the tests pass. To do that, we'll change the base_1.py script. The code of the file will look as follows:
Let's run test_base_1.py again. To do it, right-click the editor background and choose Run 'Twisted Trial for test_base_1.CalculationTestCase'. This time the tests pass:
Let us recall what we have done with the help of PyCharm:
Installed required packages.
Created a project, Python packages and files.
Ran the tests in the Test Runner.