1

I am having a postman collection which consists of request and test cases for each requests. I have two test case for each request. one for validating status code and other for validating response time. I need to execute status code test case frequently and response time test case results occasionally.How to achieve it without modifying the collection for every run and is it achievable in providing any option in terminal?

collection.json

                {
                    "name": "Metadata",
                    "item": [
                        {
                            "name": "info",
                            "event": [
                                {
                                    "listen": "test",
                                    "script": {
                                        "id": "32cf67e7-5d42-4231-86fe-e7fffa32c855",
                                        "exec": [
                                            "pm.test(\"Status code is 200\", function () {",
                                            "    pm.response.to.have.status(200);",
                                            "});",
                                            "pm.test(\"Response time is less than 300ms\", function () {",
                                            "    pm.expect(pm.response.responseTime).to.be.below(300);",
                                            "});"
                                        ],
                                        "type": "text/javascript"
                                    }
                                }
                            ],
                            "request": {
                                "auth": {
                                    "type": "bearer",
                                    "bearer": [
                                        {
                                            "key": "token",
                                            "value": "{{tokenAdmin}}",
                                            "type": "string"
                                        }
                                    ]
                                },
                                "method": "GET",
                                "header": [],
                                "url": {
                                    "raw": "{{url}}/api/m0/metadata/info",
                                    "host": [
                                        "{{url}}"
                                    ],
                                    "path": [
                                        "api",
                                        "m0",
                                        "metadata",
                                        "info"
                                    ]
                                }
                            },
                            "response": []
                        }
                    ],
                    "protocolProfileBehavior": {},
                    "_postman_isSubFolder": true
                }


muthu
  • 723
  • 10
  • 27
  • Can you clarify what you mean here please - "I need to execute status code test case frequently and response time test case results occasionally." How frequently? How are you defining what occasionally means? – Danny Dainton May 21 '20 at 07:01
  • @DannyDainton I need to execute status code test cases(need to skip response time test case ) every day to ensure endpoint is working fine. I need to execute response time test case(include status code test cases) at end of every sprint(i.e 15 days once) – muthu May 21 '20 at 07:09
  • Are you running these manually, with the Collection Runner, on a Monitor or with Newman in a CI system? You've tagged everything so I'm not sure – Danny Dainton May 21 '20 at 07:14
  • using newman library on monitor @DannyDainton – muthu May 21 '20 at 07:20
  • You could add the status check as normal so that it's always running and them maybe add the response times to run on a specific day of the month? Like at the end of your sprint, as you mentioned. Or have two monitors that run at different times :) – Danny Dainton May 21 '20 at 07:35
  • Is there any way I can add tags to each tests and provide tag name on terminal? – muthu May 21 '20 at 08:21
  • You can use `--folder ` to run that specific test. That has quirks to it but it should work if the collection is basic. – Danny Dainton May 21 '20 at 08:37
  • @DannyDainton could you please provide an example by considering my collection – muthu May 21 '20 at 08:48
  • Do not vandalize your posts. By posting on this site, you've irrevocably granted the Stack Exchange network the right to distribute that content under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/) for as long as it sees fit to do so. For alternatives to deletion, see: [I've thought better of my question; can I delete it?](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question) – Sabito stands with Ukraine Jan 13 '21 at 09:07

1 Answers1

0

For a very basic flow, you can use moment to check which day it currently is and if that matches the condition, it will run the responseTime test.

let moment  = require('moment'),
    date    = moment().format('dddd');

// Runs on each request
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

// Only runs on a Friday 
if (date === 'Friday') {
    pm.test("Response time is less than 1000ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(1000);
    });
}

Moment has lots of different options available to you and might work if you want to only run that check at the end of the sprint or on a given day.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80