1

I would like to use the same instance of @BeforeMethod for my tests from different classes but It just wont work

 package com.code.theCode public class theConfiguration{ @BeforeMethod(groups = {"example"}, alwaysRun = true) public void setupMethod(){ System.out.println("inside setupMethod"); } } 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 package com.code.test public class theTest{ @Test(groups = {"example"}, alwaysRun = true) public void setupMethod(){ System.out.println("inside test"); } } 

testng.xml

<suite name="AllTests" verbose="1"> <test name="AllTests"> <groups> <run> <include name="example"> </run> </groups> <packages> <package name="com.code.*" /> </packages> </test> 

When run my tests I get blank sys outs

Any help greatly appreciated

7
  • How do you execute it? Commented May 7, 2015 at 15:42
  • If you're getting blank sys outs then your BeforeMethod as well as your test are not being executed, so the problem isn't only the BeforeMethod. How are you running the tests? IDE? console? with which arguments? Commented May 7, 2015 at 15:44
  • yes, passes and prints System.out.println("inside test"); but wont print the @BeforeTest syso...It dosnt show me errors it the logs :( Commented May 7, 2015 at 15:45
  • How are you running the tests? IDE? console? with which arguments? Commented May 7, 2015 at 15:46
  • Eclipse IDE, TestNG Run configuration pointing to the testng.xml suite file Commented May 7, 2015 at 15:47

1 Answer 1

3

Create an abstract class which include your configuration methods (what you want to use for more @Tests). After that extend your Test class with the created Abstract class. For example:

public abstract class configurationClass { @BeforeMethod public void beforeMethod() { System.out.println("beforeMethod"); } } public class testClass extends configurationClass { @Test public void test1() { System.out.println("test1"); } @Test public void test2() { System.out.println("test2"); } } 

When you run the test class, the output will be:

beforeMethod test1 beforeMethod test2 
Sign up to request clarification or add additional context in comments.

4 Comments

Hi peetya, I have achieved this already but I don't want to use inheritance hence why I thought the xml approach would be better.
For some reason it will calls BeforeSuite in the configuration class but not BeforeMethod....BeforeMethod should be called before (AT-SIGN)Test runs, correct?
I'm not sure that you can achieve it only from xml. Yes, beforeMethod runs before @Tests.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.