Git Product home page Git Product logo

hitchstory's Introduction

HitchStory

Main branch status

Type-safe StrictYAML python integration testing framework. With this framework, your tests can:

Rewrite themselves from program output (command line test example)

Test rewriting itself

Autogenerate documentation (website test example)

Test writing docs

The tests can be run on their own or as pytest tests.

Demo projects with demo tests

Project Storytests Python code Doc template Autogenerated docs
Website add todo, correct spelling engine.py docstory.yml Add todo, Correct my spelling
REST API add todo, correct spelling engine.py docstory.yml Add todo, Correct my spelling
Interactive command line app add todo, correct spelling test_integration.py docstory.yml Add todo, Correct my spelling
A Python API add todo, correct spelling test_integration.py docstory.yml Add todo, Correct my spelling

Code 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
  following steps:
  - Clicked: new email
  - Form filled:
      to: [email protected]
      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 hitchstory import Failure, strings_match
from strictyaml import Str

class Engine(BaseEngine):
    given_definition = GivenDefinition(
        website=GivenProperty(Str()),
    )
    
    def __init__(self, rewrite=False):
        self._rewrite = rewrite

    def set_up(self):
        print(f"Load web page at {self.given['website']}")

    def form_filled(self, **textboxes):
        for name, contents in sorted(textboxes.items()):
            print(f"Put {contents} in name")

    def clicked(self, name):
        print(f"Click on {name}")
    
    def failing_step(self):
        raise Failure("This was not supposed to happen")
    
    def error_message_displayed(self, expected_message):
        """Demonstrates steps that can rewrite themselves."""
        actual_message = "error message!"
        try:
            strings_match(expected_message, actual_message)
        except Failure:
            if self._rewrite:
                self.current_step.rewrite("expected_message").to(actual_message)
            else:
                raise

    def email_was_sent(self):
        print("Check email was sent!")
>>> from hitchstory import StoryCollection
>>> from pathlib import Path
>>> from engine import Engine
>>> 
>>> StoryCollection(Path(".").glob("*.story"), Engine()).named("Email sent").play()
RUNNING Email sent in /path/to/working/example.story ... Load web page at /login
Put hunter2 in name
Put AzureDiamond in name
Click on login
Click on new email
Put Hey guys,

I think I got hacked!
 in name
Put Cthon98@aol.com in name
Click on send email
Check email was sent!
SUCCESS in 0.1 seconds.

Install

$ pip install hitchstory

Community

Help is available if you ask questions in these places: Github discussions | Github issues (not just for bugs) | Slack channel

Using HitchStory

Every feature of this library is documented and listed below. It is tested and documented with itself.

Using HitchStory: With Pytest

If you already have pytest set up, you can quickly and easily write a test using hitchstory that runs alongside your other pytest tests:

Using HitchStory: Engine

How to use the different features of the story engine:

Using HitchStory: Documentation Generation

How to autogenerate documentation from your tests:

Using HitchStory: Inheritance

Inheriting stories from each other:

Using HitchStory: Runner

Running the stories in different ways:

Approach to using HitchStory

Best practices, how the tool was meant to be used, etc.

Design decisions and principles

Design decisions are justified here:

Why not X instead?

HitchStory is not the only integration testing framework. This is how it compares with the others:

Using HitchStory: Setup on its own

If you want to use HitchStory without pytest:

Using HitchStory: Behavior

Miscellaneous docs about behavior of the framework:

hitchstory's People

Contributors

crdoconnor avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

hitchstory's Issues

Getting FileNotFound errors when running example tests

Hi,
I've recently discovered hitchstory and been trying to get the rewrite test_correct_my_spelling example working.
The issue I'm currently seeing is that when running $ STORYMODE=rewrite ./run.sh pytest -k test_correct_my_spelling I'm getting the error No such file or directory: '/src/tests/docstory.yml.

I'm wondering if this is to do with the recent directory changes?

It appears a lot of the links on that example page are also broken as a result of the directoy changes.

Or is there something I'm doing wrong?

Thanks

the wiki link code to code

I think the wiki link should take me to the wiki, or wiki should be disabled so it doesn't show in the tab list for the project.

Accessing Pytest fixtures from Engine

I'm testing some Django code, and I'd like to use the Django test client. This is available in Pytest via pytest-django providing a client fixture.

It's meant to be used like this:

