Part 2. Framework Structure

Introduction

In this section, we will create our project along with the Python packages it needs and the appropriate framework structure

Let’s begin 🙂

Pipenv

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. It handles all our Python packages as well as creating our virtual environment in a new shell.

Installing Pipenv

  1. Enter the following command to install pipenv via pip on the user level, to prevent breaking any system-wide packages.

Setting up Pipenv Shell

Next, let’s set up our virtual environment in a new shell

  1. Open up a Terminal (or Command prompt) and change directory in to your project root…
  2. Enter the following command to create the virtual environment shell…

Installing our Python packages

We will now use pipenv to install our Python packages locally to our project.  This way, any one else who clones our repository can easily install all necessary packages listed in our Pipfile by entering pipenv install 🙂

pytest-bdd

  1. Enter the following command, whilst in the root of the project to install the pytest-bdd framework…

selenium

  1. Enter the following command, whilst in the root of the project to install selenium

webdriver-manager

  1. Enter the following command, whilst in the root of the project to install webdriver-manager, which will automatically manage our webdriver binaries for us…

Now, we can open up our project in our chosen IDE, so let’s do that 🙂

Framework Structure

Good practice when using pytest-bdd is to create a parent test directory, and then have features and step_defs sub-directories/python packages contained within the test directory.

  1. Add the following directories and python packages so that your project tree looks the same/similar to below…

Creating our ConfTest file

Next, we will create our conftest.py python file which will contain our setUp and tearDown functions for our webdriver

  1. In the test python package, add a new Python file and name it conftest.py (this is where we will store our shared steps and fixtures)
  2. Add the following in the file (I have added comments to explain what is going on)…

The foundations are now setup.  In the next section, we will begin writing our first BDD scenario 🙂

Liked it? Take a second to support Thomas on Patreon!

Previous Article

Next Article

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.