Forum Discussion

macheshmeh's avatar
macheshmeh
Contributor
2 years ago

Reusing a block of code

a block of code is repeated in some of my tests , i want to create a class or function and call them to different tests instead of repeat the code each time , i know with keyword test we have the possibility the create data driven loop 

can we create data driven loop in script also ?

or there is another way to call this part of test?

 

thanks for your help

3 Replies

  • Kitt's avatar
    Kitt
    Regular Contributor

    like this?

    function bigFunction() {
      // do a lot of stuff
      // do a lot of stuff
      // do a lot of stuff
      // do a lot of stuff
      // do a lot of stuff
      // do a lot of stuff
    }
    
    function test1() {
      bigFunction();
    }
    
    function test2() {
      bigFunction();
    }
    
    function test3() {
     bigFunction();
    }
    ...

    I'm not entirely sure what you are trying to accomplish - extracting data for your tests or running blocks of code, but I would suggest reviewing the DDT docs [here] 

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you find yourself starting to implement the same functions, then it's probably best to create a separate project which has all your functions, which can then be called from other projects

     

    For example,