@BeforeClass is not executed before @BeforeTest ?

1 278 peržiūros
Praleisti ir pereiti prie pirmo neskaityto pranešimo

thierry.henrio

neskaityta,
2008-04-23 13:19:022008-04-23
kam: testng-users
Hi all, I'm a bit confused with the following test

public class BeforeClassIsNotCalledBeforeTest
{
private String beforeClass;
private String beforeTest;

@BeforeClass
protected void init() {
beforeClass = "init";
}

@BeforeTest
protected void clean() {
Assert.assertNotNull(beforeClass,
"@BeforeClass is not called before @BeforeTest in 5.8 ??!");
beforeTest = "clean";
}

@Test
public void wasBeforeClassReallyCalled() {
Assert.assertNotNull(beforeClass,
"@BeforeClass is not called before @Test");
Assert.assertNotNull(beforeTest,
"@BeforeTest is not called before @Test");
}
}

result is clean() fails with
java.lang.AssertionError: @BeforeClass is not called before
@BeforeTest expected:<true> but was:<false>
at org.testng.Assert.fail(Assert.java:84)
at org.testng.Assert.failNotEquals(Assert.java:438)
at org.testng.Assert.assertTrue(Assert.java:32)
at org.testng.Assert.assertNotNull(Assert.java:352)
at
com.foo.test.BeforeClassIsNotCalledBeforeTest.clean(BeforeClassIsNotCalledBeforeTest.java:
23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:
580)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:
398)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:145)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:82)
at org.testng.TestRunner.beforeRun(TestRunner.java:490)
at org.testng.TestRunner.run(TestRunner.java:458)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:301)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:296)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:276)
at org.testng.SuiteRunner.run(SuiteRunner.java:191)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:808)
at org.testng.TestNG.runSuitesLocally(TestNG.java:776)
at org.testng.TestNG.run(TestNG.java:701)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)

wasBeforeClassReallyCalled() and init() are skipped

How would you advice me to have
1 initialisation code runned once
1 other initialisation code runned before each test

Regards,

Cédric Beust ♔

neskaityta,
2008-04-23 13:22:202008-04-23
kam: testng...@googlegroups.com
Aren't you confusing @BeforeText (before a <test> tag) and @BeforeMethod (before a test method)?

--
Cédric

thierry.henrio

neskaityta,
2008-04-23 18:36:292008-04-23
kam: testng-users
Hi, Cedric
to be more precise, i'm willing to move from junit4 to testng on one
project at office

one test class is several test method that generate files in a
"isolated" directory
it has the following junit4 structure
@BeforeClass : init ioc container and isolated directory (private
static)
@Before : ensure the (private static) isolated directory is empty
@Test : do produce some files, assert there is the good number, check
each of them

moving to testng with bare import and junit @Before to tesng
@BeforeTest did made my build fail (i do not account Assert which was
a great pain :))

so how can I manage to do this in testng ?
I tried "move all @BeforeClass code to @BeforeTest"
this works if idempotent init code is executed ... and that is a
chance but i'm not happy with it : it looks like tearDown+setUp/
testXXX in junit 3.8.1 semantic

here there not a means to achieve in testng what i was doing in
junit4 ?
Regards,


On Apr 23, 7:22 pm, Cédric Beust ♔ <cbe...@google.com> wrote:
> Aren't you confusing @BeforeText (before a <test> tag) and @BeforeMethod
> (before a test method)?
>
> --
> Cédric
>
> On Wed, Apr 23, 2008 at 10:19 AM, thierry.henrio <thierry.hen...@gmail.com>

Cédric Beust ♔

neskaityta,
2008-04-23 18:39:432008-04-23
kam: testng...@googlegroups.com
The equivalent of JUnit4's "@Before" is "@BeforeMethod", not "@BeforeTest".

In TestNG, a Test is a set of classes surrounded by a <test> tag in testng.xml.

--
Cedric
--
Cédric

Mark Derricutt

neskaityta,
2008-04-23 18:45:372008-04-23
kam: testng...@googlegroups.com
I've often found that slightly confusing for newbies - @BeforeTest sounds like it should match with @BeforeMethod.   I almost want a time machine to we could suggest suggest moving @Test to @TestMethod and @BeforeTestMethod - but they're also rather wordy.  And the time machines broken.

Mark


On Thu, Apr 24, 2008 at 10:39 AM, Cédric Beust ♔ <cbe...@google.com> wrote:
The equivalent of JUnit4's "@Before" is "@BeforeMethod", not "@BeforeTest".

In TestNG, a Test is a set of classes surrounded by a <test> tag in testng.xml.

--
"It is easier to optimize correct code than to correct optimized code." -- Bill Harlan

thierry.henrio

neskaityta,
2008-04-24 05:05:002008-04-24
kam: testng-users
It's ok Cedric, but I must admit I was a bit confused, as Mark pointed
out

so, I have moved 8 junit4 to testng, with a false translation (@Before
@BeforeTest)
no testng.xml configuration file
mvn test
Tests run: 20, Failures: 1, Errors: 0, Skipped: 19
the one failure is the first @BeforeTest, all the rest is skipped

now I understand I must use @BeforeMethod instead of @BeforeTest
this is done, and i've now
Tests run: 10, Failures: 0, Errors: 0, Skipped: 0

btw, i now have the impression to run fewer tests (10 now versus 20
before :)

