Package: py-http-auto-test
py-http-auto-test: Python HTTP automated testing facilityWhat's this?This is a simple HTTP testing library for Python.The main idea is to be able to write simple YAML files that describe a suite of HTTP tests, and then run them either as a standalone script or as apytesttest suite, with the providedconftest.pyplugin.A YAML test suite might look like this:base_url:https://mysite.example.comtests:-url:"/"description:"Fetchtheindexpage"match:status:200body:-"<!DOCTYPEhtml>"-url:"/favicon.ico"description:"Ensurethefaviconfileispresentandservedwiththecorrectcontenttype"match:headers:-"content-type:image/x-icon"status:200-...Why?Once upon a time, there washttp-cukewhich I made as an attempt to free myself of having to write tests. At that time, people believed that BDD/cucumber would enable not-necessarily technical people to write business logic test suites. I have personally never seen this happen.Instead of making the same mistake again, and learning from the past years of experience testing systems, I tried to write something that:makes use of a solid HTTP library: libcurl, pycurlcovers websockets testing.pycurl doesn't yetminimizes the amount of code one needs to write to run a test suiteSome Use-CasesWhen building a new package image/AMI, it's nice to be able to spawn a new instance and get a simple and clear answer to the question:Can I online this instance? Will things work?Regression testing a web applicationLive testing of a running system every x minutesRequirementsSee therequirements.txtfile for the full list of requirements. The main ones are:Python 3.8 or laterThelibcurl4-openssl-devpackage on Debian-based systems, orlibcurl-develor whatever the equivalent on your system is, required to buildpycurlpycurlversion 7+websocketslibrary to run tests against websocketsOptional Requirementspytestif you want to run http tests as a pytest test suiteInstallationsudoapt-getupdate-qq# You need libcurl to build and install pycurlsudoapt-getinstall-ylibcurl4-openssl-dev
pipinstall--upgradepy-http-auto-testUsageThere are two main ways to use this library:standalonepytestStandaloneThis distribution provides ahttp-test-runner.pyscript (usually installed in/usr/local/bin, or in your virtualenv'sbinfolder) that can be used to run one or more YAML test files.Examples:http-test-runner.py--help

http-test-runner.py--test-filewebsite.yaml# Run the tests but against an alternative IP address# (this is similar to `MAP * <ip>` in Chrome or `--connect-to *:*:<ip>` in curl)http-test-runner.py--test-filewebsite.yaml--target-host127.0.0.1The library also supports template variables in the YAML files. This is useful if you want to define a set of tests for your web application, and then run them against different environments, for example:---# YAML test filebase_url:"https://{{hostname}}"tests:-url:"/"description:"Indexpageexistsandit'sservedcorrectly"...and then:http-test-runner.py--test-filewebsite.yaml--var"hostname=test.mydomain.com"The{{ hostname }}variable will be replaced withtest.mydomain.comin the test suite.The template format isjinja2.pytestAll that was described for the standalone case works also as a pytest test suite. You need to make sure the providedconftest.pyplugin is found when you runpytest.Example:pytest-v./test.yamlIf you need to pass on arguments or template variables, you can do so using environment variables prefixed with the stringHTTPTEST_, as in:HTTPTEST_TARGET_HOST="127.0.0.1"HTTPTEST_TEMPLATE_VARS="hostname=test.mydomain.com"pytest-v./test.yamlTo pass on multiple environment variables, use a space character to separate multiple values, as in:HTTPTEST_TARGET_HOST="127.0.0.1"HTTPTEST_TEMPLATE_VARS="hostname=test.mydomain.com protocol=https"pytest-v./test.yamlComprehensive Example of a Single TestAn HTTP test has a bunch of attributes, some of which are optional. The following is a semi-complete example:-url:"/index.html"description:"Verifythatwecancorrectlydownloadtheindexpageasgzip-compressedresponse"headers:-"accept:text/html"-"accept-encoding:gzip"verbose:true# false is the defaulthttp2:true# false (http/1.1) is the default# Here is where you specify the test requirementsmatch:# Response HTTP status must be 200 for the test to pass# This can also be a list of status codesstatus:200# Verify that the response matches the following headers.# Header names will be matched regardless of upper or lower case.headers:-"content-type:text/html"-"content-encoding:gzip"# Verify that the response body contains the specified string patternsbody:-"<!DOCTYPEhtml>"-"<h1>Hello</h1>"# Verify that the request took less than 500mstiming:500ms