
Python 2.7.10 Testing with Behave and Intellij (Idea)
I felt it was about time to write how I have successfully integrated Intellij (Idea, version 14.1 Ultimate Edition), Python 2.7.10, and behave tests.
You can now run, and debug your behave tests from within Intellij :O)</blockquote>
I am going to describe how I set up an Idea project from scratch, create a behave feature, and steps. But first, I am going to tell you how to check your python environment.
The first thing I am going to do is check my python version. I am using OSX, but this way of checking Python can be used with any operating system:
$ python
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license"
for more information. * * *
The important thing is that you are using Python 2.7.10. I installed the “official” python-2.7.10-macosx10.6.pkg.
I’ve created a new Python 2.7 virtual environment. This will be used for our Intellij project (background detail http://docs.python-guide.org/en/latest/dev/virtualenvs/)
Create my virtualenv, source the new virtualenv, and check that the python binary location (in the virtualenv directory) is correct.
Now I am going to create a new python project in Idea
File -> New -> Project:
Select Python interpreter and SDK
You need to select the “my_behave_project” virtualenv you created earlier.
Call the project my_behave_project.
Then click Finish.
Create some directories in Intellij
It is time to create some directories:
I have created a requirements folder, with a dev.txt file. I will keep the Python dependencies (requirements) for my project in here, with a specific version for each one.
I will show you how to install them, in a bit.
I have also created a directory named tests, with sub-directories, behave -> features -> steps (take a look in https://pythonhosted.org/behave/tutorial.html for background detail).
It is now time set up our dependencies. Ensure your dev.txt includes:
behave==1.2.5
This tells my project to explicitly use version 1.2.5 of behave.
It’s time to install the dependencies. I prefer to use my terminal to do this:
$ pip install -r requirements/dev.txt
Collecting behave==1.2.5 (from -r requirements/dev.txt (line 1))
Running setup.py bdist_wheel for parse-type
Successfully built parse-type
Installing collected packages: parse, six, enum34, parse-type, behave
Successfully installed behave-1.2.5 enum34-1.1.6 parse-1.6.6 parse-type-0.3.4 six-1.10.0
I have used pip to install the dependencies in requirements/dev.txt pip install -r requirements/dev.txt
Back to Intellij
I have created a file named tfl.feature in the features folder:
Feature: Parse TFL XML data feed
# Enter feature description here
Scenario: get XML from TFL endpoint
Given that the TFL connections endpoint is available for "type" line
When I make a request
Then I should receive "XML" data
And I should receive a "400" response
You can clearly see that some of the scenarios (steps) are coloured yellow.
This is because there is no Python code to represent the “step definitions”.
Let’s fix that. Idea can create the “beginnings” of the Python code for you:
Put your Cursor over the yellow, and select Create All Step Definitions.
It is important that Python (behave) is selected, and the steps directory is “the file location”.
There is a bug in Idea, which will (likely) create the file in the features directory, and may only fix one (yellow) step.
Once the tfl.py file is created, move it to the steps directory, and then repeat (creating each step in tfl.py), until all the yellow is gone:
Running your behave tests
The next thing to do is to “see” if your behave test will run from the command line (terminal).
Don’t worry, they won’t. We will fix it :)**
I have changed directory to:
my_behave_project/tests/behave
I then type "behave"
Back to Intellij. Let’s fix the breaking tests.
I have made the following changes to the Python (in tfl.py)
All code for this blog post is in https://github.com/chocksaway/my_behave_project
I have deleted the line:
use_step_matcher("re")
I have added a** {}** whenever a value is passed (as part) of a step (decorator / annotation), and the variable (for example -> data_type) to the method signature. For example:
@then('I should receive "{data_type}" data')
def step_impl(context, data_type):
"""
:type context behave.runner.Context
"""
pass
> > pass is used to "successfully pass" any step > which has no implementation. > >
Let’s run the tests again
Back to the terminal. I type behave :
$ behave
Feature: Parse TFL XML data feed # features/tfl.feature:2
Scenario: get XML from TFL endpoint # features/tfl.feature:5
Given that the TFL connections endpoint is available for "type" line # features/steps/tfl.py:12 0.000s
When I make a request # features/steps/tfl.py:20 0.000s
Then I should receive "XML" data # features/steps/tfl.py:28 0.000s
And I should receive a "400" response # features/steps/tfl.py:4 0.000s
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
4 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s
You will see that everything has passed, and the step parameters (passed in with {}) are highlighted.
Let’s get the behave test running in Intellij.
Open your tfl.feature file.
Right click:
Make sure you select the **“B” Create tfl.feature…. **
You will get a run dialogue:
Just click OK.
Now select the Run button:
You will see that the tests are all green.
*Congratulations, your behave test is now running from Idea /Intellij :O)*
“Please let me know if there any issues with this post.”