Package: tavern
Easier API testingTavern is a pytest plugin, command-line tool and Python library for
automated testing of APIs, with a simple, concise and flexible
YAML-based syntax. It's very simple to get started, and highly
customisable for complex tests. Tavern supports testing RESTful APIs as
well as MQTT based APIs.The best way to use Tavern is withpytest. Tavern comes with a
pytest plugin so that literally all you have to do is install pytest and
Tavern, write your tests in.tavern.yamlfiles and run pytest. This
means you get access to all of the pytest ecosystem and allows you to do
all sorts of things like regularly run your tests against a test server
and report failures or generate HTML reports.You can also integrate Tavern into your own test framework or continuous
integration setup using the Python library, or use the command line
tool,tavern-ciwith bash scripts and cron jobs.To learn more, check out theexamplesor the completedocumentation. If you're interested in contributing
to the project take a look at theGitHub
repo.QuickstartFirst up runpip install tavern.Then, let's create a basic test,test_minimal.tavern.yaml:---# Every test file has one or more tests...test_name:Get some fake data from the JSON placeholder API# ...and each test has one or more stages (e.g. an HTTP request)stages:-name:Make sure we have the right ID# Define the request to be made...request:url:https://jsonplaceholder.typicode.com/posts/1method:GET# ...and the expected response code and bodyresponse:status_code:200json:id:1userId:1title:"suntautfacererepellatprovidentoccaecatiexcepturioptioreprehenderit"body:"quiaetsuscipit\nsuscipitrecusandaeconsequunturexpeditaetcum\nreprehenderitmolestiaeututquastotam\nnostrumrerumestautemsuntremevenietarchitecto"This file can have any name, but if you intend to use Pytest with
Tavern, it will only pick up files calledtest_*.tavern.yaml.This can then be run like so:$pipinstalltavern[pytest]$py.testtest_minimal.tavern.yaml-v===================================testsessionstarts===================================platformlinux--Python3.5.2,pytest-3.4.2,py-1.5.2,pluggy-0.6.0--/home/taverntester/.virtualenvs/tavernexample/bin/python3
cachedir:.pytest_cache
rootdir:/home/taverntester/myproject,inifile:
plugins:tavern-0.7.2
collected1item

test_minimal.tavern.yaml::GetsomefakedatafromtheJSONplaceholderAPIPASSED[100%]================================1passedin0.14seconds=================================It is strongly advised that you use Tavern with Pytest - not only does
it have a lot of utility to control discovery and execution of tests,
there are a huge amount of plugins to improve your development
experience. If you absolutely can't use Pytest for some reason, use thetavern-cicommand line interface:$pipinstalltavern
$tavern-ci--stdouttest_minimal.tavern.yaml2017-11-0816:17:00,152[INFO]:(tavern.core:55)Runningtest:GetsomefakedatafromtheJSONplaceholderAPI2017-11-0816:17:00,153[INFO]:(tavern.core:69)Runningstage:MakesurewehavetherightID2017-11-0816:17:00,239[INFO]:(tavern.core:73)Response:'<Response [200]>'({"userId":1,"id":1,"title":"sunt aut facere repellat provident occaecati excepturi optio reprehenderit","json":"quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"})2017-11-0816:17:00,239[INFO]:(tavern.printer:9)PASSED:MakesurewehavetherightID[200]Why not Postman, Insomnia or pyresttest etc?Tavern is a focused tool which does one thing well: automated testing of
APIs.PostmanandInsomniaare excellent tools which cover a wide
range of use-cases for RESTful APIs, and indeed we use Tavern alongside
Postman. However, specifically with regards to automated testing, Tavern
has several advantages over Postman:A full-featured Python environment for writing easily reusable custom validation functionsTesting of MQTT based systems in tandem with RESTful APIS.Seamless integration with pytest to keep all your tests in one placeA simpler, less verbose and clearer testing languageTavern does not do many of the things Postman and Insomnia do. For
example, Tavern does not have a GUI nor does it do API monitoring or
mock servers. On the other hand, Tavern is free and open-source and is a
more powerful tool for developers to automate tests.pyresttestis a similar tool to Tavern for testing RESTful APIs, but
is no longer actively developed. On top of MQTT testing, Tavern has
several other advantages over PyRestTest which overall add up to a
better developer experience:Cleaner test syntax which is more intuitive, especially for
non-developersValidation function are more flexible and easier to useBetter explanations of why a test failedHacking on TavernIf you want to add a feature to Tavern or just play around with it
locally, it's a good plan to first create a local development
environment (this
pagehas a
good primer for working with development environments with Python).
After you've created your development environment, justpip install toxand runtoxto run the unit tests. If you want
to run the integration tests, make sure you havedockerinstalled and runtox -c tox-integration.ini(bear in mind this might take a while.)
It's that simple!If you want to develop things in tavern, enter your virtualenv and runpip install -r requirements.txtto install the library, any requirements,
and other useful development options.Tavern usesblackto keep all of the code
formatted consistently. There is a pre-commit hook to run black which can
be enabled by runningpre-commit install.If you want to add a feature to get merged back into mainline Tavern:Add the feature you wantAdd some tests for your feature:If you are adding some utility functionality such as improving verification
of responses, adding some unit tests might be best. These are in thetests/unit/folder and are written using Pytest.If you are adding more advanced functionality like extra validation
functions, or some functionality that directly depends on the format of the
input YAML, it might also be useful to add some integration tests. At the
time of writing, this is done by adding an example flask endpoint intests/integration/server.pyand a corresponding Tavern YAML test file in
the same directory. This will be cleaned up a bit once we have a proper
plugin system implemented.Open apull request.SeeCONTRIBUTING.mdfor more details.AcknowledgementsTavern makes use of several excellent open-source projects:pytest, the testing
framework Tavern intergrates withrequests, for HTTP
requestsYAMLandpyyaml, for the test syntaxpykwalify, for YAML schema
validationpyjwt, for decoding JSON Web
Tokenscolorlog, for
formatting terminal outputspaho-mqtt, for
sending MQTT messagesMaintenanceTavern is currently maintained by@michaelboulton