Type-safe, YAML-based BDD, TDD & specification by example framework for python.
Project description
HitchStory
HitchStory is a python 3 testing and living documentation framework for building easily maintained example driven executable specifications (sometimes dubbed acceptance tests).
It was designed initially to make realistic testing of code less of a chore so the tests would actually get written and run.
The executable specifications can be written to specify and test applications at any level and have been used successfully to replace traditional low level unit tests, integration tests and end to end tests with easier to maintain tests.
The specifications are written using StrictYAML and the code to execute them is written by you, in python.
Example
example.story:
Logged in:
given:
website: /login # preconditions
steps:
- Form filled:
username: AzureDiamond
password: hunter2
- Clicked: login
Email sent:
about: |
The most basic email with no subject, cc or bcc
set.
based on: logged in # inherits from and continues from test above
steps:
- Clicked: new email
- Form filled:
to: Cthon98@aol.com
contents: | # long form text
Hey guys,
I think I got hacked!
- Clicked: send email
- Email was sent
engine.py:
from hitchstory import BaseEngine, GivenDefinition, GivenProperty
from mockemailchecker import email_was_sent
from mockselenium import Webdriver
from strictyaml import Str
class Engine(BaseEngine):
given_definition = GivenDefinition(
website=GivenProperty(Str()),
)
def set_up(self):
self.driver = Webdriver()
self.driver.visit(
"http://localhost:5000{0}".format(self.given['website'])
)
def form_filled(self, **textboxes):
for name, contents in sorted(textboxes.items()):
self.driver.fill_form(name, contents)
def clicked(self, name):
self.driver.click(name)
def email_was_sent(self):
email_was_sent()
from hitchstory import StoryCollection
from pathquery import pathquery
from engine import Engine
StoryCollection(pathquery(".").ext("story"), Engine()).named("Email sent").play()
Will output:
RUNNING Email sent in /path/to/working/example.story ...
Visiting http://localhost:5000/login
Entering text hunter2 in password
Entering text AzureDiamond in username
Clicking on login
Clicking on new email
In contents entering text:
Hey guys,
I think I got hacked!
Entering text Cthon98@aol.com in to
Clicking on send email
Email was sent
SUCCESS in 0.1 seconds.
Installation and set up
You can install hitchstory through pypi in any python 3 virtualenv:
$ pip install hitchstory
However, it's recommended to install and set up hitchstory with hitchkey, which will take care of automatically setting up a up the recommended hitchstory environment.
Install hitchkey with pipx:
pipx install hitchkey
Once hitchkey is installed:
Example demo of hitchstory basics:
cd temp
hk --demo hitchstory ; hk bdd email
Example python API test demo (uses game of life):
cd temp
hk --demo pythonapi ; cd pythonapi ; hk bdd
Using HitchStory
- Abort a story with ctrl-C
- Continue on failure when playing multiple stories
- Hiding stacktraces for expected exceptions
- Handling failing tests
- Flaky story detection
- Generate documentation from stories
- Given preconditions
- Gradual typing of story steps
- Inherit one story from another
- Extra story metadata - e.g. adding JIRA ticket numbers to stories
- Story with parameters
- Play multiple stories in sequence
- Story that rewrites itself
- Running a single named story successfully
- Shortcut lookup for story names
- Special exception named failure
- Arguments to steps
- Strong typing
- Variations
Approach to using HitchStory
Best practices, how the tool was meant to be used, etc.
- What is the difference betweeen a test and a story?
- Recommended complementary tools
- Triality
- Tests are an investment
- Executable specifications
- Recommended Environment
- Can I do BDD with hitchstory? How do I do BDD with hitchstory?
- Does hitchstory let your BA or Product Manager write stories while you just write the code?
- What is a testing and living documentation framework?
- Flaky Tests
- The importance of test realism
- How can executable specifications and living documentation be used for stakeholder collaboration?
- Screenplay Principle
- Testing non-deterministic code
Design decisions and principles
Somewhat controversial design decisions are justified here.
- Principles
- Two Unit Tests, Zero Integration Tests
- Declarative User Stories
- Why is inheritance a feature of hitchstory stories?
- Why does hitchstory not have an opinion on what counts as interesting to "the business"?
- Why programatically rewrite stories?
- Why does HitchStory use StrictYAML?
- Why does hitchstory mandate the use of given but not when and then?
- Why does hitchstory not have a command line interface?
Why not X instead?
There are several tools you can use instead, this is why you should use this one instead:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.