Search code examples
postmanpostman-pre-request-scriptpostman-testcase

Postman Test Scripts


For my postman project,testscript template are same for most requests.I don't want to maintain it all requests.

I want to keep all test assertions inside like a method and need to call that method inside the requests wherever required.

Any solution for this

My Testscript template is enter image description here

I am thinking the way...


Solution

  • Postman is using JavaScript for the testing. I have created functions that I can re-use, to parse data, return values, etc. While this is not necessarily the direct use you have, it could work.

    In the collection, I put the functions I am going to call into the Pre-Request script, Variables also has anything I need set as well.

    As these are functions, you can streamline the test by using common functions, or simply add the couple of lines in each test case.

    For instance, in my pre-request script

    SharedFunctions = {
    
        // configuration/1.35.0 - returns configuration
        GetServiceNameFromString: function (StringValue) {
            const ServiceName = StringValue.split('/');
            return ServiceName[0];
        },
    
        // configuration/1.35.0, 1, 32, 0
        IsVersionAtLeast: function (StringValue, Major, Minor, Patch) {
            const ServiceInfo = StringValue.split('/');
            const VersionInfo = ServiceInfo[1].split('.');
            if (parseInt(VersionInfo[0]) > Major) { return true; }
            if (parseInt(VersionInfo[0]) < Major) { return false; }
    
            // Major is equal if here
            if (parseInt(VersionInfo[1]) > Minor) { return true; }
            if (parseInt(VersionInfo[1]) < Minor) { return false; }
    
            // Major & Minor are equal if here
            if (parseInt(VersionInfo[2]) >= Patch) { return true; }
            return false;
        },
    
    }
    

    I then can reference these functions from any test by adding a test case in the normal test location

    pm.test('SharedFunctions should work', function () {
        const SMEService = pm.response.headers.get('MyHeader-ServiceName');
        const ServiceName = SharedFunctions.GetServiceNameFromString(SMEService);
        pm.expect(ServiceName).not.null;
        pm.expect(ServiceName).to.equal('configuration');
        pm.expect(SharedFunctions.IsVersionAtLeast(SMEService, 1, 15, 0)).to.equal(true);
    });
    

    As the shared functions can accept parameters, you can make your call to the external service, and send the response as the first parameter. Write your Shared function test to simply return true/false, and then your test simply has the call to the shared function returns true.

    For instance:

    CommonValidationFunction = {
       // Validate Job Added Successfully
       ValidateJobAdd: function (jsonData) {
          if (jsonData.Message != "JobAddedSuccessMessage") return false
          if (jsonData.ErrorCode != 0) return false
          ...
          return true;
       }
    }
    

    Your test then simply calls your external service to get the jsonData.

    pm.test("Common Function works", function () {
       pm.expect(CommonValidationFunction.ValidateJobAdd(jsonData)).to.equal(true);
    }