Parameterize @BeforeMethod method in TestNG Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceTestNG: Identifying which tests methods are nextSpring's @ContextConfiguration test configuration in a testng class is creating beans only once per test class, not per testTestNG using multiple DataProviders with single Test methodHow do I limit threads on a TestNG dataprovider method used as a factoryTestNG Appium Paralell Runs OrganizationTestNG @DataProvider to Return Dynamic ClassParallel TestNG tests with ExtentReports are using the wrong @BeforeMethodTestNG - Is there a lightweight (not dataprovider, not XML) way to pass parameters among @Test and @BeforeMethods?How to store the TestNG dataprovider values in a POJO classSelenium testng base class setup with DataProvider and Factory

Windows 10: How to Lock (not sleep) laptop on lid close?

How did passengers keep warm on sail ships?

Fishing simulator

Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?

3 doors, three guards, one stone

How should I respond to a player wanting to catch a sword between their hands?

How can I protect witches in combat who wear limited clothing?

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

What's the point in a preamp?

Why does tar appear to skip file contents when output file is /dev/null?

Slither Like a Snake

Autumning in love

What is the largest species of polychaete?

I'm thinking of a number

Can I throw a sword that doesn't have the Thrown property at someone?

What was the last x86 CPU that did not have the x87 floating-point unit built in?

What's the difference between (size_t)-1 and ~0?

What do you call a plan that's an alternative plan in case your initial plan fails?

How to say that you spent the night with someone, you were only sleeping and nothing else?

Mortgage adviser recommends a longer term than necessary combined with overpayments

How to pour concrete for curved walkway to prevent cracking?

Writing Thesis: Copying from published papers

What was Bilhah and Zilpah's ancestry?

Two different pronunciation of "понял"



Parameterize @BeforeMethod method in TestNG



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceTestNG: Identifying which tests methods are nextSpring's @ContextConfiguration test configuration in a testng class is creating beans only once per test class, not per testTestNG using multiple DataProviders with single Test methodHow do I limit threads on a TestNG dataprovider method used as a factoryTestNG Appium Paralell Runs OrganizationTestNG @DataProvider to Return Dynamic ClassParallel TestNG tests with ExtentReports are using the wrong @BeforeMethodTestNG - Is there a lightweight (not dataprovider, not XML) way to pass parameters among @Test and @BeforeMethods?How to store the TestNG dataprovider values in a POJO classSelenium testng base class setup with DataProvider and Factory



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








4















I have a base test class for my tests which does the initialisation work before each test.



Here is the code



public class BaseTestParameters 

MyObj myObj;

@DataProvider(name = "apiType")
public static Object[][] createData()

return new Object[][] "type", "1","type","2";


@BeforeMethod()
@Factory(dataProvider = "apiType")
public void setup(String type,String param) throws Exception

myObj = createMyObject(param);





All my test classes extend this base class and they use the myObj for the tests.



myObj has two different ways of creation (depending on param).
All the tests will run twice . One with each way of constituting myObj.



How do I enable this scenario ?
Using @Factory annotation means I need to return Object[] from that method, but I don't have to return any test classes from that method.










share|improve this question






























    4















    I have a base test class for my tests which does the initialisation work before each test.



    Here is the code



    public class BaseTestParameters 

    MyObj myObj;

    @DataProvider(name = "apiType")
    public static Object[][] createData()

    return new Object[][] "type", "1","type","2";


    @BeforeMethod()
    @Factory(dataProvider = "apiType")
    public void setup(String type,String param) throws Exception

    myObj = createMyObject(param);





    All my test classes extend this base class and they use the myObj for the tests.



    myObj has two different ways of creation (depending on param).
    All the tests will run twice . One with each way of constituting myObj.



    How do I enable this scenario ?
    Using @Factory annotation means I need to return Object[] from that method, but I don't have to return any test classes from that method.










    share|improve this question


























      4












      4








      4








      I have a base test class for my tests which does the initialisation work before each test.



      Here is the code



      public class BaseTestParameters 

      MyObj myObj;

      @DataProvider(name = "apiType")
      public static Object[][] createData()

      return new Object[][] "type", "1","type","2";


      @BeforeMethod()
      @Factory(dataProvider = "apiType")
      public void setup(String type,String param) throws Exception

      myObj = createMyObject(param);





      All my test classes extend this base class and they use the myObj for the tests.



      myObj has two different ways of creation (depending on param).
      All the tests will run twice . One with each way of constituting myObj.



      How do I enable this scenario ?
      Using @Factory annotation means I need to return Object[] from that method, but I don't have to return any test classes from that method.










      share|improve this question
















      I have a base test class for my tests which does the initialisation work before each test.



      Here is the code



      public class BaseTestParameters 

      MyObj myObj;

      @DataProvider(name = "apiType")
      public static Object[][] createData()

      return new Object[][] "type", "1","type","2";


      @BeforeMethod()
      @Factory(dataProvider = "apiType")
      public void setup(String type,String param) throws Exception

      myObj = createMyObject(param);





      All my test classes extend this base class and they use the myObj for the tests.



      myObj has two different ways of creation (depending on param).
      All the tests will run twice . One with each way of constituting myObj.



      How do I enable this scenario ?
      Using @Factory annotation means I need to return Object[] from that method, but I don't have to return any test classes from that method.







      java testng testng-dataprovider parameterized-tests






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 7:37









      talex

      11.9k11749




      11.9k11749










      asked Mar 22 at 7:12









      lostintranslationlostintranslation

      165110




      165110






















          1 Answer
          1






          active

          oldest

          votes


















          4














          You can use @Parameters annotation, but you have to specify values in testng,xml it means you have to have separate testng.xml for each set of parameters.



          Here is example:



          AppTest.java



          public class AppTest 
          @Parameters("par1", "par2")
          @BeforeMethod()
          public void setUp(String a, String b)
          System.out.println("a = [" + a + "], b = [" + b + "]");


          @Test
          public void testApp()




          testng.xml



          <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
          <suite name="Suite1" verbose="1" >

          <test name="Run1" >
          <parameter name="par1" value="val"/>
          <parameter name="par2" value="anotherval"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>

          <test name="Run2" >
          <parameter name="par1" value="newValue"/>
          <parameter name="par2" value="yetAnotherVal"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>
          </suite>





          share|improve this answer

























          • This will just replace par1 and par2 values in setup function. I need testApp to run once with par1 and once with par2. Some kind of iteration over parameters while running the tests.

            – lostintranslation
            Mar 22 at 9:45












          • @lostintranslation I updated my answer. Now it runs test twice with different values.

            – talex
            Mar 22 at 10:00












          • Getting this error - Parameter 'par1' is required by '@Configuration' on method setup but has not been marked '@Optional' or defined. par2 is overwriting par1 when testng is initialising.

            – lostintranslation
            Mar 22 at 10:10











          • Adding two <test> seems overdo for such a simple use case. Will shift to junit. Testng is not a good choice for such cases.

            – lostintranslation
            Mar 22 at 10:11











          • How do you run your test? It seems like you didn't use testng.xml.

            – talex
            Mar 22 at 10:12











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55294585%2fparameterize-beforemethod-method-in-testng%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You can use @Parameters annotation, but you have to specify values in testng,xml it means you have to have separate testng.xml for each set of parameters.



          Here is example:



          AppTest.java



          public class AppTest 
          @Parameters("par1", "par2")
          @BeforeMethod()
          public void setUp(String a, String b)
          System.out.println("a = [" + a + "], b = [" + b + "]");


          @Test
          public void testApp()




          testng.xml



          <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
          <suite name="Suite1" verbose="1" >

          <test name="Run1" >
          <parameter name="par1" value="val"/>
          <parameter name="par2" value="anotherval"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>

          <test name="Run2" >
          <parameter name="par1" value="newValue"/>
          <parameter name="par2" value="yetAnotherVal"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>
          </suite>





          share|improve this answer

























          • This will just replace par1 and par2 values in setup function. I need testApp to run once with par1 and once with par2. Some kind of iteration over parameters while running the tests.

            – lostintranslation
            Mar 22 at 9:45












          • @lostintranslation I updated my answer. Now it runs test twice with different values.

            – talex
            Mar 22 at 10:00












          • Getting this error - Parameter 'par1' is required by '@Configuration' on method setup but has not been marked '@Optional' or defined. par2 is overwriting par1 when testng is initialising.

            – lostintranslation
            Mar 22 at 10:10











          • Adding two <test> seems overdo for such a simple use case. Will shift to junit. Testng is not a good choice for such cases.

            – lostintranslation
            Mar 22 at 10:11











          • How do you run your test? It seems like you didn't use testng.xml.

            – talex
            Mar 22 at 10:12















          4














          You can use @Parameters annotation, but you have to specify values in testng,xml it means you have to have separate testng.xml for each set of parameters.



          Here is example:



          AppTest.java



          public class AppTest 
          @Parameters("par1", "par2")
          @BeforeMethod()
          public void setUp(String a, String b)
          System.out.println("a = [" + a + "], b = [" + b + "]");


          @Test
          public void testApp()




          testng.xml



          <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
          <suite name="Suite1" verbose="1" >

          <test name="Run1" >
          <parameter name="par1" value="val"/>
          <parameter name="par2" value="anotherval"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>

          <test name="Run2" >
          <parameter name="par1" value="newValue"/>
          <parameter name="par2" value="yetAnotherVal"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>
          </suite>





          share|improve this answer

























          • This will just replace par1 and par2 values in setup function. I need testApp to run once with par1 and once with par2. Some kind of iteration over parameters while running the tests.

            – lostintranslation
            Mar 22 at 9:45












          • @lostintranslation I updated my answer. Now it runs test twice with different values.

            – talex
            Mar 22 at 10:00












          • Getting this error - Parameter 'par1' is required by '@Configuration' on method setup but has not been marked '@Optional' or defined. par2 is overwriting par1 when testng is initialising.

            – lostintranslation
            Mar 22 at 10:10











          • Adding two <test> seems overdo for such a simple use case. Will shift to junit. Testng is not a good choice for such cases.

            – lostintranslation
            Mar 22 at 10:11











          • How do you run your test? It seems like you didn't use testng.xml.

            – talex
            Mar 22 at 10:12













          4












          4








          4







          You can use @Parameters annotation, but you have to specify values in testng,xml it means you have to have separate testng.xml for each set of parameters.



          Here is example:



          AppTest.java



          public class AppTest 
          @Parameters("par1", "par2")
          @BeforeMethod()
          public void setUp(String a, String b)
          System.out.println("a = [" + a + "], b = [" + b + "]");


          @Test
          public void testApp()




          testng.xml



          <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
          <suite name="Suite1" verbose="1" >

          <test name="Run1" >
          <parameter name="par1" value="val"/>
          <parameter name="par2" value="anotherval"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>

          <test name="Run2" >
          <parameter name="par1" value="newValue"/>
          <parameter name="par2" value="yetAnotherVal"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>
          </suite>





          share|improve this answer















          You can use @Parameters annotation, but you have to specify values in testng,xml it means you have to have separate testng.xml for each set of parameters.



          Here is example:



          AppTest.java



          public class AppTest 
          @Parameters("par1", "par2")
          @BeforeMethod()
          public void setUp(String a, String b)
          System.out.println("a = [" + a + "], b = [" + b + "]");


          @Test
          public void testApp()




          testng.xml



          <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
          <suite name="Suite1" verbose="1" >

          <test name="Run1" >
          <parameter name="par1" value="val"/>
          <parameter name="par2" value="anotherval"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>

          <test name="Run2" >
          <parameter name="par1" value="newValue"/>
          <parameter name="par2" value="yetAnotherVal"/>
          <packages>
          <package name="dummy.java" />
          </packages>
          </test>
          </suite>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 22 at 10:00

























          answered Mar 22 at 7:49









          talextalex

          11.9k11749




          11.9k11749












          • This will just replace par1 and par2 values in setup function. I need testApp to run once with par1 and once with par2. Some kind of iteration over parameters while running the tests.

            – lostintranslation
            Mar 22 at 9:45












          • @lostintranslation I updated my answer. Now it runs test twice with different values.

            – talex
            Mar 22 at 10:00












          • Getting this error - Parameter 'par1' is required by '@Configuration' on method setup but has not been marked '@Optional' or defined. par2 is overwriting par1 when testng is initialising.

            – lostintranslation
            Mar 22 at 10:10











          • Adding two <test> seems overdo for such a simple use case. Will shift to junit. Testng is not a good choice for such cases.

            – lostintranslation
            Mar 22 at 10:11











          • How do you run your test? It seems like you didn't use testng.xml.

            – talex
            Mar 22 at 10:12

















          • This will just replace par1 and par2 values in setup function. I need testApp to run once with par1 and once with par2. Some kind of iteration over parameters while running the tests.

            – lostintranslation
            Mar 22 at 9:45












          • @lostintranslation I updated my answer. Now it runs test twice with different values.

            – talex
            Mar 22 at 10:00












          • Getting this error - Parameter 'par1' is required by '@Configuration' on method setup but has not been marked '@Optional' or defined. par2 is overwriting par1 when testng is initialising.

            – lostintranslation
            Mar 22 at 10:10











          • Adding two <test> seems overdo for such a simple use case. Will shift to junit. Testng is not a good choice for such cases.

            – lostintranslation
            Mar 22 at 10:11











          • How do you run your test? It seems like you didn't use testng.xml.

            – talex
            Mar 22 at 10:12
















          This will just replace par1 and par2 values in setup function. I need testApp to run once with par1 and once with par2. Some kind of iteration over parameters while running the tests.

          – lostintranslation
          Mar 22 at 9:45






          This will just replace par1 and par2 values in setup function. I need testApp to run once with par1 and once with par2. Some kind of iteration over parameters while running the tests.

          – lostintranslation
          Mar 22 at 9:45














          @lostintranslation I updated my answer. Now it runs test twice with different values.

          – talex
          Mar 22 at 10:00






          @lostintranslation I updated my answer. Now it runs test twice with different values.

          – talex
          Mar 22 at 10:00














          Getting this error - Parameter 'par1' is required by '@Configuration' on method setup but has not been marked '@Optional' or defined. par2 is overwriting par1 when testng is initialising.

          – lostintranslation
          Mar 22 at 10:10





          Getting this error - Parameter 'par1' is required by '@Configuration' on method setup but has not been marked '@Optional' or defined. par2 is overwriting par1 when testng is initialising.

          – lostintranslation
          Mar 22 at 10:10













          Adding two <test> seems overdo for such a simple use case. Will shift to junit. Testng is not a good choice for such cases.

          – lostintranslation
          Mar 22 at 10:11





          Adding two <test> seems overdo for such a simple use case. Will shift to junit. Testng is not a good choice for such cases.

          – lostintranslation
          Mar 22 at 10:11













          How do you run your test? It seems like you didn't use testng.xml.

          – talex
          Mar 22 at 10:12





          How do you run your test? It seems like you didn't use testng.xml.

          – talex
          Mar 22 at 10:12



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55294585%2fparameterize-beforemethod-method-in-testng%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해