Test annotation

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

Test annotation

Niklas Saers

Dear sirs,
I need to make annotations to my tests. How can I do this?

Let me give my example:
I have a lot of test-data acumulated in a database, and I do a while
loop to throw it all at a set of functions. These functions may throw
an exception. So my test function can typically look like this:

@Test(groups = { "alpha"}) public void requestProductGroup_V1() throws
Exception {
        TestData data = new TestData();
        Iterator i = data.EuroAlarmProductGroups.entrySet().iterator();
        while(i.hasNext()) {
                Map.Entry<String, String> e = (Map.Entry<String, String>) i.next();
                // e.Key() contains the test-value
                // e.Value() contains the expected value
                String s = testobject.myFunc(e.Key());
                assertEquals("Some error message", e.Value(), s);
        }
}

Now, in some cases this may fail (believe it or not ;-) ) and then an
exception may be thrown, such as in this case:
java.lang.StringIndexOutOfBoundsException: String index out of range:
7
        at java.lang.String.charAt(String.java:558)

Ok, easy problem to solve, index out of range. But hey, for what data
did this happen?? No idea. So, what I need to do is annotate the test
with data while the test is running. I would like the error to be
something like:

org.testng.AssertJUnit.exceptionCaught:
StringIndexOutOfBoundsException in test. Test data used [<key value>,
<value value>]
java.lang.StringIndexOutOfBoundsException: String index out of range:
7
        at java.lang.String.charAt(String.java:558)

So, how can I annotate my test data during the test so that if it
fails with an exception, I will know for what data it failed?

Cheers

   Nik


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

Reply | Threaded
Open this post in threaded view
|

Re: Test annotation

Alexandru Popescu ☀

Considering that this is data you are internally handling probably the
only solution for you would be:

/// data here
try {
    // operations
}
catch(Exception ex) {
    Assert.fail("data: " + data, ex);
}


./alex
--
.w( the_mindstorm )p.
  TestNG co-founder
EclipseTestNG Creator

On 4/12/07, Nik <[hidden email]> wrote:

>
> Dear sirs,
> I need to make annotations to my tests. How can I do this?
>
> Let me give my example:
> I have a lot of test-data acumulated in a database, and I do a while
> loop to throw it all at a set of functions. These functions may throw
> an exception. So my test function can typically look like this:
>
> @Test(groups = { "alpha"}) public void requestProductGroup_V1() throws
> Exception {
>         TestData data = new TestData();
>         Iterator i = data.EuroAlarmProductGroups.entrySet().iterator();
>         while(i.hasNext()) {
>                 Map.Entry<String, String> e = (Map.Entry<String, String>) i.next();
>                 // e.Key() contains the test-value
>                 // e.Value() contains the expected value
>                 String s = testobject.myFunc(e.Key());
>                 assertEquals("Some error message", e.Value(), s);
>         }
> }
>
> Now, in some cases this may fail (believe it or not ;-) ) and then an
> exception may be thrown, such as in this case:
> java.lang.StringIndexOutOfBoundsException: String index out of range:
> 7
>         at java.lang.String.charAt(String.java:558)
>
> Ok, easy problem to solve, index out of range. But hey, for what data
> did this happen?? No idea. So, what I need to do is annotate the test
> with data while the test is running. I would like the error to be
> something like:
>
> org.testng.AssertJUnit.exceptionCaught:
> StringIndexOutOfBoundsException in test. Test data used [<key value>,
> <value value>]
> java.lang.StringIndexOutOfBoundsException: String index out of range:
> 7
>         at java.lang.String.charAt(String.java:558)
>
> So, how can I annotate my test data during the test so that if it
> fails with an exception, I will know for what data it failed?
>
> Cheers
>
>    Nik
>
>
> >
>

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

Reply | Threaded
Open this post in threaded view
|

Re: Test annotation

Cédric Beust ♔


On 4/12/07, Alexandru Popescu ☀ <[hidden email]> wrote:

Considering that this is data you are internally handling probably the
only solution for you would be:

/// data here
try {
    // operations
}
catch(Exception ex) {
    Assert.fail("data: " + data, ex);
}

Additionally, if you are iterating over 100 elements and that more than one can cause an exception and you want to know which ones, you can catch the exception inside the loop, store the exception in a collection, then iterate to the next element, and at the end, if the collection is not empty, throw an exception with its content.

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

Reply | Threaded
Open this post in threaded view
|

Re: Test annotation

Alex Ruiz-2
Excellent! Thanks for the clarification Cedric ;)

On 4/12/07, Cédric Beust ♔ <[hidden email]> wrote:


On 4/12/07, Alexandru Popescu ☀ <[hidden email]> wrote:

Considering that this is data you are internally handling probably the
only solution for you would be:

/// data here
try {
    // operations
}
catch(Exception ex) {
    Assert.fail("data: " + data, ex);
}

Additionally, if you are iterating over 100 elements and that more than one can cause an exception and you want to know which ones, you can catch the exception inside the loop, store the exception in a collection, then iterate to the next element, and at the end, if the collection is not empty, throw an exception with its content.

--
Cédric




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

Reply | Threaded
Open this post in threaded view
|

Re: Test annotation

Niklas Saers
In reply to this post by Cédric Beust ♔
Hi guys,
sorry for being late in my reply.

