Test Class variables that were initialized in BeforeSuite, are null during the test

2,041 views
Skip to first unread message

דקלה וילגה

unread,
Jun 14, 2011, 9:30:29 AM6/14/11
to testng...@googlegroups.com
Hi,

I have a Base Test Class from which all my test classes inherit. There are some fields in the base class that are initialized before the suite execution.

When I run only one test - everything works fine. But when running several tests, the class fields are null during some of the tests.

I attach a zip file of the eclipse project that I created to demonstrate the problem.

It contains the base class:

public class BaseTest
{
    protected String someGeneralSetting;
   
    @BeforeSuite
    public void init()
    {
        someGeneralSetting = "ABC";
    }
}

And an example of a test class:

public class MyTest extends BaseTest
{
    @Test
    public void test1()
    {
        assert someGeneralSetting != null;
    }
   
    @Test
    public void test2()
    {
        assert someGeneralSetting != null;
    }
}

The xml is as follows:

<suite name="Suite" parallel="false">
    <test name="Test1">
         <classes>
              <class name="MyTest">
                  <methods>
                      <include name="test1" />
                  </methods>
              </class>
          </classes>
   </test>
  
     <test name="Test2">
         <classes>
              <class name="MyTest">
                  <methods>
                      <include name="test2" />
                  </methods>
              </class>
          </classes>
   </test>
</suite>


I expect that both tests will pass, but in fact only the second test passes, while the first fails.

Is this a bug, or am I doing something wrong?

Thanks,
Dikla
TestProject2.zip

Carsten

unread,
Jun 14, 2011, 9:52:05 AM6/14/11
to testng-users
Hi Dikla,

in your example you are using two test-tags, therefore two instances
of MyTest get created. BeforeSuite gets only called for one of those
two instances.

Change your annotation in BaseTest from @BeforeSuite to @BeforeTest
and it should work.

Carsten
>  TestProject2.zip
> 1526KAnzeigenHerunterladen

דקלה וילגה

unread,
Jun 14, 2011, 3:14:15 PM6/14/11
to testng...@googlegroups.com
Right, thanks a lot!
--
דקלה




--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


Reply all
Reply to author
Forward
0 new messages