This article uses a simple blog application as an example to show you how to use Htte for automated testing of your API.
Htte introduction
Htte is an automated testing framework. It allows you to write tests using YAML without having to write code. The difficulty is low, the hand is quick.
Writing a test lists the request data and the expected response data. The HTTE automatically generates the request for you and verifies that the response data matches expectations.
It provides plug-ins, so there is no data that cannot be generated and represented, no data that cannot be checkmated, and no scenario that cannot be tested.
Using data from other tests has always been a pain in interface automation testing. Postman handles this problem with environment variables, which is tedious and error-prone, while Htte innovatively uses session and path reference data to handle this problem safely and easily.
Htte is an excellent interface automation test tool, worth a try.
The installation
Install htTE command line using NPM:
npm i -g htte
Copy the code
Interface details
There’s a super simple blog app. It has only four ports.
- Login interface
User login request path: localhost:3000/login Request method: POST Request header: {"Content-Type": "application/json"} Request data: {"username": "john"."password": "johnsblog"} Response: - Condition: username and password correct Response status code: 200 Response data: {"name": "john"."token": "..."} - Condition: username or password incorrect Response status code: 401 Response data: {"errcode": 11001, "message": "invalid username or password"}
Copy the code
- Add a blog
Localhost :3000/articles: POST header: {"Content-Type": "application/json"."Authorization": "Bearer ..."} request data: > {"title": "How to use htte"."content": "htte is ...."."tags": ["howto"."htte"]} Response: - Status: added successfully Response status code: 200 Response data: > {"title": "How to use htte"."slug": "how-to-use-htte-A43bcF"."content": "htte is ...."."tags": ["howto"."htte"]."createdAt": "The 2018-04-28 T07:35:08. 471 z"
}
Copy the code
- Edit the post
Function: edit post request path: localhost: 3000 / article/how-to - use - htte - A43b request method: PUT request header: {"Content-Type": "application/json"."Authorization": "Bearer ..."} request data: > {"title": "How to create htte plugin"."content": "htte plugin is ...."."tags": ["howto"."htte"."plugin"]} Response: - Status: editing success Response status code: 200 Response data: > {"title": "How to use htte plugin"."slug": "how-to-use-htte-A43b"."content": "htte plugin is ...."."tags": ["howto"."htte"."plugin"]."createdAt": "The 2018-04-28 T07:35:08. 471 z"
}
Copy the code
- Get a list of blog posts
Localhost :3000/articles GET header: {"Content-Type": "application/json"."Authorization": "Bearer ..."} QueryString: tag= HOWTO response: - Status: successful response status code: 200 Response data: {"articlesCount": 1,
"articles": [{"title": "How to use htte plugin"."slug": "how-to-use-htte-A43b"."content": "htte plugin is ...."."tags": ["howto"."htte"."plugin"]."createdAt": "The 2018-04-28 T07:35:08. 471 z"}}]Copy the code
The test interface
Add the configuration file.htte.yaml to describe the interface
url: http://localhost:3000
type: json Decompress the request response data using JSON
apis:
login:
method: post
uri: /login
createArticle:
method: post
uri: /articles
updateArticle:
method: put
uri: /article/{slug}
listArticles: /articles
Copy the code
Add the test file blog.yaml and write the tests.
Test login failure
units:
- describe: Login failed
api: login
req:
body:
username: john
password: johnblog
res:
status: 401
body:
errcode: 11001
message: ! @exist
Copy the code
Describe describes the purpose of the test. This field is presented in the best test reports.
API describes the interface being tested, and this field is associated with the interface definition in the configuration file.
Req defines the request data. The data under body is encapsulated in the request body {“username”: “John “, “password”: “johnblog”}.
Res Expected response data. If it does not match the actual response data, the test will fail.
When htte loaded during this test code, structure request, namely will send a post request to http://localhost:3000/login, a request header {” content-type “: {“username”: “John “, “password”: “johnblog”}} When the server responds, it makes the following assertions about the data
- The response status code is 401
- The response body is an object
- The response body object has two and only two properties
errcode
和message
- Of the response body object
errcode
The property value is 11001 - Of the response body object
message
Attribute values forinvalid username or password
So if all assertions pass, the test will pass. If the response status code is not 401 or there are other fields in the response body, the test fails.
Test Login success
- describe: Login successful
api: login
name: johnLogin
req:
body:
username: john
password: johnsblog
res:
body:
username: john
token: ! @exist
Copy the code
Name defines the test name by which subsequent tests can access the test data, such as the token value of the response.
The omission of status in res does not mean that the status code is not checked. In this case HTTE checks to see if the status code is in the 200-299 range.
The token value is variable, and we check its value. So use YAML tags! @exist performs a special kind of check. It verifies the existence of the token field but does not verify its value.
! @exist is provided by the HTTE plug-in. Use it when you only care about the presence of a field and not the value.
Test adding a blog post
- describe: Adding a blog post succeeded
api: createArticle
name: articleUsehtte
req:
headers:
Authorization: ! $concat ['Bearer', ' '. ! $query ? johnLogin.res.body.token]
body:
title: How to use htte
content: htte is http automation testing tool
tags:
- howto
- htte
res:
body:
title: How to use htte
slug: ! @regexp /^how-to-use-htte/
content: htte is http automation testing tool
tags:
- howto
- htte
createdAt: ! @exist
Copy the code
Headers Describes the request header. It appends the Authorization header to the sent request header.
Adding the blog interface will perform JWT permission verification, so we need to add the Authorization header to provide login tokens.
This token can be obtained from the johnLogin test. We pass the tag! $query references this token value.
The HTTE logs request and response data for tests that have been run through the session. This way you can retrieve data for tests that are in front of you. There is no need to pre-define, just locate (access path).
Then use the tag! $concat concatenates Bearer and token worthy of specific Authorization values.
Slug represents an access link to a blog post, generated by title concatenation of a random 4-character string. It is also not a concrete value, so we use! @regexp does a positive match verification.
In most cases, the test request data and expected response data can be given, but there are always special situations that require plug-ins. Htte has many built-in plug-ins, which should be enough to handle most of the common ones. If you encounter a situation that you can’t handle, issue an issue or write a plug-in.
Test edit blog post
- describe: Edit blog post successfully
api: updateArticle
req:
headers:
Authorization: ! $concat ['Bearer', ' '. ! $query ? johnLogin.res.body.token]
params:
slug: ! $query ? articleUsehtte.res.body.slug
res:
body:
title: How to use htte plugin
slug: ! @query ? $req.params.slug
content: htte plugin is flexiabe
tags:
- howto
- htte
- plugin
createdAt: 2018- 04- 28T07:35:08. 471 z
Copy the code
The request path for this test is /article/{slug} with the path parameter slug. Htte will replace it with the corresponding value in Params before sending the request. The final request path is /article/ how-to-use-htte-a43bcf
! The @query tag checks whether the response value is equal to a reference value to determine whether the updated article or slug has changed.
Test to get a list of blog posts
Get all my blog posts
- describe: Get all my blog posts
api: listArticles
req:
headers:
Authorization: ! $concat ['Bearer', ' '. ! $query ? johnLogin.res.body.token]
res:
body:
articlesCount: ! @exist
articles: ! @array
0:
title: How to use htte plugin
slug: ! @regexp /^how-to-use-htte/
content: htte plugin is flexiabe
tags:
- howto
- htte
- plugin
createdAt: ! @exist
Copy the code
! Be careful with @array. Htte normally compares an array with the number of elements first, and then one element at a time. However, articles may be an array of variable length, in which case regular alignment is meaningless. Use! @array Customizes the array alignment behavior so that only certain elements are compared. For example, in this test, only the first element is compared and other elements are ignored. If the element matches, the test passes.
Select my posts by tagging them
- describe: Filter my posts by tags
api: listArticles
req:
headers:
Authorization: ! $concat ['Bearer', ' '. ! $query ? johnLogin.res.body.token]
query:
tag: howto
res:
body:
articlesCount: ! @exist
articles: ! @array
0:
title: How to use htte plugin
slug: ! @regexp /^how-to-use-htte/
content: htte plugin is flexiabe
tags:
- howto
- htte
- plugin
createdAt: ! @exist
Copy the code
The query to describe the querystring, the url will be http://localhost:3000/articles? Tag = howto.
The command line
With the Web available, run the htte command in the directory where the.htte.yaml and blog.yaml reside, and the htte will run the tests one by one. And print the results of the test execution.
Some htTE command lines have some useful tips:
- Suspend execution when a test fails
htte --bail
Copy the code
- Start from where you failed last time, suitable for interface debugging
htte --bail --amend
Copy the code
- Run only the specified tests
htte --unit blog-articleUsehtte --shot
Copy the code
- Displays debugging information and synchronously prints request and response data
htte --debug
Copy the code
conclusion
You should be familiar with Htte from this blog application interface test case with only 4 interfaces, that’s all. Htte automatically generates data, sends requests, compares response data, orchestrates and executes test cases, and prints reports.
Testing is using YAML to describe the request and response of the interface. Plug-ins are provided for the very few scenarios that cannot be described. You have the advantage of writing tests with descriptive policies, but you also have the flexibility that plug-ins provide.
Project address: github.com/sigoden/htt…