Assertions problem

61 views
Skip to first unread message

Patryk

unread,
Mar 11, 2013, 4:00:28 AM3/11/13
to testng...@googlegroups.com
Hello.

I have some methods that I use in a test method. For example

@Test
testMethodName() {
method1();
method2();
method3(); 
...
}

Each of the methods have assertion inside. For example: if in the method1() assertion is failed, test method is still running. Is there a way to stop the test in the way I'm doing it? Is it doable if the testMethod is compsed of the submethods?
Example output:

Assertion passed: Category list displayed
Assertion failed: No records found
Assertion failed: No product to display

I would like to stop it after 1st assertion failed.
Hope its clear what Im trying to achiev here.

Tomek Kaczanowski

unread,
Mar 11, 2013, 6:59:13 AM3/11/13
to testng...@googlegroups.com
Hi Patryk,

this is not how TestNG behaves. Not sure how you create your
assertions, but there must be something strange about it.

See this example:

public class BlahTest {

@Test
public void testMethodName() {
method1();
method2();
method3();
}

private void method1() {
System.out.println("method 1");
Assert.assertTrue(true);
}

private void method2() {
System.out.println("method 2");
Assert.assertTrue(false);
}

private void method3() {
System.out.println("method 3");
Assert.assertTrue(true);
}
}

after running you get:

method 1
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at BlahTest.method2(BlahTest.java:26)
at BlahTest.testMethodName(BlahTest.java:12)
method 2


--
Regards / Pozdrawiam
Tomek Kaczanowski
http://practicalunittesting.com

Patryk

unread,
Mar 11, 2013, 8:11:11 AM3/11/13
to testng...@googlegroups.com
Well, I was trying to use  org.testng.asserts.Assertion class which was introduced in testNG v6.8 (??) not the org.testng.Assert class.
Even if I do something like this

Assertion assertion = new Assertion();
assertion .assertTrue(false, "Message");

I still get the test result as passed, not failed :/ Maybe Im doing something wrong?
With Assert class it works ok.

Tomek Kaczanowski

unread,
Mar 11, 2013, 8:26:29 AM3/11/13
to testng...@googlegroups.com
Any particular reasons you use Assertion instead of Assert? It not
then just stick with Assert.

P.S. Regarding Assertion have a look at
http://beust.com/weblog/2012/07/29/reinventing-assertions/

Patryk

unread,
Mar 11, 2013, 8:36:00 AM3/11/13
to testng...@googlegroups.com
Yes, because I'm using onAssertSuccess() and onAssertFailure() to log assertions results.
Maybe I am doing it wrong. Did exactly the same way as it is representec in Cedric's blog and still got test result passed even if the assertion failed.
Message has been deleted

Cédric Beust ♔

unread,
Mar 11, 2013, 3:29:27 PM3/11/13
to testng...@googlegroups.com
Hi Patryk,

Can you post your exact test class? I have tests for the new assertion support but of course, it's always possible that there are bugs.

-- 
Cédric


-- 
Cédric



On Mon, Mar 11, 2013 at 5:36 AM, Patryk <kulecki...@gmail.com> wrote:
Yes, because I'm using onAssertSuccess() and onAssertFailure() to log assertions results.
Maybe I am doing it wrong. Did exactly the same way as it is representec in Cedric's blog and still got test result passed even if the assertion failed.

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

Patryk

unread,
Mar 11, 2013, 3:43:42 PM3/11/13
to testng...@googlegroups.com, ced...@beust.com
Hello,

My test class is a little bit messy right now since I tried to do something with my issue. I decided to check examples posted on your blog and I do not know why it is not working.

Assert.assertTrue(false, "Message") works fine and test is failing, but 

private Assertion assertion = new Assertion();
assertion.assertTrue(false, "Message") 

still marks test as passed. Maybe I do not understand correctly what is going on. Maybe I should override some methods here? I tried to extend Assertion class and so on but no luck yet.

Cédric Beust ♔