def test_with_client(client):
    response = client.get('/')
    assert response.content == 'Foobar'

I think the most natural way to adapt this to the Hitchstory approach is to add the decorator to my test_foo Pytest function:

def test_foobar(client):
    hs.named("Something with a client").play()

but it's not obvious there's a clean way to pass the client object through so that the engine can get at it. I think the client is recreated with each test (it potentially holds state) so we don't want to create the client once and include it when instantiating the engine.

`hk regression` and `hk bdd dirty` fail on Arch Linux

I have installed hitchkey with pip install -U hitchkey directly before trying to run tests and I am trying both commands on the current strictyaml master.

❯ pip show hitchkey       
Name: hitchkey
Version: 0.6.1
Summary: HitchKey bootstrapper - set up a development environment.
Home-page: https://hitchtest.readthedocs.org/
Author: Colm O'Connor
Author-email: [email protected]
License: AGPL
Location: /home/daniel/.local/lib/python3.9/site-packages
Requires: 
Required-by: 
❯ hk regression
CommandError

Can't create CommandPath object: /home/daniel/.hitch/0phcf6/py3.7.0/bin does not exist.
RUNNING A YAMLError will be raised if there are syntactic problems, violations of your schema or use of disallowed YAML features/For example, a schema violation in /home/daniel/projects/strictyaml/hitch/story/quickstart.story ... Downloading Python-2.7.14.tar.xz...
-> https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
Installing Python-2.7.14...

BUILD FAILED (Arch rolling using python-build 20180424)

Inspect or clean up the working tree at /tmp/python-build.20210814125339.59626
Results logged to /tmp/python-build.20210814125339.59626.log

Last 10 log lines:
/usr/bin/ld: libpython2.7.a(posixmodule.o): in function `posix_tempnam':
/tmp/python-build.20210814125339.59626/Python-2.7.14/./Modules/posixmodule.c:7589: warning: the use of `tempnam' is dangerous, better use `mkstemp'
./python -E -S -m sysconfig --generate-posix-vars ;\
if test $? -ne 0 ; then \
	echo "generate-posix-vars failed" ; \
	rm -f ./pybuilddir.txt ; \
	exit 1 ; \
fi
generate-posix-vars failed
make: *** [Makefile:514: pybuilddir.txt] Error 1
FAILED in 27.5 seconds.



[1]: function 'set_up'
  /home/daniel/projects/strictyaml/hitch/engine.py

    
        70 :             ).with_python_version(self.given["python version"])\
        71 :              .with_packages({"ruamel.yaml": self.given["ruamel version"]})
    --> 72 :             self.pylibrary.ensure_built()
        73 :             self.python = self.pylibrary.bin.python
    
    

[2]: function 'ensure_built'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuild/build.py

    
        249 :     def ensure_built(self):
        250 :         with BuildContextManager(self):
    --> 251 :             self.build()
        252 : 
    
    

[3]: function 'build'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchpylibrarytoolkit/build.py

    
        56 :     def build(self):
        57 :         pipinstalle = self.virtualenv.incomplete()
    --> 58 :         self.virtualenv.ensure_built()
        59 :         if pipinstalle:
    
    

[4]: function 'ensure_built'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuild/build.py

    
        249 :     def ensure_built(self):
        250 :         with BuildContextManager(self):
    --> 251 :             self.build()
        252 : 
    
    

[5]: function 'build'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuildpy/virtualenv.py

    
        39 :             self.build_path.rmtree(ignore_errors=True)
        40 :             self.build_path.mkdir()
    --> 41 :             self.base_python.build.ensure_built()
        42 :             self.base_python.build.bin.virtualenv(self.build_path).run()
    
    

[6]: function 'ensure_built'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuild/build.py

    
        249 :     def ensure_built(self):
        250 :         with BuildContextManager(self):
    --> 251 :             self.build()
        252 : 
    
    

