Giter Club home page Giter Club logo

postman-bdd's Introduction

Postman BDD

✨ Postman now has it's own BDD test syntax ✨

Postman-BDD is no longer necessary, because Postman now has its own BDD and fluent syntax built-in!

I recommend that you start using Postman's new test syntax instead of Postman-BDD. However, if you want to continue using Postman-BDD, then you can find the original ReadMe here.

Docs

Example

// example using pm.response.to.have
pm.test("response is ok", () => {
    pm.response.to.have.status(200);
});

// example using pm.expect()
pm.test("environment to be production", () => {
    pm.expect(pm.environment.get("env")).to.equal("production");
});

// example using response assertions
pm.test("response should be okay to process", () => {
    pm.response.to.not.be.error;
    pm.response.to.have.jsonBody("");
    pm.response.to.not.have.jsonBody("error");
});

// example using pm.response.to.be*
pm.test("response must be valid and have a body", () => {
     // assert that the status code is 200
     pm.response.to.be.ok; // info, success, redirection, clientError,  serverError, are other variants
     // assert that the response has a valid JSON body
     pm.response.to.be.withBody;
     pm.response.to.be.json; // this assertion also checks if a body  exists, so the above check is not needed
});

Migration Guide

Postman's new BDD and fluent syntax are a bit different from Postman-BDD. Here are the changes you need to make to migrate your tests:

Remove describe blocks

describe() blocks were optional in Postman-BDD, and they don't exist at all in Postman's new syntax. So just remove them.

Replace it blocks with pm.test

Postman-BDD used it blocks to define tests, such as:

it('should return the correct customer', () => {
  // assertions here
});

Postman now has pm.test blocks, which work the same way. For example:

pm.test('returns the correct customer', () => {
  // assertions here
});

Move hooks to folder/collection scripts

Postman-BDD allowed you to define common assertions or setup/teardown logic in hooks, such as before(), after(), beforeEach() and afterEach(). This is no longer necessary because Postman now allows you to define test scripts for folders and collections.

Different assertion syntax

Postman-BDD used the Chai.js and Chai-HTTP assertion libraries, which let you write assertions using an intuitive, fluent, English-like syntax.

it('should return a 200 response', () => {
  response.should.have.status(200);
});

it('should set the Location header', () => {
  response.should.have.header('Location');
});

it('should return a JSON response', () => {
  response.should.be.json;
});

it('should return the correct customer', () => {
  response.body.should.have.property('id', 12345);
});

Postman now supports its own fluent assertion syntax, which is somewhat similar.

pm.test('returns a 200 response', () => {
  pm.response.to.have.status(200);
});

pm.test('sets the Location header', () => {
  pm.response.to.have.header("Location");
});

pm.test('returns a JSON response', () => {
  pm.response.to.be.json;
});

pm.test('returns the correct customer', () => {
  let jsonData = pm.response.json();
  pm.expect(jsonData.id).to.eql(12345);
});

postman-bdd's People

Contributors

jamesmessinger avatar lampei avatar rdoubleui avatar urosjarc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

postman-bdd's Issues

Using latest Postman

The postman team added a "pm.sendRequest" (https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990). I keep getting "pm.sendRequest is not a function" when I try adding that to globals or to the prerequest script. Do you have any idea how I can add that functionality to my tests? Just trying to get a way to do "setup" really for tests (e.g. getting current totals from API, getting current info on object from API, etc.) then validating in the test.

How to stop the test case if the test fails.

When some test failed.I would like to terminate the script. Can I terminate next test?

`
eval(globals.postmanBDD);

describe('Get customer info', () => {

it('should return a 200 response', () => {
    response.should.have.status(200);
    
    // in case of status, not 200. I would like to terminate the test.
});

// There isn't run this.
it('test 2', () => {
});

});
`

Create typescript definition for postman-bdd

I'm creating e2e postman tests localy in code, example for postman call:

"use strict";

const postman = require('../postman');

exports.name = 'getAdvice';
exports.description = 'Advice list';

exports.request = {
  url: " {{url}}/v1/collections/HW2016_RUNNING__IDE",
  method: "GET",
  header: [
    {
      key: 'x-api-key',
      value: '{{x-api-key}}'
    }
  ]
};

exports.event = [
  postman.events.test(() => {
    describe('Get customer info', () => {
      it('should return a 200 response', () => {
        response.should.have.status(200);
      });
    });
  })
];

And it would speed up my process of writing tests if postman-bdd would have somekind of typescript definition file which would make IDE hinting more acurate for faster development.

Array assertion expects the returned JSON to be an Object.

I am using the Postman BDD library for asserting my JSON response from an API. The API response returned is an array like this -

[
{
"last_page": 1,
"locality": [
{
"name": "Delhi",
"id": 464
},
{
"name": "Mumbai",
"id": 102242
},
{
"name": "Kolkata",
"id": 465
},
{
"name": "Chennai",
"id": 2349
},
{
"name": "Bangaluru",
"id": 41444
},
{
"name": "Hyderabad",
"id": 3843
}
]
}
]

My assertion for this is to check if the returned response is an array -

describe("checking the response and keys",()=>{ it("response is a JSON object",()=>{ response.should.be.an("Array"); });

However, this is returning in an error, saying - expected {Object(status,statusType,...)} to be an array.

after, afterEach, before, beforeEach are not defined in Postman

Hi,

I have returned back to my project after 3 months and I try to run my tests in postman however I am getting after is not defined error.

I am loading postman-bdd.min.js as it's specify in doc:

// "install" Postman BDD
postman.setGlobalVariable('postmanBDD', responseBody);

and here is my test:

// Load Postman BDD
eval(postman.getGlobalVariable('postmanBDD'));

describe('Logout', () => {
    after(() => postman.setEnvironmentVariable("token", ''));

    it('should return successful response with status code 204', () => {
        response.should.have.status(204);
        response.should.be.json;
        response.body.should.be.empty;
    });
});

The describe, it are defined and only afte, afterEach, before, beforeEachare not defined for some reason. I am using Postman desktopVersion 4.8.1` on Mac OSX.

before hook inside describe is not load

When I use the before block inside the describe block the code inside the before block isn't called but if I put the before block outside of the describe block it is called normally. Is this the normal behaviour?

My script:

eval(globals.postmanBDD);

var schema = {"type":"array","minItems":3,"items":{"type":"object","properties":{"id":{"type":"integer"},"action_code":{"type":["string","null"]},"source":{"type":"string"},"event_extra_info":{"type":"object","properties":{"tag":{"type":"string"}}},"created_at":{"type":"string"}},"required":["id","action_code","source","event_extra_info","created_at"],"additionalProperties":false}};
    

describe('Approved Calculators API', function(){
    before('load schema', function(){ 
        // For now the schema will be maintened on the script too
        console.log("schema:", globals.schema);
        if(globals.schema !== null && globals.schema !== undefined) {
            schema = JSON.parse(globals.schema);
        }
    });
   
    
   it('should return a valid JSON response', function(){
     expect(response).to.be.json;
    //  expect(response.body).to.not.be.empty;
     expect(response.body).to.have.schema(schema);  
   }); 
   
   it('should return 200 as status code', function(){
     expect(response).to.have.status(200);
   });
   
   it('should not take more than 200 miliseconds to load', function(){
     expect(response.time).to.be.at.most(200);
   });
   
   after('clean', function(){
     postman.clearGlobalVariable("schema"); 
   });
});

False negatives

Given a test example:

describe('Products API', function(){
    it('should return a valid JSON response', function(){
        expect(response).to.be.json;
        expect(response.body).to.not.be.empty;
        expect(response.body).to.have.schema(schema);
    });
    
    it('should return 200 as status code', function(){
        expect(response).to.have.status(200);
    });
   
    it('should not take more than ' + globals.lowResponseTime + ' miliseconds', function(){
        expect(responseTime).to.be.at.most(globals.lowResponseTime);
    });
});

After I runned the test the following errors were shown:

* Products API should not take more than 200 miliseconds to load
* expected 519 to be at most 200

The problem is that the second failure is just a better explanation for the first one and it give an impression of false negative.
I understood that the first is about the scenario described on the it statement and the second is about the assertion made, but I think a better approach can be join the 2 messages because it is about the same failure.
Given another example with multiples assertions inside the same 'it' statement I think it should be join on the same failure too because all the assertions inside the same 'it' is about the same test.
Another problem is that on the final test report the number of failed test-scripts isn't correct and is always 0.

Example:


┌─────────────────────────┬──────────┬──────────┐
│                         │ executed │   failed │
├─────────────────────────┼──────────┼──────────┤
│              iterations │        1 │        0 │
├─────────────────────────┼──────────┼──────────┤
│                requests │       61 │        0 │
├─────────────────────────┼──────────┼──────────┤
│            test-scripts │       60 │        0 │
├─────────────────────────┼──────────┼──────────┤
│      prerequest-scripts │        3 │        0 │
├─────────────────────────┼──────────┼──────────┤
│              assertions │      223 │       70 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 2m 0s                     │
├───────────────────────────────────────────────┤
│ total data received: 130.17KB (approx)        │
├───────────────────────────────────────────────┤
│ average response time: 1894ms                 │
└───────────────────────────────────────────────┘



Asserting header contents

I have an API, which pulls a token out and we have to use that token in subsequent requests. In the CSRF token, I am getting lot of headers and I want to assert if their values are above certain value.

I am particularly concerned about the Keep-Alive header. The response header that I am recieving is

Keep-Alive →timeout=15, max=98

where the max value keep changing to 98-100. I want to assert that we have this value above , say 95+. How would I achieve this using the library.

BDD Library gives error on checking Content-Length header

I have a RESTful API, that returns a Content-Length as one of it's response headers. I want to assert that the length of this header is below 2000.

In my Postman app, I can see that the Content-Length header returns a value of 539.

If I use this assertion,
it('response should have content length below 2000',()=>{ response.should.have.header('Content-Length').and.should.be.an('object');

it passes, since the returned header is of type object. Now if I want to assert that the value of the Content-Length is below 2000, I'm using this assertion

response.should.have.header('Content-Length').and.should.be.below(2000);

but this fails stating
expected [object Object] to be a number or a date

As per the this post , Content-Length is returned as an decimal number of octet, so I changed the value to be

response.should.have.header('Content-Length').and.should.be.below(3720); [octet of 2000]

but this also fails.

How can I assert that the Content-Length is below 2000?

Postman collection not able to run using newman command line

I am trying to run a collection of Postman tests, which are using Postman-BDD Library for simple tests. I started using the newman command line runner to run the tests. However, I am getting a Reference Error for the describe used in the tests.

Here is my tests

eval(globals.postmanBDD);

describe("Success Failure Check",()=>{
it('response should be ok', () => {
response.ok.should.be.true;
});

it('HTTP code returned should be 200', () => {
response.should.have.status(200);
});

it('response should not be error', () => {
response.error.should.be.false;
});
});

The tests pass, but the Newman command line throughs an error - which I am not able to understand, since I installed newman globally. I have attached the image for the failure -

Error in Newman

I have gone through the newman npm page, and I see that it integrates well with the library. So I am not able to get why I'm getting this error.

Invalid Chai property: result

I've upgraded from v4.3.0 to v5.0.3 and have run into an issue.

When I call

"eval(globals.postmanBDD);",
							"",
							"it('should be successful', () => {",
              "    response.should.have.status(200);",
							"});",
							"",
              "it('should have result as an array', () => {",
              "    response.body.should.have.result",
              "    response.body.result.should.be.an('array');",
							"});",
							"",
              "it('should have total as a number', () => {",
              "    response.body.should.have.total",
              "    response.body.total.should.be.a('number');",
							"});"

I get Invalid Chai property: result and Invalid Chai property: total
The errors occur at response.body.result and response.body.total The only thing I changed was to put the new postmanBDD js code in my globals file.

Any ideas what I could be doing wrong?

Not much of an issue

Hey I really love the ongoing project. Is there a way we can add release notes so that we know what really changed from a birds eye view.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.