unread,
Mar 11, 2013, 4:13:48 PM3/11/13
to Patryk, testng...@googlegroups.com
Hi Patryk,

Well, I have to apologize: this was indeed broken... Looks like I need to add more tests for the Assertion support.

Can you try http://testng.org/beta and report back?


-- 
Cédric

Patryk

unread,
Mar 11, 2013, 4:17:44 PM3/11/13
to testng...@googlegroups.com, ced...@beust.com
My assertion class with 2 overriden methods. I need them to report assertions results to the html file.

public class myAssert extends Assertion {
@Override
public void onAssertSuccess(IAssert assertCommand) {
super.onAssertSuccess(assertCommand);
Reporter.log("Assertion passed: " + assertCommand.getMessage(), true);
}
@Override
public void onAssertFailure(IAssert assertCommand) {
super.onAssertFailure(assertCommand);
Reporter.log("Assertion failed: " + assertCommand.getMessage(), true);
}
}

and simple test with that class:

public class TestClass {
static WebDriver driver;
static myAssert assertion = new myAssert();

@BeforeTest
public static void beforeMethod() {
System.setProperty("webdriver.chrome.driver",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
}
@AfterTest
public static void afterMethod() {
driver.close();
}
@Test
public void f() {
driver.get("http://www.google.com");
assertion.assertEquals(driver.getTitle(), "Googlee", "Message 1");
// Assert.assertEquals(driver.getTitle(), "Googlea", "Message 2");
}
}

and test result

Assertion failed: Message 1
===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Shouldn't it be mark as failed? With Assert class it seems to be ok and it is marked as failed.
Message has been deleted

Patryk

unread,
Mar 11, 2013, 4:41:00 PM3/11/13
to testng...@googlegroups.com, Patryk, ced...@beust.com
In beta version it is also not working. 
Don't know it if helps you, but during debugging there is a ClassNotFoundException. Maybe this is a reason why it is not working?

Cédric Beust ♔

unread,
Mar 11, 2013, 5:21:02 PM3/11/13
to Patryk, testng...@googlegroups.com
Stack trace?

Also, this is what I'm seeing:

public class AA {

  @Test
  public void f() {
    Assertion a = new Assertion();
    a.assertTrue(false);
  }
}

[TestNG] Running:
  /Users/cbeust/java/testng/src/test/resources/testng-single.yaml

FAILED: f
java.lang.AssertionError: expected [true] but found [false]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:494)
at org.testng.Assert.assertTrue(Assert.java:42)
at org.testng.Assert.assertTrue(Assert.java:52)
at org.testng.asserts.Assertion$2.doAssert(Assertion.java:118)



-- 
Cédric

Patryk

unread,
Mar 11, 2013, 6:38:33 PM3/11/13
to testng...@googlegroups.com, Patryk, ced...@beust.com
When trying to use: assertion.assertTrue(false);

java.lang.NoSuchMethodError: org.testng.asserts.Assertion.assertTrue(Z)V
at assertions.test.TestClass.f(TestClass.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.run(TestNG.java:1036)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

While assertion.assertEquals(var, var, "Message")

java.lang.Thread.getStackTrace(Unknown Source)
assertions.test.TestClass.afterMethod(TestClass.java:27)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
org.testng.TestRunner.afterRun(TestRunner.java:1021)
org.testng.TestRunner.run(TestRunner.java:621)
org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
org.testng.SuiteRunner.run(SuiteRunner.java:240)
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
org.testng.TestNG.run(TestNG.java:1036)
org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Cédric Beust ♔

unread,
Mar 11, 2013, 6:54:53 PM3/11/13
to Patryk, testng...@googlegroups.com
That's not a ClassCastException. Something seems to be wrong with your classpath.

-- 
Cédric

Message has been deleted
Message has been deleted

Patryk

unread,
Mar 12, 2013, 5:50:34 AM3/12/13
to testng...@googlegroups.com
In beta version is working fine. Thanks for the help. Had some problem but solved them :)
Reply all
Reply to author
Forward
0 new messages