Skip to content

Instantly share code, notes, and snippets.

@jasonlagaac
Last active August 9, 2018 04:27
Show Gist options
  • Save jasonlagaac/0cf5562e9ae9e8a4c56bd4b5f136d751 to your computer and use it in GitHub Desktop.
Save jasonlagaac/0cf5562e9ae9e8a4c56bd4b5f136d751 to your computer and use it in GitHub Desktop.
Oh Behave Part 1

Chapter 1 - Getting Started

1. Welcome

Hello. You are now in the most unfortunate place of learning Python with behave from someone like me who has no teaching experience of programming whatsoever. Here I am. A person who is usually locked away in a back corner of the office working on projects in Objective-C and iOS. I am now going to attempt to write a book about a subject I do not have a lot of knowledge about or even the required amount of experience.

So what is the rationale of making an attempt at something like this:

  1. I have inexperienced testers with some to no programming experience
  2. They need to learn automated testing.
  3. Looking at a dire situation which sees them doing manual testing every week.

So throughout this book we'll struggle, like myself to find the words to explain what stuff is and what to do, Python, behave, etc to hopefully get an understanding of things and make you a better tester/programmer.

2. Working with the terminal and making your project directory


We'll be jumping in to using the terminal/shell to make your project directory and running stuff via command line. There are some basic shell commands that we'll be using and I'll explain them on the way. I will not reduce you down to the inferior caste of GUI operators. Don't be afraid of screens of text and blinking curors :)

Windows

I'm not too familiar with Windows and Powershell, but I am just going to assume this will work. I have not been a Windows user since the early 00's and know now they have Powershell. Use that.

MacOS

  1. Open the Finder and then go into your Applications folder.
  2. Inside the Applications folder enter the Utilities folder.
  3. Open the Terminal application.
  4. Optionally keep it in the Dock.

Linux

I'll assume you know what you are doing. Using Gnome, KDE, whatever.

Working that Terminal

With you terminal open we're going to learn some basic terminal commands

Listing files

The #ls# command lists files in the directory. In this example below shows a project directory on my computer.

> ls
BeamWallet-Test              Cartfile                     build
BeamWallet-Test-Target       Cartfile.private             build-framework.sh
BeamWallet-iOS-SDK           Cartfile.resolved            fastlane
BeamWallet-iOS-SDK.xcodeproj Carthage
BeamWalletTests              README.md

Making a directory

The #mkdir# command allows you to make a directory.

Let's make a directory for your project.

> mkdir behave-python-book

Once that command has been run then we can verify if it has been created by runnin the #ls# command once again

Changing directories and getting the current directory

The #cd# command allows you to change directory. Let's go inside your project directory

> cd behave-python-book

If you have lost your way, you can find your current location path by printing the working directory.

> pwd

You are able to navigate backwards a directory by running:

cd ..

Being lazy

If you are tired of typing out every character there is a way to quickly auto completing your option by pressing the #tab# key. The shell will either prompt you to print out suggestions:

Multiple instances of a command starting with the target character

➜ c
zsh: do you wish to see all 186 possibilities (47 lines)?

Another possibility

➜ mk
mkbom        mkdep        mke2fs       mkfifo       mklocale     mkpassdb
mkbundle     mkdir        mkextunpack  mkfile       mknod        mktemp

If it does have a one to one match it should auto complete.

Oh shit, I mistyped something.

Don't panic. You an cancel whatever you are typing by entering the key combination #CTRL-C#

These are the basic shell commands to help you get on with your journey.

3. Setting up your environment and installing Python 3 and other stuff we'll need

Installing Python is usually a painless process, but in our instance we'll be working with virtual environments. Let's do some installing and then I'll explain what virtual environments are.

(Pip installation and setup)

Windows

Install Scoop

Reference: https://scoop.sh/

Open PowerShell and run

iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

Install Git

Next we will install the latest version of Git. I'll explain what git is in the next chapter and why we need it.

scoop install git

Install Python 3

Next we will install the latest version of Python

scoop install python

Install pipenv

Next we will install the latest version of pipenv.

pip install pipenv

