Re: [testng-users] Nested @Factory annotations

177 views
Skip to first unread message

Krishnan Mahadevan

unread,
Dec 2, 2012, 8:42:37 PM12/2/12
to testng...@googlegroups.com
From your explanation it looks like you have a bunch of objects (each having another factory drive its instances). 

Do you have a sample that can be used to recreate the problem ?

Ideally spaeaking if you did have a static Factory create Test Class instances and a @Factory annotated constructor on the Test Class, then there is no way what you describe should happen. 


But am confused as to why would you need to take this route of Nesting factories. A factory with data provider is still going to run multiple instances of the same test method but with different data. Why would you need another factory here? Whats the dependency on u having to use it. 

On Friday, November 30, 2012, Santhosh L wrote:
I have a scenario where @Factory annotation is used to run multiple instances/threads of the same test.

Simply put, you have a test Test001, and you want to run it in 2 parallel threads, each thread having its own set of params. So we have a method which will return Object[] having 2 instances of the test with different input. We are running this in parallel programatically (creating a xml suite and using testng.run()). This works fine for normal tests. However, this gets complicated in case of DataDriven tests.

@Factory
@Parameters({"noOfInstances"})
public Object[] createInstances(int noOfInstances) {
...
}

We have a Data driven framework where we create test objects from the data file again using @Factory.

@Factory
@Parameters({ "dataFile" })
public Object[] getTests(@Optional("") String dataFile) {
...
}

What I see is when I try to run multiple instances of a DD test, I find unnecessary repetition and failures.

Suppose I run 1 instance of DD test TestDD1, and data in file has input data for tests 1, 2
Expectation: Run
TestDD1-1
TestDD1-2

Actual: Ran
TestDD1
TestDD1-1
TestDD1-2

And TestDD1 fails while the other 2 pass.

Similarly, if I run 2 instances/threads of DD test TestDD1,

Expectation: Run (|| denotes in parallel)
TestDD1-1 || TestDD1-1
TestDD1-2 || TestDD1-2

Actual: Ran
TestDD1    || TestDD1
TestDD1-1 || TestDD1-1
TestDD1-2 II TestDD1-2


What changes can I make to achieve what I need? Is it possible to remove one of the @Factory annotations and modify code in the manner to achieve my requirement? Or, can I intercept the test objects returned by Factory and exclude ones I don't need? Is there any other TestNG functionality that I can use?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/x_1OZJiQZ24J.
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.


--
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/

Santhosh L

unread,
Dec 3, 2012, 2:40:58 PM12/3/12
to testng...@googlegroups.com
Thanks for that response Krishnan.
To answer your question, my preference is not to have multiple factories. However, we are using a test executor tool developed within our company. This tool uses @Factory annotation (outer one) to run test instances in parallel , code for which I cannot change. The need basically boils down to having different parameters to different test instances. This works well for non-DD tests. However, when we attempt to run DD tests that have its own @Factory annotations we run into this issue.

There is one correction in to my original description. Let me add that -

Suppose I run 1 instance of DD test TestDD1, and data in file has input data for tests 1, 2
Expectation: Run
TestDD1-1
TestDD1-2

Actual: Ran
TestDD1
TestDD1-1
TestDD1-2

And TestDD1 fails while the other 2 pass.

Similarly, if I run 2 instances/threads of DD test TestDD1,

Expectation: Run (|| denotes in parallel)
TestDD1-1 || TestDD1-1
TestDD1-2 || TestDD1-2

Actual: Ran
TestDD1    || TestDD1 || TestDD1-1 |I TestDD1-2
To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Santhosh L

unread,
Mar 13, 2013, 8:37:59 PM3/13/13
to testng...@googlegroups.com
Any suggestions or updates to this?
Reply all
Reply to author
Forward
0 new messages