https://github.com/pactumjs/pactum
REST API Testing Tool for all levels in a Test Pyramid
https://github.com/pactumjs/pactum
api-testing bdd component-testing contract-testing cucumber e2e-testing integration-testing javascript mock-server nodejs pactum pactumjs test-pyramid testing
Last synced: about 1 month ago
JSON representation
REST API Testing Tool for all levels in a Test Pyramid
- Host: GitHub
- URL: https://github.com/pactumjs/pactum
- Owner: pactumjs
- License: mit
- Created: 2019-12-25T11:53:42.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-28T03:15:55.000Z (7 months ago)
- Last Synced: 2024-10-29T15:18:39.556Z (6 months ago)
- Topics: api-testing, bdd, component-testing, contract-testing, cucumber, e2e-testing, integration-testing, javascript, mock-server, nodejs, pactum, pactumjs, test-pyramid, testing
- Language: JavaScript
- Homepage: https://pactumjs.github.io
- Size: 2.13 MB
- Stars: 544
- Watchers: 14
- Forks: 52
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

# PactumJS




[](https://github.com/pactumjs/pactum/stargazers)
[](https://twitter.com/pactumjs)#### REST API Testing Tool for all levels in a Test Pyramid
**PactumJS** is a REST API Testing Tool used to automate e2e, integration, contract & component (*or service level*) tests.
- ⚡ Swift
- 🎈 Lightweight
- 🚀 Simple & Powerful
- 🛠️ Compelling Mock Server
- 💎 Elegant Data Management
- 🔧 Extendable & Customizable
- 📚 Clear & Comprehensive Testing Style
- 🔗 Component, Contract & E2E testing of APIs
## Documentation
This readme offers an basic introduction to the library. Head over to the full documentation at https://pactumjs.github.io
- [API Testing](https://pactumjs.github.io/guides/api-testing)
- [Integration Testing](https://pactumjs.github.io/guides/integration-testing)
- [Component Testing](https://pactumjs.github.io/guides/component-testing)
- [Contract Testing](https://pactumjs.github.io/guides/contract-testing)
- [E2E Testing](https://pactumjs.github.io/guides/e2e-testing)
- [Mock Server](https://pactumjs.github.io/guides/mock-server)## Need Help
We use Github [Discussions](https://github.com/pactumjs/pactum/discussions) to receive feedback, discuss ideas & answer questions.
## Installation
```shell
# install pactum as a dev dependency
npm install --save-dev pactum# install a test runner to run pactum tests
# mocha / jest / cucumber
npm install --save-dev mocha
```or you can simply use
```bash
npx pactum-init
```
# Usage
**pactum** can be used for all levels of testing in a test pyramid. It can also act as an standalone mock server to generate contracts for contract testing.
## API Testing
Tests in **pactum** are clear and comprehensive. It uses numerous descriptive methods to build your requests and expectations.
### Simple Test Cases
#### Using Mocha
Running simple api test expectations.
```js
const { spec } = require('pactum');it('should be a teapot', async () => {
await spec()
.get('http://httpbin.org/status/418')
.expectStatus(418);
});it('should save a new user', async () => {
await spec()
.post('https://jsonplaceholder.typicode.com/users')
.withHeaders('Authorization', 'Basic xxxx')
.withJson({
name: 'bolt',
email: '[email protected]'
})
.expectStatus(200);
});
``````shell
# mocha is a test framework to execute test cases
mocha /path/to/test
```#### Using Cucumber
See [pactum-cucumber-boilerplate](https://github.com/pactumjs/pactum-cucumber-boilerplate) for more details on pactum & cucumber integration.
```gherkin
Scenario: Check Tea Pot
Given I make a GET request to "http://httpbin.org/status/418"
When I receive a response
Then response should have a status 418
``````js
// steps.js
const pactum = require('pactum');
const { Given, When, Then, Before } = require('@cucumber/cucumber');let spec = pactum.spec();
Before(() => { spec = pactum.spec(); });
Given('I make a GET request to {string}', function (url) {
spec.get(url);
});When('I receive a response', async function () {
await spec.toss();
});Then('response should have a status {int}', async function (code) {
spec.response().should.have.status(code);
});
```## Mock Server
**pactum** can act as a standalone *mock server* that allows us to mock any server via HTTP or HTTPS, such as a REST endpoint. Simply it is a simulator for HTTP-based APIs.
Running **pactum** as a standalone *mock server*.
```js
const { mock } = require('pactum');mock.addInteraction({
request: {
method: 'GET',
path: '/api/projects'
},
response: {
status: 200,
body: [
{
id: 'project-id',
name: 'project-name'
}
]
}
});mock.start(3000);
```
# Notes
Inspired from [frisby](https://docs.frisbyjs.com/) and [pact](https://docs.pact.io).
## Support
Like this project! Star it on [Github](https://github.com/pactumjs/pactum/stargazers) and follow on [Twitter](https://twitter.com/pactumjs). Your support means a lot to us.
## Contributors
If you've ever wanted to contribute to open source, and a great cause, now is your chance! See the [contributing docs](https://github.com/pactumjs/pactum/blob/master/CONTRIBUTING.md) for more information.
Thanks to all the people who contribute.