Installing Python 3 on MacOS

Install Homebrew

Reference: https://brew.sh/

Open up the terminal and run

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install Git

Next we will install the latest version of Python

brew install git

Install pipenv

Next we will install the latest version of pipenv

brew install pipenv

Virtual Environments

What is a virtual environment you may ask? Well think of it this way, your computer is the big playground where everything can and does happen from people playing soccer, having picnics, flying kites to teenagers in skinny jeans loitering around. So like this scenario, anything that you do is at the mercy of the environment that you are in. Balls can come flying at you, kids running around aimlessly, getting chased by rabid dogs blah blah blah. In a computer sense you have environment variables changing to packages which update to newer versions. One day you might be writing something in a different version say Python 3.6 and then you update your system and all of a sudden there is now Python version 3.8 and things suddenly stop working. In this mad world how can we maintain consistency?

This is where virtual environments come in. Inside your playground you have a sandbox where things don't change unless you want them to. That same idea is similar to virtual environments. You have the same packages and libraries which are consistent no matter what happens on your computer. Things will (hopefully) work when you run updates and the like.

In our workflow, we'll be using #pipenv# to help create and manager our Python project development environment.

Setting up your environment

Open up your terminal application and then navigate to your #behave-python-book# directory.

> cd behave-python-book

Inside we have nothing set up thus far. If you run the #ls# command it'll show that there is nothing inside.

Source/Projects/behave-python-book
➜ ls

Source/Projects/behave-python-book
➜

So what we'll be doing is setting up your python environment in that directory

> pipenv install

Here is some output when I run it in that directory.

➜ pipenv install
Creating a virtualenv for this project...
Pipfile: /Users/jasonlagaac/Source/Projects/behave-python-book/Pipfile
Using /usr/local/opt/python/bin/python3.7 (3.7.0) to create virtualenv...
⠋Already using interpreter /usr/local/opt/python/bin/python3.7
Using base prefix '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7'
/usr/local/lib/python3.7/site-packages/virtualenv.py:1041: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
New python executable in /Users/jasonlagaac/.local/share/virtualenvs/behave-python-book-PVwIL60i/bin/python3.7
Also creating executable in /Users/jasonlagaac/.local/share/virtualenvs/behave-python-book-PVwIL60i/bin/python
Installing setuptools, pip, wheel...done.
Setting project for behave-python-book-PVwIL60i to /Users/jasonlagaac/Source/Projects/behave-python-book

Virtualenv location: /Users/jasonlagaac/.local/share/virtualenvs/behave-python-book-PVwIL60i
Creating a Pipfile for this project...
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (a65489)!
Installing dependencies from Pipfile.lock (a65489)...
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

Source/Projects/behave-python-book took 7s
➜

When you run the #ls# command it should have created two files #Pipfile# and #Pipfile.lock#

Source/Projects/behave-python-book
➜ ls
Pipfile      Pipfile.lock

Congratulations! you have created your own virtual environment!

Chapter 2 - Setting up your environment

Pipfile and Pipfile.lock - What are they?

Going back to your virtual environement explaination, remember how we wanted to maintain a level of consistency? With these two files it allows for the virtual environment to maintain application specific versions.

I'm not going to claim that I am good at explaining things and going to take credit for stuff that I didnt write. But here is my source for the explaination for Pipfile and Pipfile.lock

Pipenv

The pipenv file is intended to specify packages requirements for your Python application or library, both to development and execution. We use already existing librarys to leverage their utility but also help us speed up the development process by using an existing library rather than creating one from scratch.

In our example we're going to install some libraries that we're going to use. In the terminal and inside the #behave-python-book# directory we'll run the following command to install #Behave#

> pipenv install behave

Here is the example output from my terminal:

➜ pipenv install behave
Installing behave...
Collecting behave
  Using cached https://files.pythonhosted.org/packages/a8/6c/ec9169548b6c4cb877aaa6773408ca08ae2a282805b958dbc163cb19822d/behave-1.2.6-py2.py3-none-any.whl
