This repository provides some working boilerplate code for building automated test suites on top of the lemoncheesecake test framework and uses the "component-tests" model for functional testing of RESTful APIs.
- Uses lemoncheesecake 1.x as a core functional test framework
- Provides awesome and highly readable HTML reports with request and response details
- Provides pre-configured Slack reporting which posts the report link(s) at suite level to a specific Slack channel; requires only
SLACK_AUTH_TOKEN
and channel name - Provides a way to define different base URL based test environment configurations
- Provides a way to run one or more of your test suites in a single run serially
- The
components
package contains all the individual component files, e.g.user.py
. The scope of a component file is to define various HTTP methods pertaining to an individual REST entity under test. - The
core
package contains various other packages likecommon
,conf
,utils
, etc. -- Thecommon
package contains modules likeendpoint_constants.py
(used to define various HTTP endpoints to be tested and used in a component file),header.py
(used to define aHeader
class for passing request headers while making a HTTP request in a component file),init.py
(used to initialise all the base URLs of APIs under test using aConfig
object) andrequest.py
(a wrapper around Python'srequests
module's HTTP methods with additionallemoncheesecake
logging). -- Theconf
package contains anenvironment
package, which lets you define various test environments (base URLs for the most part). Note that theconfig.py
uses Python'simportlib
module to define aconfig
object using theTEST_ENV
environment variable set while running the tests. -- Theutilities
package lets you define various utility functions. - The
scripts
directory contains various ad-hoc scripts, e.g.test_ping.py
(used to check if all the endpoints under test are up). - The
tests
package contains the actual API tests. - The
entrypoint.sh
script does reporting related tasks and uses anotherrun.sh
script which is run per test suite. You must edit various variables likeLCC_SLACK_AUTH_TOKEN
,LCC_SLACK_CHANNEL
,ALL_TEST_SUITES
as per your needs. - In the
run.sh
script, your must edit various variables likeSERVER_URL
,WWW_REPORTS_DIR
,VENV_NAME
as per your needs.
You can obtain a Slack API access token for your workspace by following the steps below:
- In your Slack Workspace, click the Apps section.
- In the Apps page, click Manage apps.
- The App Directory page shows up, in this page, make a search using the keyword “bots” in the top text box Search App Directory.
- Click Bots app > Add configuration.
- Set Username and click Add bot integration.
- You’ll get the API access token in Integration Settings.
This repository contains below examples to run some simple API tests from the endpoints from https://jsonplaceholder.typicode.com/ in the test suite file tests/user_tests.py
. The required base URLs and actual endpoints can be found in core/conf/environments/default.py
and core/common/endpoint_constants.py
files respectively.
verify_get_all_users
- Makes a GET API call to the/users
endpoint and checks if the number of users returned in the response in greater than 1.verify_get_user_details
- Makes a GET API call to the/users
endpoint by passing a givenuserId
in the path parameters of the API request and checks if theid
returned in the response is same as that in the request.verify_create_user
- Makes a POST API call to the/users
endpoint by passing the required payload to the request and checks if the user gets created successfully.
-
Ensure Python3 and other required tools (
pip
andvirtualenv
) are installed on your machine. If not, install them.a. Install Python with Homebrew:
brew install python3
b. Install
pip
: Downloadpip
from https://bootstrap.pypa.io/get-pip.py and runpython3 ~/Downloads/get-pip.py
c. Install
virtualenv
:pip install virtualenv
-
Clone this repository.
-
Create a virtual environment in the root directory of this project. Note that the name of this virtual environment (
pyrestest-venv
in this case) should be the same as that in therun.sh
and.gitignore
files.virtualenv pyrestest-venv
-
Activate the virtual environment we created in step 2.
source pyrestest-venv/bin/activate
-
Install all the project dependencies.
pip install -r requirements.txt
-
Set the required environment variables and run the tests with
entrypoint.sh
script.TEST_ENV=default PYTHONPATH=<your_project_root_dir> ./entrypoint.sh
TEST_ENV
- A suitable configuration defined in any of the configuration modules in theenvironment
package. Not setting this will make the test framework use thedefault
configuration defined in theenvironment
package since we have defined this underconfig.py
.- Providing no arguments to
entrypoint.sh
will run all the test suites defined in theALL_TEST_SUITES
shell variable in the given order. In order to run one or more test suites in a custom order, you can useentrypoint.sh my_test_suite_1 my_test_suite_2
(note no.py
extension in the test suite names). - If the
LCC_SLACK_AUTH_TOKEN
andLCC_SLACK_CHANNEL
specified inentrypoint.sh
are valid, you should see a message in your Slack channel with test suite name, number of passed/failed tests and report link.
That's all, folks. Happy testing!