How to reuse Postman scripts and tests among different test cases

Поделиться
HTML-код
  • Опубликовано: 16 дек 2024

Комментарии • 34

  • @vdespa
    @vdespa  4 года назад +1

    👉 Want to learn more about Postman? Check my Postman online course. Get it at a special price and help support this RUclips channel:
    vdespa.com/courses/?q=RUclips

  • @AZKACasablanca
    @AZKACasablanca 6 лет назад +2

    So much better than beating the bush around StackOverflow!
    Thanks a lot Valentin!!!!

  • @rueleonheart2467
    @rueleonheart2467 2 года назад +3

    You can also declare functions inside an object on the Pre-request script of the Collection header, and all requests under that collection can use those functions without eval. The only caveat is you cannot use pm inside the function body, but you can pass pm as function parameter.

    • @vdespa
      @vdespa  2 года назад

      Not sure what you mean. Can you share an example or more details?

  • @varsharyali1879
    @varsharyali1879 Год назад +1

    Hi Velentin
    Can you make video on how to re-use functions in javascript that take in arguments as well? I'm having trouble setting that up

  • @vs200712
    @vs200712 6 лет назад

    Awesome. I was going a step further to have all common functionality in a separate collection and then call the common() method using eval from another collection within the same environment. Trying this, I'm getting "eval not a function" error. I tried with both environment and globals and not having any luck. Is this not possible? Can you please walk us thru on doing this way?.

  • @xiangyap1248
    @xiangyap1248 3 года назад

    hi, may i know what is the purpose for the extra bracket behind the eval()?
    eval(pm.environment.get(""))();

    • @vdespa
      @vdespa  3 года назад

      Sure. That is a function call.

  • @rashmiambedkar5143
    @rashmiambedkar5143 4 года назад +1

    I tried it . Feel good to optimize the code

    • @vdespa
      @vdespa  4 года назад

      Great to hear!

  • @hydtechietalks3607
    @hydtechietalks3607 3 года назад

    How can refer an external file in POSTman script.. Eval is evil may not be allowed by sensitive tools?

    • @vdespa
      @vdespa  3 года назад

      You can't read from the filesystem from Postman scripts.

  • @freshrace1
    @freshrace1 Год назад

    Valentin, I am a beginner so don't be hard on my question please) Why did you place the tests to the pre-request script? I thought tests should always be executed after the request.

    • @vdespa
      @vdespa  Год назад

      The tests are in the pre-request script but are not executed there. Maybe this concept is a bit harder to grasp in the beginning.
      If you are just getting started with Postman, consider taking a course.

    • @freshrace1
      @freshrace1 Год назад

      @@vdespa thank you, I am already considering this. But could you please answer, why would you not leave the tests in the Tests tab?

    • @vdespa
      @vdespa  Год назад

      @@freshrace1 The tests are stored in an environment variable, so it does not matter where they are in initially.

    • @vdespa
      @vdespa  Год назад

      @@freshrace1 Write me a message on LinkedIn. I can help you.

  • @wahahah
    @wahahah 3 года назад

    why environment variable, why not use global/collection etc? Is there a rule or convention?

    • @vdespa
      @vdespa  3 года назад

      Use whatever makes more sense for your use-case. I was just demonstrating the principle.

  • @SandyShiralkar
    @SandyShiralkar 3 года назад

    I tried this on a soap request, after adding the get statement in test tab of another request i get the message "There was an error in evaluating the test script: TypeError: Cannot read property 'MC' of undefined". Same thing works absolutely fine when the get statement is in the same request api in test tab as that of the var created and set statement.

    • @vdespa
      @vdespa  3 года назад

      You need to understand some basics around JavaScript. Check this RUclips channel for a full course.

    • @SandyShiralkar
      @SandyShiralkar 3 года назад

      @@vdespa I have gone through your course on Udemy, but no where have I seen anything about soap calls. Do you want to say that the above approach is not suitable for soap requests?

    • @vdespa
      @vdespa  3 года назад

      @@SandyShiralkar Please send me a private message with what you have tried and where you got stuck. I need a bit more context.

  • @victorcardenaslopez4994
    @victorcardenaslopez4994 6 лет назад

    Gracias, me ahorra mucho código. Thanks, this will reduce my code.

  • @tanmaisharma3774
    @tanmaisharma3774 5 лет назад

    Doesn't seem to work with Postman 7.0.6 ;commonTests(); throws error as:
    There was an error in evaluating the test script: ReferenceError: commonTests is not defined

  • @franznataly
    @franznataly 5 лет назад

    can I use this for pre-request, reuse a pre-request in multiples request?
    if I have , pre-request and test, but I only want to reuse the pre-request , how can i do it?

  • @glacagnina
    @glacagnina 4 года назад +2

    This trick is not satisfactory for me. First of all, the definition of function taking parameters does not seem feasible in a reasonable way. Second, the definition of the "shared" tests is strongly tied to the call in which one defines them. This means that I need to run this call first every time. The video is from 2017, I frankly find it hard to believe that nobody needed to add own JS code to their projects. Having to use this hack in 2020 reduces, in my opinion, the usefulness of Postman by a significant amount.

    • @vdespa
      @vdespa  4 года назад +1

      I am not a big fan of this either, Giuseppe. But I don't know a better way to do it. I agree, this is 2020 and the app should allow more. Feel free to submit an issue with Postman github.com/postmanlabs/postman-app-support/issues/

    • @daveyq
      @daveyq 4 года назад

      Did you find a solution to this?

    • @glacagnina
      @glacagnina 4 года назад

      David Quick nope. This limits the extent to which I use Postman.

  • @FatsoPilot
    @FatsoPilot 2 года назад

    Hacky, Postman is disappointing. These workarounds for re-using requests and functions and test scripts should be built in app functionality.

    • @vdespa
      @vdespa  2 года назад +1

      I agree, Jeremy. Unfortunately Postman has not done much since this video was posted to address this problem.