Collecting parse-type>=0.4.2 (from behave)
  Using cached https://files.pythonhosted.org/packages/a2/c9/e6fd8092a5a06f2519ec434ca8e9e42238384f64c9b659456d98b0593b89/parse_type-0.4.2-py2.py3-none-any.whl
Collecting parse>=1.8.2 (from behave)
Collecting six>=1.11 (from behave)
  Using cached https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Installing collected packages: parse, six, parse-type, behave
Successfully installed behave-1.2.6 parse-1.8.4 parse-type-0.4.2 six-1.11.0

Adding behave to Pipfile's [packages]...
Pipfile.lock (a65489) out of date, updating to (ecf4b7)...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (ecf4b7)!
Installing dependencies from Pipfile.lock (ecf4b7)...
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 4/4 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

Pipenv.lock

The pipenv.lock file is intended to specify, based on the packages present in Pipfile file, which specific version of those should be used, avoiding the risks of automatically upgrading packages that depend upon each other and breaking your project dependency tree.

You can lock your currently installed packages using:

> pipenv lock

Looking inside files

To look inside files from the shell, you are able to do it in two ways #cat# and #more# To look at a file in a non-paginated and just a massive dump of text run the #cat# command:

> cat Pipfile.lock

Here is the cat output:

Source/Projects/behave-python-book
➜ cat Pipfile.lock
{
    "_meta": {
        "hash": {
            "sha256": "b316a33dc153e346058ef2c5cfe6512e97a3f7e6710b32fefbbc0f3a94ecf4b7"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.7"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "behave": {
            "hashes": [
                "sha256:b9662327aa53294c1351b0a9c369093ccec1d21026f050c3bd9b3e5cccf81a86",
                "sha256:ebda1a6c9e5bfe95c5f9f0a2794e01c7098b3dde86c10a95d8621c5907ff6f1c"
            ],
            "index": "pypi",
            "version": "==1.2.6"
        },
        "parse": {
            "hashes": [
                "sha256:c3cdf6206f22aeebfa00e5b954fcfea13d1b2dc271c75806b6025b94fb490939"
            ],
            "version": "==1.8.4"
        },
        "parse-type": {
            "hashes": [
                "sha256:6e906a66f340252e4c324914a60d417d33a4bea01292ea9bbf68b4fc123be8c9",
                "sha256:f596bdc75d3dd93036fbfe3d04127da9f6df0c26c36e01e76da85adef4336b3c"
            ],
            "version": "==0.4.2"
        },
        "six": {
            "hashes": [
                "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9",
                "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb"
            ],
            "version": "==1.11.0"
        }
    },
    "develop": {}
}

For a paginated format you can run the more command.

> more Pipfile.lock

An example #more# output would be:

{
    "_meta": {
        "hash": {
            "sha256": "b316a33dc153e346058ef2c5cfe6512e97a3f7e6710b32fefbbc0f3a94ecf4b7"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.7"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "behave": {
            "hashes": [
                "sha256:b9662327aa53294c1351b0a9c369093ccec1d21026f050c3bd9b3e5cccf81a86",
                "sha256:ebda1a6c9e5bfe95c5f9f0a2794e01c7098b3dde86c10a95d8621c5907ff6f1c"
            ],
            "index": "pypi",
            "version": "==1.2.6"
        },
        "parse": {
            "hashes": [
                "sha256:c3cdf6206f22aeebfa00e5b954fcfea13d1b2dc271c75806b6025b94fb490939"
            ],
            "version": "==1.8.4"
        },
        "parse-type": {
            "hashes": [
                "sha256:6e906a66f340252e4c324914a60d417d33a4bea01292ea9bbf68b4fc123be8c9",
                "sha256:f596bdc75d3dd93036fbfe3d04127da9f6df0c26c36e01e76da85adef4336b3c"
            ],
            "version": "==0.4.2"
        },
        "six": {
            "hashes": [
                "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9",
                "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb"
            ],
            "version": "==1.11.0"
Pipfile.lock

If you were to press the spacebar it should go to the next page in the text output.

Summary

In this chapter we have covered a lot of content from basic shell commands, working with pipenv and setting up environments. In the next chapter we are going to learn some python basics and the behave workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment