Skip to content

Commit

Permalink
Merge pull request #2 from naodeng/main
Browse files Browse the repository at this point in the history
add dataDriving demo and multiEnv demo
  • Loading branch information
naodeng authored Nov 9, 2023
2 parents 6edc64e + 735fcae commit 2515fb6
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 73 deletions.
6 changes: 6 additions & 0 deletions Config/testConfig-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Test config file for dev environment
module.exports = {
host: 'https://jsonplaceholder.typicode.com', // Test endpoint
getAPI: '/posts/1', // Test GET API URL
postAPI: '/posts', // Test POST API URL
};
6 changes: 6 additions & 0 deletions Config/testConfig-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Test config file for test environment
module.exports = {
host: 'https://jsonplaceholder.typicode.com', // Test endpoint
getAPI: '/posts/1', // Test GET API URL
postAPI: '/posts', // Test POST API URL
};
6 changes: 6 additions & 0 deletions Config/testConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Test config file
module.exports = {
host: 'https://jsonplaceholder.typicode.com', // Test endpoint
getAPI: '/posts/1', // Test GET API URL
postAPI: '/posts', // Test POST API URL
};
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
<div align="right"><strong>🇨🇳中文</a></strong> | <strong><a href="./README_EN.md">🇬🇧English</strong></div>
<div align="right"><strong><a href="README_ZH.md">🇨🇳中文</a></strong> | <strong>🇬🇧English</strong></div>

# SuperTest-Jest-demo
a SuperTest API automation testing demo project with Jest

一个使用 Jest 的 SuperTest API 自动化测试演示项目
## Environment preparation
- Nodejs, I'm using v21.1.0.
- SuperTest, I'm using version 6.3.3.
- Jest, I'm using the latest version 29.7.0.

## 环境准备
- Nodejs ,我使用的 v21.1.0
- SuperTest 我使用的 6.3.3 版本
- Jest 我使用的是最新的 29.7.0 版本

## 技术栈
## Tech stack
- SuperTest
- Jest
- jest-html-reporters
- GitHub action

## 项目结构
## Project structure

```Text
SuperTest-Jest-demo
├── README.md
├── package.json
├── package-lock.json
├── Config // 测试配置文件
├── Config // TEST configuration file
│ └── config.js
├── Specs // 测试用例文件
├── Config // TEST data file
│ └── requestData.js
│ └── responseData.js
├── Specs // TEST case file
│ └── test.spec.js
├── Utils // 测试工具文件
├── Utils // TEST tool file
│ └── utils.js
├── Report // 测试报告文件
├── Report // TEST report file
│ └── report.html
├── .gitignore
└── node_modules // 项目依赖
└── node_modules // Project dependencies
```

## 项目运行
## HOW TO RUN

```bash
node run test
```
## 测试报告截图
## Test report screenshot