I know that I can put this in a try/catch clause. My point is just that I believe testing with different sets of test-data is a very common scenario, and just like having to write all those runners with JUnit, I was hoping that this code-duplication could be avoided. If I could just say 'this.testData = "Hello world";' and leave the rest to TestNG, then I would save very many code-lines of copy/paste code for each and every test. Already I can just say "throws SQLException" in my test definition instead of having to try/catch that. I really just want this "throws" handler to be able to display some test-data that I can set within my test. One line to add the testdata to the test should be all right. :-)

Cheers

Nik

On Apr 12, 2007, at 5:47 PM, Cédric Beust ♔ wrote:



On 4/12/07, Alexandru Popescu ☀ <[hidden email]> wrote:

Considering that this is data you are internally handling probably the
only solution for you would be:

/// data here
try {
    // operations
}
catch(Exception ex) {
    Assert.fail("data: " + data, ex);
}

Additionally, if you are iterating over 100 elements and that more than one can cause an exception and you want to know which ones, you can catch the exception inside the loop, store the exception in a collection, then iterate to the next element, and at the end, if the collection is not empty, throw an exception with its content.

--
Cédric




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

Reply | Threaded
Open this post in threaded view
|

Re: Test annotation

Alexandru Popescu ☀
On 4/16/07, Niklas Saers <[hidden email]> wrote:

> Hi guys,
> sorry for being late in my reply.
>
> I know that I can put this in a try/catch clause. My point is just that I
> believe testing with different sets of test-data is a very common scenario,
> and just like having to write all those runners with JUnit, I was hoping
> that this code-duplication could be avoided. If I could just say
> 'this.testData = "Hello world";' and leave the rest to TestNG, then I would
> save very many code-lines of copy/paste code for each and every test.
> Already I can just say "throws SQLException" in my test definition instead
> of having to try/catch that. I really just want this "throws" handler to be
> able to display some test-data that I can set within my test. One line to
> add the testdata to the test should be all right. :-)
>

I see your point, but in your scenario TestNG is not aware of your
data, nor about the exception that can be thrown, so there is not much
it can do. If the exception would be raised from our code (f.e. an
assertion), in that case we would probably be able to provide more
information. However, I will think about it and see if there are any
valid alternatives.

bests,

./alex
--
.w( the_mindstorm )p.
  TestNG co-founder
EclipseTestNG Creator

> Cheers
>
>  Nik
>
>
> On Apr 12, 2007, at 5:47 PM, Cédric Beust ♔ wrote:
>
>
> On 4/12/07, Alexandru Popescu ☀
> <[hidden email]> wrote:
> >
> > Considering that this is data you are internally handling probably the
> > only solution for you would be:
> >
> > /// data here
> > try {
> >     // operations
> > }
> > catch(Exception ex) {
> >     Assert.fail("data: " + data, ex);
> > }
>
> Additionally, if you are iterating over 100 elements and that more than one
> can cause an exception and you want to know which ones, you can catch the
> exception inside the loop, store the exception in a collection, then iterate
> to the next element, and at the end, if the collection is not empty, throw
> an exception with its content.
>
> --
> Cédric
>
>
>
>
>  >
>

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

Reply | Threaded
Open this post in threaded view
|

Re: Test annotation

Niklas Saers
Hi,

I see your point, but in your scenario TestNG is not aware of your
data, nor about the exception that can be thrown

I was under the impression that if a test generates an exception, TestNG captures this. If I could use an instructions like 

this.testdata = "hello world";

and get the handler to be able to report that, that would be splendid.

, so there is not much
it can do. If the exception would be raised from our code (f.e. an
assertion), in that case we would probably be able to provide more
information.

The current asserts could also very well add that. I find myself adding the testdata to every error message, such as just a few minutes ago:

assertEquals("fixCertNumber does not fix as expected for " + testString, fixed, map.getValue());


However, I will think about it and see if there are any
valid alternatives.

Thank you very much. :-)

Cheers

Nik

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

Reply | Threaded
Open this post in threaded view
|

Re: Test annotation

Alexandru Popescu ☀

On 4/19/07, Niklas Saers <[hidden email]> wrote:

>
> Hi,
>
>
> I see your point, but in your scenario TestNG is not aware of your
> data, nor about the exception that can be thrown
>
> I was under the impression that if a test generates an exception, TestNG
> captures this. If I could use an instructions like
>
> this.testdata = "hello world";
>
> and get the handler to be able to report that, that would be splendid.
>

I think you can try implementing the org.testng.ITest interface.

>
> , so there is not much
> it can do. If the exception would be raised from our code (f.e. an
> assertion), in that case we would probably be able to provide more
> information.
>
> The current asserts could also very well add that. I find myself adding the
> testdata to every error message, such as just a few minutes ago:
>
> assertEquals("fixCertNumber does not fix as expected for " + testString,
> fixed, map.getValue());
>

That an Assert extension that you can right and use. I think in your
case it may be very usefull.

>
>
> However, I will think about it and see if there are any
> valid alternatives.
> Thank you very much. :-)
>

bests,

./alex
--
.w( the_mindstorm )p.
  TestNG co-founder
EclipseTestNG Creator

> Cheers
>
>  Nik
>  >
>

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