[7]: function 'build'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuildpy/pyenv.py

    
        22 :             self.build_path.mkdir()
        23 : 
    --> 24 :             Command(
        25 :                 Path(__file__).dirname().abspath().joinpath("bin", "python-build")
    
    

[8]: function 'run'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/commandlib/command.py

    
        234 : 
        235 :         if returncode != 0 and not self._ignore_errors:
    --> 236 :             raise CommandError('"{0}" failed (err code {1})'.format(
        237 :                 self.__repr__(),
    
    

commandlib.exceptions.CommandError
  commandlib exception.
"/home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuildpy/bin/python-build 2.7.14 /home/daniel/.hitch/share/python2.7.14" failed (err code 1)
RUNNING A YAMLError will be raised if there are syntactic problems, violations of your schema or use of disallowed YAML features/For example, a schema violation in /home/daniel/projects/strictyaml/hitch/story/quickstart.story ... Downloading Python-2.7.14.tar.xz...
-> https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
Installing Python-2.7.14...

BUILD FAILED (Arch rolling using python-build 20180424)

Inspect or clean up the working tree at /tmp/python-build.20210814125407.66268
Results logged to /tmp/python-build.20210814125407.66268.log

Last 10 log lines:
/usr/bin/ld: libpython2.7.a(posixmodule.o): in function `posix_tempnam':
/tmp/python-build.20210814125407.66268/Python-2.7.14/./Modules/posixmodule.c:7589: warning: the use of `tempnam' is dangerous, better use `mkstemp'
./python -E -S -m sysconfig --generate-posix-vars ;\
if test $? -ne 0 ; then \
	echo "generate-posix-vars failed" ; \
	rm -f ./pybuilddir.txt ; \
	exit 1 ; \
fi
generate-posix-vars failed
make: *** [Makefile:514: pybuilddir.txt] Error 1
FAILED in 27.6 seconds.



[1]: function 'set_up'
  /home/daniel/projects/strictyaml/hitch/engine.py

    
        70 :             ).with_python_version(self.given["python version"])\
        71 :              .with_packages({"ruamel.yaml": self.given["ruamel version"]})
    --> 72 :             self.pylibrary.ensure_built()
        73 :             self.python = self.pylibrary.bin.python
    
    

[2]: function 'ensure_built'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuild/build.py

    
        249 :     def ensure_built(self):
        250 :         with BuildContextManager(self):
    --> 251 :             self.build()
        252 : 
    
    

[3]: function 'build'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchpylibrarytoolkit/build.py

    
        56 :     def build(self):
        57 :         pipinstalle = self.virtualenv.incomplete()
    --> 58 :         self.virtualenv.ensure_built()
        59 :         if pipinstalle:
    
    

[4]: function 'ensure_built'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuild/build.py

    
        249 :     def ensure_built(self):
        250 :         with BuildContextManager(self):
    --> 251 :             self.build()
        252 : 
    
    

[5]: function 'build'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuildpy/virtualenv.py

    
        39 :             self.build_path.rmtree(ignore_errors=True)
        40 :             self.build_path.mkdir()
    --> 41 :             self.base_python.build.ensure_built()
        42 :             self.base_python.build.bin.virtualenv(self.build_path).run()
    
    

[6]: function 'ensure_built'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuild/build.py

    
        249 :     def ensure_built(self):
        250 :         with BuildContextManager(self):
    --> 251 :             self.build()
        252 : 
    
    

[7]: function 'build'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuildpy/pyenv.py

    
        22 :             self.build_path.mkdir()
        23 : 
    --> 24 :             Command(
        25 :                 Path(__file__).dirname().abspath().joinpath("bin", "python-build")
    
    

[8]: function 'run'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/commandlib/command.py

    
        234 : 
        235 :         if returncode != 0 and not self._ignore_errors:
    --> 236 :             raise CommandError('"{0}" failed (err code {1})'.format(
        237 :                 self.__repr__(),
    
    

commandlib.exceptions.CommandError
  commandlib exception.
"/home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuildpy/bin/python-build 2.7.14 /home/daniel/.hitch/share/python2.7.14" failed (err code 1)

❯ hk bdd dirty
RUNNING Dirty load/Flow style mapping in /home/daniel/projects/strictyaml/hitch/story/dirty-load.story ... Downloading Python-3.7.0.tar.xz...
-> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
Installing Python-3.7.0...
Installed Python-3.7.0 to /home/daniel/.hitch/share/python3.7.0

FAILED in 108.9 seconds.



[1]: function 'set_up'
  /home/daniel/projects/strictyaml/hitch/engine.py

    
        70 :             ).with_python_version(self.given["python version"])\
        71 :              .with_packages({"ruamel.yaml": self.given["ruamel version"]})
    --> 72 :             self.pylibrary.ensure_built()
        73 :             self.python = self.pylibrary.bin.python
    
    

[2]: function 'ensure_built'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuild/build.py

    
        249 :     def ensure_built(self):
        250 :         with BuildContextManager(self):
    --> 251 :             self.build()
        252 : 
    
    

[3]: function 'build'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchpylibrarytoolkit/build.py

    
        56 :     def build(self):
        57 :         pipinstalle = self.virtualenv.incomplete()
    --> 58 :         self.virtualenv.ensure_built()
        59 :         if pipinstalle:
    
    

[4]: function 'ensure_built'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuild/build.py

    
        249 :     def ensure_built(self):
        250 :         with BuildContextManager(self):
    --> 251 :             self.build()
        252 : 
    
    

[5]: function 'build'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuildpy/virtualenv.py

    
        39 :             self.build_path.rmtree(ignore_errors=True)
        40 :             self.build_path.mkdir()
    --> 41 :             self.base_python.build.ensure_built()
        42 :             self.base_python.build.bin.virtualenv(self.build_path).run()
    
    

[6]: function 'ensure_built'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuild/build.py

    
        249 :     def ensure_built(self):
        250 :         with BuildContextManager(self):
    --> 251 :             self.build()
        252 : 
    
    

[7]: function 'build'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/hitchbuildpy/pyenv.py

    
        30 :                 self.bin.easy_install("--upgrade", "pip").run()
        31 :             else:
    --> 32 :                 self.bin.pip("install", "pip", "--upgrade").run()
        33 :             self.bin.pip("install", "virtualenv", "--upgrade")\
    
    

[8]: function 'run'
  /home/daniel/.hitch/0phcf6/hvenv/lib/python3.9/site-packages/commandlib/command.py

    
        234 : 
        235 :         if returncode != 0 and not self._ignore_errors:
    --> 236 :             raise CommandError('"{0}" failed (err code {1})'.format(
        237 :                 self.__repr__(),
    
    

commandlib.exceptions.CommandError
  commandlib exception.
"/home/daniel/.hitch/share/python3.7.0/bin/pip install pip --upgrade" failed (err code -11)

It looks like it is trying to install a specific python version and is failing... Is there a way to tell it to use the system python version?

Contributing Guide

Hi @crdoconnor

I'm thinking of using hitchstory for another project (remember we met at Babylon).

I was going to run through the tests and check everything was working with Python 3.9 (maybe 3.10). What's the best way for me to get setup for contributing? Happy to write this up in a contributing guide.

Cheers!

untrue statement on https://hitchdev.com/strictyaml/why/implicit-typing-removed/

Hi there hitchdev!

I found no better place to send in the following pedantry, so here it goes:

As of today (2023-02-25), the webpage https://hitchdev.com/strictyaml/why/implicit-typing-removed/ demonstrates the "The Norway Problem", where the YAML specification directs the parsing of values (without surrounding quotes) like GB, IE, FR, and DE into strings, but to the surprise of many users mandates parsing the value (again without surrounding quotes) NO, the two letter code for Norway, as boolean false.
Further down, also the following statement is made:

The most tragic aspect of this bug, however, is that it is intended behavior according to the YAML 1.2 specification.

Where the last part of the sentence is a hyperlink to https://yaml.org/spec/1.2.2/

However,

  1. for the 1.2.2 specification, i could not find the paragraph where the string "NO" is a valid format of the boolean true.

  2. The 1.2.2 changelog of the specification (link here: https://yaml.org/spec/1.2.2/ext/changes/ ) explicitely lists the change, that starting with the revision 1.2.0 (2009-07-21):

    Only true and false strings are parsed as booleans (including True and TRUE); y, yes, on, and their negative counterparts are parsed as strings.

Notwithstanding all the other aspects of YAML that justify StrictYaml to refuse to parse certain features of YAML, this particular statement on the homepage, that NO (without quotes) shall be parsed into a boolean false when pedanticly following the 1.2.2 revision of the YAML-Spec is not true.

I would suggest that

  • The last part of the above quoted sentence on the homepage is changed to "YAML 1.1 specification";
  • the hyperlink points to the YAML 1.1.0 revision;
  • a note is added to the sentence that this particular problem is not present in the YAML 1.2 specification any longer [*].

best regards,

Max

[*] which means that some valid yaml documents are parsed differently with a yaml-1.1 parser and a yaml-1.2 parser, and this demonstrates another aspect of the YAML spec to be on the lookout for.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.