![12ZreT](https://cdn.jsdelivr.net/gh/naodeng/blogimg@master/uPic/12ZreT.png)

## 添加API测试用例
## Add API test cases

- Just add a new test case in the Specs directory.

- 在 Specs目录下新加测试用例即可
## More info

## 更多信息
- [Project Tutorial](https://github.com/Automation-Test-Starter/SuperTest-API-Test-Starter)

- [项目教程](https://github.com/Automation-Test-Starter/SuperTest-API-Test-Starter)
52 changes: 0 additions & 52 deletions README_EN.md

This file was deleted.

55 changes: 55 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<div align="right"><strong>🇨🇳中文</a></strong> | <strong><a href="README.md">🇬🇧English</strong></div>

# SuperTest-Jest-demo

一个使用 Jest 的 SuperTest API 自动化测试演示项目

## 环境准备
- Nodejs ,我使用的 v21.1.0
- SuperTest 我使用的 6.3.3 版本
- Jest 我使用的是最新的 29.7.0 版本

## 技术栈
- SuperTest
- Jest
- jest-html-reporters
- GitHub action

## 项目结构

```Text
SuperTest-Jest-demo
├── README.md
├── package.json
├── package-lock.json
├── Config // 测试配置文件
│ └── config.js
├── Config // 测试数据文件
│ └── requestData.js
│ └── responseData.js
├── Specs // 测试用例文件
│ └── test.spec.js
├── Utils // 测试工具文件
│ └── utils.js
├── Report // 测试报告文件
│ └── report.html
├── .gitignore
└── node_modules // 项目依赖
```

## 项目运行

```bash
node run test
```
## 测试报告截图

![12ZreT](https://cdn.jsdelivr.net/gh/naodeng/blogimg@master/uPic/12ZreT.png)

## 添加API测试用例

- 在 Specs目录下新加测试用例即可

## 更多信息

- [项目教程](https://github.com/Automation-Test-Starter/SuperTest-API-Test-Starter)
31 changes: 31 additions & 0 deletions Specs/dataDrivingTest.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Test: dataDrivingTest.spec.js
const request = require('supertest');

const config = require('../Config/testConfig'); // import test config
const requestData = require('../TestData/requestData'); // import request data
const responseData = require('../TestData/responseData'); // import response data

// Test Suite
describe('Data Driving-Verify that the Get and POST API returns correctly', () => {
// Test case 1
it('Data Driving-Verify that the GET API returns correctly', async () => {
const res = await request(config.host) // Test endpoint
.get(config.getAPI) // API endpoint
.send(requestData.getAPI) // request body
.expect(200); // use supertest's expect to verify that the status code is 200
// user jest's expect to verify the response body
expect(res.status).toBe(200); // Verify that the status code is 200
expect(res.body).toEqual(responseData.getAPI); // Verify that the id is 1
});

// Test case 2
it('Data Driving-Verify that the POST API returns correctly', async() =>{
const res = await request(config.host) // Test endpoint
.post(config.postAPI) // API endpoint
.send(requestData.postAPI) // request body
.expect(201); // use supertest's expect to verify that the status code is 201
// user jest's expect to verify the response body
expect(res.statusCode).toBe(201);
expect(res.body).toEqual(responseData.postAPI);
});
});
31 changes: 31 additions & 0 deletions Specs/multiEnvTest.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Test: multiEnvTest.spec.js
const request = require('supertest');

const config = process.env.NODE_ENV === 'test' ? require('../Config/testConfig-test') : require('../Config/testConfig-dev'); // import test config
const requestData = process.env.NODE_ENV === 'test' ? require('../TestData/requestData-test') : require('../TestData/requestData-dev'); // import request data
const responseData= process.env.NODE_ENV === 'test' ? require('../TestData/responseData-test') : require('../TestData/responseData-dev'); // import response data

// Test Suite
describe('multiEnv-Verify that the Get and POST API returns correctly', () => {
// Test case 1
it('multiEnv-Verify that the GET API returns correctly', async () => {
const res = await request(config.host) // Test endpoint
.get(config.getAPI) // API endpoint
.send(requestData.getAPI) // request body
.expect(200); // use supertest's expect to verify that the status code is 200
// user jest's expect to verify the response body
expect(res.status).toBe(200); // Verify that the status code is 200
expect(res.body).toEqual(responseData.getAPI); // Verify that the id is 1
});

// Test case 2
it('multiEnv-Verify that the POST API returns correctly', async() =>{
const res = await request(config.host) // Test endpoint
.post(config.postAPI) // API endpoint
.send(requestData.postAPI) // request body
.expect(201); // use supertest's expect to verify that the status code is 201
// user jest's expect to verify the response body
expect(res.statusCode).toBe(201);
expect(res.body).toEqual(responseData.postAPI);
});
});
9 changes: 9 additions & 0 deletions TestData/requestData-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Test request data file for dev environment
module.exports = {
getAPI: '', // request data for GET API
postAPI:{
"title": "foo",
"body": "bar",
"userId": 1
}, // request data for POST API
};
9 changes: 9 additions & 0 deletions TestData/requestData-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Test request data file for dev environment
module.exports = {
getAPI: '', // request data for GET API
postAPI:{
"title": "foo",
"body": "bar",
"userId": 1
}, // request data for POST API
};
9 changes: 9 additions & 0 deletions TestData/requestData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Test request data file
module.exports = {
getAPI: '', // request data for GET API
postAPI:{
"title": "foo",
"body": "bar",
"userId": 1
}, // request data for POST API
};
15 changes: 15 additions & 0 deletions TestData/responseData-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Test response data file for dev environment
module.exports = {
getAPI: {
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}, // response data for GET API
postAPI:{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}, // response data for POST API
};
15 changes: 15 additions & 0 deletions TestData/responseData-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Test response data file for dev environment
module.exports = {
getAPI: {
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}, // response data for GET API
postAPI:{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}, // response data for POST API
};
15 changes: 15 additions & 0 deletions TestData/responseData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Test response data file
module.exports = {
getAPI: {
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}, // response data for GET API
postAPI:{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}, // response data for POST API
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "a SuperTest demo project with JEST AND TypeScript",
"main": "index.js",
"scripts": {
"test": "jest"
"test": "NODE_ENV=test jest",
"dev": "NODE_ENV=dev jest"
},
"keywords": [],
"author": "",
Expand Down

0 comments on commit 2515fb6

Please sign in to comment.