In my java code, having @BeforeTest just 3 lines above a @Test makes
me feel the "Test" of @BeforeTest is just bellow and not a <test> of a
xml configuration file i have not written ...
when using testng with maven "out of the box", i do not know about any
<test>

so yes, Mark, @BeforeTestMethod @TestMethod in code would bring more
hint than @BeforeMethod @Test (what is the Method ?)
could another way be ?
another name for Test = <test> concept so that it is not confused
with @Test ?

Cédric Beust ♔

neskaityta,
2008-04-24 11:02:532008-04-24
kam: testng...@googlegroups.com
I agree, but I'd like to point out that when I introduced this terminology four years ago,
  • It was consistent with the way testng.xml is structured (<suite> is wrapped by @BeforeSuite/@AfterSuite, <test> is wrapped by @BeforeTest/@AfterTest and <class> is wrapped by @BeforeClass/@AfterClass), and it still is.

  • There was no standard way of describing this (JUnit itself has never been very clear on what a test suite actually is and the documentation sometimes describes a test as a test method and sometimes as a test class).
On the ironic side, I think JUnit4 is making the same mistake that I made (choosing an ambiguous annotation name) with @Before, but that's a separate topic :-)

--
Cedric
--
Cédric

Cédric Beust ♔

neskaityta,
2008-04-24 11:06:032008-04-24
kam: testng...@googlegroups.com
On Thu, Apr 24, 2008 at 2:05 AM, thierry.henrio <thierry...@gmail.com> wrote:

It's ok Cedric, but I must admit I was a bit confused, as Mark pointed
out

so, I have moved 8 junit4 to testng, with a false translation (@Before
@BeforeTest)
no testng.xml configuration file
mvn test
Tests run: 20, Failures: 1, Errors: 0, Skipped: 19
the one failure is the first @BeforeTest, all the rest is skipped

now I understand I must use @BeforeMethod instead of @BeforeTest
this is done, and i've now
Tests run: 10, Failures: 0, Errors: 0, Skipped: 0

btw, i now have the impression to run fewer tests (10 now versus 20
before :)

That's certainly surprising, can you post an example where changing a @BeforeTest into a @BeforeMethod will change the number of tests run?

 
In my java code, having @BeforeTest just 3 lines above a @Test makes
me feel the "Test" of @BeforeTest is just bellow and not a <test> of a
xml configuration file i have not written ...
when using testng with maven "out of the box", i do not know about any
<test>

so yes, Mark, @BeforeTestMethod @TestMethod in code would bring more
hint than @BeforeMethod @Test (what is the Method ?)
could another way be ?
 another name for Test = <test> concept so that it is not confused
with  @Test ?

It's unfortunately a bit late for that, and I believe that in the TestNG world, most users are now comfortable with the concepts of <suite>, <test> and <class> as they are documented, but maybe I'm mistaken...

--
Cédric

thierry.henrio

neskaityta,
2008-04-25 08:57:132008-04-25
kam: testng-users
For the "number of tests"

public class FailingBeforeTestIncreaseNumberOfTests
{
@BeforeClass
protected void init() {
}

@BeforeTest
protected void clean() {
throw new RuntimeException("failure");
}

@Test(groups={"testng"})
public void oneTest() {
}
}

I run this test class with this code, then replaced @BeforeTest by
@BeforeMethod, then removed throw from @BeforeMethod

running under eclipse I've
with @BeforeTest
Tests : 1/1 Methods=3/1, Passed=0, Failed=1, Skipped=2

with @BeforeMethod
Tests : 1/1 Methods=2/1, Passed=0, Failed=1, Skipped=1

with @BeforeMethod with no throw
Tests : 1/1 Methods=1/1, Passed=1, Failed=0, Skipped=0


running under surefire, I've
with @BeforeTest
Tests run: 3, Failures: 1, Errors: 0, Skipped: 2, Time elapsed: 0.436
sec <<< FAILURE!

with @BeforeMethod with throw
Tests run: 2, Failures: 1, Errors: 0, Skipped: 1, Time elapsed: 0.436
sec <<< FAILURE!

with @BeforeMethod without throw
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.427
sec

so i looks like surefire displays the eclipse "Methods" as "Test run"
i both display, a failing @BeforeTest make the @BeforeTest look like a
Skipped test

this is not important for me, as I my goal is to have some good tests
and, since one time, no more failure on them :)

On 24 avr, 17:06, Cédric Beust ♔ <cbe...@google.com> wrote:
> That's certainly surprising, can you post an example where changing a
> @BeforeTest into a @BeforeMethod will change the number of tests run?
>
> Cédric

Cédric Beust ♔

neskaityta,
2008-04-25 12:11:262008-04-25
kam: testng...@googlegroups.com
If you remove a throw between the two runs, it's not surprising to see the number of tests change.  When a configuration method throws, it will cause subsequent test methods be be skipped.  "Which" test methods get skipped depend on which configuration method failed:
  • @BeforeSuite -> the entire suite gets skipped
  • @BeforeTest -> all the test methods in the <test> tag get skipped
  • @BeforeGroup -> all the test methods in that group get skipped
  • @BeforeClass -> all the test methods in that class get skipped
  • @BeforeMethod -> all the test methods following this method will get skipped
It seems to me the behavior you're seeing is consistent with this...

--
Cédric
Atsakyti visiems
Atsakyti autoriui
Persiųsti
0 naujų pranešimų