Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@BeforeTest/@BeforeSuite in superclass only invoked on first instance that inherits from it. #313

Closed
jabbrwcky opened this issue Dec 17, 2012 · 7 comments

Comments

@jabbrwcky
Copy link

I noticed that a @BeforeTest/@BeforeSuite method defined in as superclass of a test will only be executed once on the first instance of the inheriting classes.
As the documentation on the test annotations is a little sparse sometimes, it is not clear to me if this should be considered as bug or as feature.

If the @BeforeTest/@BeforeSuite method is executed on every inheriting class it might lose some of its semantics and act like a method annotated with @BeforeClass, which might not be the perfect solution in my eyes.

The other way would be to clearly document this not immediately obvious behavior and use the test context (ITestContext) to provide Test-scoped configuration and instance data to running tests.
This could be a possible solution:

public abstract class AbstractTestBase {

    /** Property Foo is set once per Suite */
    protected String foo;

    /** Property Foo is set once per Test */
    protected String bar;

    /**
     * Store foo in context.
     *
     * @param context test context for storing test conf data
     */
    @BeforeSuite
    public void beforeSuite(ITestContext context) {
        out.println("@BeforeSuite called on " + getClass().getName());
        context.setAttribute("foo", "I was set in @BeforeSuite");
    }


    /**
     * Store bar in context.
     *
     * @param context test context for storing test conf data
     */
    @BeforeTest(alwaysRun = true)
    public void beforeTest(ITestContext context) {
        out.println("@BeforeTest called on " + getClass().getName());
        context.setAttribute("bar", "I was set in @BeforeTest");
    }

    /**
     * Fetch foo and bar from context per instance.
     *
     * @param context test context to retrieve conf data from.
     */
    @BeforeClass
    public void beforeClass(ITestContext context) {
        out.println("@BeforeClass called on " + getClass().getName());
        foo = (String) context.getAttribute("foo");
        bar = (String) context.getAttribute("bar");
    }
}

A small project demonstrating the behavior and my current solution is here: https://github.com/jabbrwcky/testng-annotation-demo

/Jens

@ash2k
Copy link

ash2k commented Dec 28, 2012

I already reported it 10 months (!) ago #184.

@VladRassokhin
Copy link
Contributor

As i can see from code this behavior is by-design:
https://github.com/cbeust/testng/tree/master/src/test/java/test/uniquesuite
So there probably documentation problem.

Also using @BeforeSuite with ITestContext may cause troubles if you have this method in more than one <test>. (Just add <class name="net.hausherr.demo.TestA" /> into your "T1" test in testng.xml)

IMHO: Nice solution for that problem is generating implementation of AbstractTest on-the-fly if needed and instantiate that class. Or print warning message.

spadgett pushed a commit to spadgett/ldp-testsuite that referenced this issue Oct 7, 2014
- Only set static variables from LdpTest setup. Setup is only called
  once even when several classes inherit from LdpTest.
- Change setup and tear down method names so we don't accidentally
  override them in subclasses.

See testng-team/testng#313

Fixes w3c#192
spadgett pushed a commit to spadgett/ldp-testsuite that referenced this issue Oct 7, 2014
- Only set static variables from LdpTest setup. Setup is only called
  once even when several classes inherit from LdpTest.
- Change setup and tear down method names so we don't accidentally
  override them in subclasses.

See testng-team/testng#313

Fixes w3c#192
@juherr
Copy link
Member

juherr commented Jul 20, 2016

@DurgaManickam Please, could you ask it on StackOverflow or the mailing list first?

@krmahadevan
Copy link
Member

@juherr - I this we can close this issue. WDYT ?

@juherr
Copy link
Member

juherr commented Sep 17, 2017

It works as expected: a before suite method is only called once by suite, even if it is located in a superclass.

@abdulrazak-mumms
Copy link

abdulrazak-mumms commented Sep 21, 2017

any conclusion fix for this issue ?

 java.lang.IllegalStateException: No TestRunnerAdaptor found, @BeforeSuite has not been called

@juherr
Copy link
Member

juherr commented Sep 21, 2017

@abdulrazak-mumms Could you open a new issue and include a sample which reproduces the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants