not getting SoftAssert.assertAll() functionality

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

not getting SoftAssert.assertAll() functionality

garvitagirotraev
Hi, 

I am using soft assertion as :

In base class i have declared object of SoftAssert class :

Class Base
{
SoftAssert softassert = new SoftAssert();
}

Class Util
{

 createchart(parameter1)
     { 
       // chart is created
       verifyLabels();
       verifyTooltips();
//   softassert.assertAll(); ----- if i use here, test does not go to next createchart();
      }
    
public void verifyLabels()
     {
       softassert.AssertTrue("actual","expected","message");
     }
}

Class PieChart
{
@Test
    public void makePieChart()
      {
        Util util = new Util();
        util.createchart("comapny");
        util.createchart("year");
// softassert.assertAll()   ------ it does not report failure
      }
}

Now i tried using softassert.assetAll() at 2 places :
1. Last line in createchart() method, it works fine. If verifyLabels() fail, still verifyTooltip() executes. But In PieChart class, if 1st craetechart() fails , next is not executed.

2. Last line in PieChart class as i want chart is created for all parameters , then it should report failure. It doe snot report any failure.


Am i using it incorrectly or this is how it should work ?

Thanks!!



--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: not getting SoftAssert.assertAll() functionality

⇜Krishnan Mahadevan⇝
The basic idea is : Every @Test method has its own SoftAssert variable. Please donot share the SoftAssert variable across multiple @Test methods by defining it as a data member of the class.

SoftAssert tends to remember all the assertions done so far, so if you had assertion failures in method1 and if you used the same SoftAssertion object in method2, then method2() will also fail, even though it didn't have any failures.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

On Wed, Nov 29, 2017 at 2:39 PM, <[hidden email]> wrote:
Hi, 

I am using soft assertion as :

In base class i have declared object of SoftAssert class :

Class Base
{
SoftAssert softassert = new SoftAssert();
}

Class Util
{

 createchart(parameter1)
     { 
       // chart is created
       verifyLabels();
       verifyTooltips();
//   softassert.assertAll(); ----- if i use here, test does not go to next createchart();
      }
    
public void verifyLabels()
     {
       softassert.AssertTrue("actual","expected","message");
     }
}

Class PieChart
{
@Test
    public void makePieChart()
      {
        Util util = new Util();
        util.createchart("comapny");
        util.createchart("year");
// softassert.assertAll()   ------ it does not report failure
      }
}

Now i tried using softassert.assetAll() at 2 places :
1. Last line in createchart() method, it works fine. If verifyLabels() fail, still verifyTooltip() executes. But In PieChart class, if 1st craetechart() fails , next is not executed.

2. Last line in PieChart class as i want chart is created for all parameters , then it should report failure. It doe snot report any failure.


Am i using it incorrectly or this is how it should work ?

Thanks!!



--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: not getting SoftAssert.assertAll() functionality

garvitagirotraev
Thank you for explaining !!

I tried implementing again, made a new object in Util class where actually assertion is done in verifyLabels() method. Created new object in pie chart class where i want to use assertAll();

I intentionally failed assertion in VerifyLabels() method. In this case, both createchart() are executed in PieChart class but assertion failure is not reported. Test case is passed. 

Is there a way, so that i can report all failures in end of PieChart class?

Thanks !! 

On Wednesday, November 29, 2017 at 2:42:42 PM UTC+5:30, Krishnan Mahadevan wrote:
The basic idea is : Every @Test method has its own SoftAssert variable. Please donot share the SoftAssert variable across multiple @Test methods by defining it as a data member of the class.

SoftAssert tends to remember all the assertions done so far, so if you had assertion failures in method1 and if you used the same SoftAssertion object in method2, then method2() will also fail, even though it didn't have any failures.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/
My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

On Wed, Nov 29, 2017 at 2:39 PM, <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DZLctk7-AAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">garvitag...@...> wrote:
Hi, 

I am using soft assertion as :

In base class i have declared object of SoftAssert class :

Class Base
{
SoftAssert softassert = new SoftAssert();
}

Class Util
{

 createchart(parameter1)
     { 
       // chart is created
       verifyLabels();
       verifyTooltips();
//   softassert.assertAll(); ----- if i use here, test does not go to next createchart();
      }
    
public void verifyLabels()
     {
       softassert.AssertTrue("actual","expected","message");
     }
}

Class PieChart
{
@Test
    public void makePieChart()
      {
        Util util = new Util();
        util.createchart("comapny");
        util.createchart("year");
// softassert.assertAll()   ------ it does not report failure
      }
}

Now i tried using softassert.assetAll() at 2 places :
1. Last line in createchart() method, it works fine. If verifyLabels() fail, still verifyTooltip() executes. But In PieChart class, if 1st craetechart() fails , next is not executed.

2. Last line in PieChart class as i want chart is created for all parameters , then it should report failure. It doe snot report any failure.


Am i using it incorrectly or this is how it should work ?

Thanks!!



--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="DZLctk7-AAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="DZLctk7-AAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
Visit this group at <a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: not getting SoftAssert.assertAll() functionality

kool79
It seems you created 2 different instances of softassert - first in PieChart  class and second in VerifyLabels. Each of those instances collects own errors. That's why you assertion failure is not reported. To fix this you need to use SAME instance for verifications and for assertAll(). To do this you need to pass it into every method which make verifications:

Class Util{
 
    static void createchart(SoftAssert sa, String parameter1){ 
       //chart is created
       verifyLabels(sa);
       verifyTooltips(sa);
    }
    
static void verifyLabels(SoftAssert sa){
        sa.AssertTrue("actual","expected","message");
    }
}

Class PieChart{

    @Test
    public void makePieChart(){
    SoftAssert sa = new SoftAssert();
        Util.createchart(sa, "comapny");
        Util.createchart(sa, "year");
sa.assertAll();
    }
}

BTW good practice is to use asserts only in @Test - methods.

If you don't like to pass softAsserts everywhere you can write something like this:


public class AssertsListener implements IInvokedMethodListener {

@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
Asserts.clear();
}


@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
try {
Asserts.assertAll();
} catch (AssertionError ae) {
Throwable error = testResult.getThrowable();
if (error == null)
testResult.setThrowable(ae);
else
testResult.setThrowable(new RuntimeException(
String.format("%s. \nOther errors: %s", error, ae)));

if (error instanceof SkipException)
testResult.setStatus(ITestResult.SKIP);
else
testResult.setStatus(ITestResult.FAILURE);
}
}
}

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

class Asserts {

private final static ThreadLocal<SoftAssert> SA_CONTAINER =
new ThreadLocal<>();

public static void clear() { SA_CONTAINER.set(new SoftAssert());}

public static SoftAssert save() { return SA_CONTAINER.get();}

public static void assertAll() {
try {
SA_CONTAINER.get().assertAll();
} finally {
clear();
}
}
}


@Listeners(AssertsListener.class)
public class MyTests {

@Test()
void myTest() {
Asserts.clear(); // optional with AssertsListener
String actual = "222";
Asserts.save().assertEquals(actual, "111");
Asserts.save().assertEquals(actual, "222");
Asserts.save().assertEquals(actual, "333");
//Asserts.assertAll(); // optional with AssertsListener
}
}



среда, 29 ноября 2017 г., 11:33:07 UTC+2 пользователь [hidden email] написал:
Thank you for explaining !!

I tried implementing again, made a new object in Util class where actually assertion is done in verifyLabels() method. Created new object in pie chart class where i want to use assertAll();

I intentionally failed assertion in VerifyLabels() method. In this case, both createchart() are executed in PieChart class but assertion failure is not reported. Test case is passed. 

Is there a way, so that i can report all failures in end of PieChart class?

Thanks !! 

On Wednesday, November 29, 2017 at 2:42:42 PM UTC+5:30, Krishnan Mahadevan wrote:
The basic idea is : Every @Test method has its own SoftAssert variable. Please donot share the SoftAssert variable across multiple @Test methods by defining it as a data member of the class.

SoftAssert tends to remember all the assertions done so far, so if you had assertion failures in method1 and if you used the same SoftAssertion object in method2, then method2() will also fail, even though it didn't have any failures.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ <a href="http://wakened-cognition.blogspot.com/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/
My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

On Wed, Nov 29, 2017 at 2:39 PM, <[hidden email]> wrote:
Hi, 

I am using soft assertion as :

In base class i have declared object of SoftAssert class :

Class Base
{
SoftAssert softassert = new SoftAssert();
}

Class Util
{

 createchart(parameter1)
     { 
       // chart is created
       verifyLabels();
       verifyTooltips();
//   softassert.assertAll(); ----- if i use here, test does not go to next createchart();
      }
    
public void verifyLabels()
     {
       softassert.AssertTrue("actual","expected","message");
     }
}

Class PieChart
{
@Test
    public void makePieChart()
      {
        Util util = new Util();
        util.createchart("comapny");
        util.createchart("year");
// softassert.assertAll()   ------ it does not report failure
      }
}

Now i tried using softassert.assetAll() at 2 places :
1. Last line in createchart() method, it works fine. If verifyLabels() fail, still verifyTooltip() executes. But In PieChart class, if 1st craetechart() fails , next is not executed.

2. Last line in PieChart class as i want chart is created for all parameters , then it should report failure. It doe snot report any failure.


Am i using it incorrectly or this is how it should work ?

Thanks!!



--
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 [hidden email].
Visit this group at <a href="https://groups.google.com/group/testng-users" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: not getting SoftAssert.assertAll() functionality

garvitagirotraev
Thanks, using same object worked to collect assertion. Will try implementing using listeners.

On Saturday, December 2, 2017 at 1:48:24 AM UTC+5:30, [hidden email] wrote:
It seems you created 2 different instances of softassert - first in PieChart  class and second in VerifyLabels. Each of those instances collects own errors. That's why you assertion failure is not reported. To fix this you need to use SAME instance for verifications and for assertAll(). To do this you need to pass it into every method which make verifications:

Class Util{
 
    static void createchart(SoftAssert sa, String parameter1){ 
       //chart is created
       verifyLabels(sa);
       verifyTooltips(sa);
    }
    
static void verifyLabels(SoftAssert sa){
        sa.AssertTrue("actual","expected","message");
    }
}

Class PieChart{

    @Test
    public void makePieChart(){
    SoftAssert sa = new SoftAssert();
        Util.createchart(sa, "comapny");
        Util.createchart(sa, "year");
sa.assertAll();
    }
}

BTW good practice is to use asserts only in @Test - methods.

If you don't like to pass softAsserts everywhere you can write something like this:


public class AssertsListener implements IInvokedMethodListener {

@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
Asserts.clear();
}


@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
try {
Asserts.assertAll();
} catch (AssertionError ae) {
Throwable error = testResult.getThrowable();
if (error == null)
testResult.setThrowable(ae);
else
testResult.setThrowable(new RuntimeException(
String.format("%s. \nOther errors: %s", error, ae)));

if (error instanceof SkipException)
testResult.setStatus(ITestResult.SKIP);
else
testResult.setStatus(ITestResult.FAILURE);
}
}
}

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

class Asserts {

private final static ThreadLocal<SoftAssert> SA_CONTAINER =
new ThreadLocal<>();

public static void clear() { SA_CONTAINER.set(new SoftAssert());}

public static SoftAssert save() { return SA_CONTAINER.get();}

public static void assertAll() {
try {
SA_CONTAINER.get().assertAll();
} finally {
clear();
}
}
}


@Listeners(AssertsListener.class)
public class MyTests {

@Test()
void myTest() {
Asserts.clear(); // optional with AssertsListener
String actual = "222";
Asserts.save().assertEquals(actual, "111");
Asserts.save().assertEquals(actual, "222");
Asserts.save().assertEquals(actual, "333");
//Asserts.assertAll(); // optional with AssertsListener
}
}



среда, 29 ноября 2017 г., 11:33:07 UTC+2 пользователь [hidden email] написал:
Thank you for explaining !!

I tried implementing again, made a new object in Util class where actually assertion is done in verifyLabels() method. Created new object in pie chart class where i want to use assertAll();

I intentionally failed assertion in VerifyLabels() method. In this case, both createchart() are executed in PieChart class but assertion failure is not reported. Test case is passed. 

Is there a way, so that i can report all failures in end of PieChart class?

Thanks !! 

On Wednesday, November 29, 2017 at 2:42:42 PM UTC+5:30, Krishnan Mahadevan wrote:
The basic idea is : Every @Test method has its own SoftAssert variable. Please donot share the SoftAssert variable across multiple @Test methods by defining it as a data member of the class.

SoftAssert tends to remember all the assertions done so far, so if you had assertion failures in method1 and if you used the same SoftAssertion object in method2, then method2() will also fail, even though it didn't have any failures.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ <a href="http://wakened-cognition.blogspot.com/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/
My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

On Wed, Nov 29, 2017 at 2:39 PM, <[hidden email]> wrote:
Hi, 

I am using soft assertion as :

In base class i have declared object of SoftAssert class :

Class Base
{
SoftAssert softassert = new SoftAssert();
}

Class Util
{

 createchart(parameter1)
     { 
       // chart is created
       verifyLabels();
       verifyTooltips();
//   softassert.assertAll(); ----- if i use here, test does not go to next createchart();
      }
    
public void verifyLabels()
     {
       softassert.AssertTrue("actual","expected","message");
     }
}

Class PieChart
{
@Test
    public void makePieChart()
      {
        Util util = new Util();
        util.createchart("comapny");
        util.createchart("year");
// softassert.assertAll()   ------ it does not report failure
      }
}

Now i tried using softassert.assetAll() at 2 places :
1. Last line in createchart() method, it works fine. If verifyLabels() fail, still verifyTooltip() executes. But In PieChart class, if 1st craetechart() fails , next is not executed.

2. Last line in PieChart class as i want chart is created for all parameters , then it should report failure. It doe snot report any failure.


Am i using it incorrectly or this is how it should work ?

Thanks!!



--
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 [hidden email].
Visit this group at <a href="https://groups.google.com/group/testng-users" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.