Behave Rest API – Web tutorial links
- How to Setup Python's BEHAVE to Automate REST APIs
- Understanding Behave Feature Files and Step Implementations
- Quick Overview of HTTP Messages
- Using Python's Request Library to Make Rest API calls
- Writing Step Implementations of your API Tests
- Using Scenario Outline to Make Your Tests Data Driven
Before We Start
This guide is targeted to Software Testers who wants to try implementing their automated tests using Python and Behave, A mature BDD testing Framework. And although Behave's Documentation is fantastic, it's sort of intimidating for Software Testers who doesn't have any background in programming.
Which is why I'm creating a detailed guide from setting up the environment and digging in to the details of the library so that QA Professionals can catch up easily. And probably start their journey in doing test automation in Python and use Behave as an introductory tool in doing Behavior Driven Testing.
The Complete Guide is created using Ubuntu Version 18.04. You might need to trouble shoot if you encounter errors if you try this out on MAC, Windows or a different flavor of Linux.
Behave can run in
python3 versions of Python. But on this guide we'll be using Python3 all the way as Python2 version of python will be running out of support in a few years.
By default Ubuntu already has Python 2 and Python 3 installed. You can check it out by running the below command.
It will then output the current version of Python3 that your system is running. If you do not have the latest version of python3 you can run the below commands on your terminal.
sudo apt-get update sudo apt-get install python3.7
After installing python you need to install
pip3 . Pip is python's package manager. We'll be needing it to install
BEHAVE later. Run the following command to install pip
sudo apt-get install -y python3-pip
Now that you have
pip installed you'll need to install a few more packages to avoid unnecessary errors. Finally run the following commands to finish your setup in python environment.
sudo apt-get install build-essential libssl-dev libffi-dev python-dev
Creating a Virtual Environment
At this point you can actually proceed in installing
Behave and start digging the Documentation and write feature files but I highly suggest that you start learning Python Virtual Environments. Python Virtual Environments are important to avoid dependency issues when you start working with other Python projects.
To create a python virtual environment start by running the below command
python3 -m venv ./sample_virtualenv_directory
The command that you run will create a directory or should I say an isolated instance of a python3 interpreter that is similar to what is running in your current system. When you run that command you now have a new python environment that runs on that directory. This virtual environment that we've created is where we will install
BEHAVE and other python libraries that we will be using to automate our REST APIS.
If you run
which python on the terminal it will most likely output the python bin folder that runs on your system which is something that is similar below
Let's now try activating the virtual environment that we've created by going to the
/bin folder inside the virtual environment you've created. When you're inside the
/bin folder run the below command to activate the virtual environment.
When you run that command you're telling your system that you want to use the python instance in the virtual environment that you've created. All the packages and projects that you want to run and install now points to the virtual environment that you've made. If you want to check it out try running
which python again on your terminal and you'll see that it points to the new python bin directory.
Note: if you want to switch back to using the python version that runs on your system you just need to run the command
deactivate. But again always remember that if you want to work on your BEHAVE project always use activate the virtual environment by running
the only tasks that's left now is to install the
behave package which is probably the easiest part of this guide. To install behave, simply check if you are using the virtual environment that you've created and then run the below command
pip3 install behave
Now that you're all done with your setup. Our next step is to understand Behave Project Structure. I'll be covering Feature files and Steps Implementation on my next guide.