Passing Object Parameters for TestNG methods?
Here is my situation.
Before my tests are run, in the beforesuite, I instantiate a bunch of "environment objects" These objects get created based on my environment configuration file. It is my tests that will actually be using these environment objects. The problem is how can I pass them to the tests.
Your first impulse might be to use a data provider, which will pass the correct objects to the test. The problem is that the data provider will have to know how to refer to the correct objects and then pass them. As it stands now the environment objects can only be referred to by name (string -> object)
My other option is to pass the test method a string which refers to the correct objects. But this has the same problem as above.
Both of these options create lots of dependencies and problems. I also have to make lots of assumptions about the type the key refers to.
Most of this problem stems from the fact that I abusing TestNG. What i really need is my own controller that can instantiate my Test object while passing the appropriate environment objects.
I was looking into testng's TestRunner interface, but I'm not sure if this would wo开发者_JS百科rk.
Any and all help appreciated. Eric
Why not use a mix of @Factory and @DataProvider?
The factory will be in charge of creating your test instances and passing them the correct parameters: either strings (in which case the data provider can then turn these strings into objects) or directly the objects, in which case your tests can use these values directly.
Feel free to post some code if this doesn't help you solve your problem.
-- Cedric
I had a similar situation and ended up using java properties instead.
You may use the property in your code like this and pass in the variables to your JVM with the -Dmyproperty.value=hello. I used it for stuff like environment path, service url, timeout amount, etc.
精彩评论