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 IntelliJ IDEA.
Creating project, packages and Python files
From the main menu, choose New Project dialog, 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 from the main menu, and then choose Python Package from the New popup menu.
Next, create a Python file. To do that, again point to Python File in the popup. This file is called base_1.py and it opens for editing immediately. Enter the following code:from 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 Python packages in IntelliJ IDEA: navigate to SDKs under the Platform Settings section, and select the target Python SDK from the list of the available SDKs. Switch to the Packages tab to preview the list of the available packages. Read the section Install, uninstall, and upgrade Python packages for details., select
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 from 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 filename 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 from 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: