Use TestNG data provider with Citrus
Posted on June 17th, 2011 by Christoph
TestNG provides brilliant support for test parameters and data providers. With some annotation magic you are able to pass parameter values to your test method and finally to your Citrus test logic.
Actually the TestNG parameters are injected to the Citrus test as normal test variables. Lets put this to the test with a simple example:
public class DataProviderITest extends AbstractTestNGCitrusTest { @Parameters( { "message" } ) @Test(dataProvider = "citrusDataProvider") public void dataProviderITest(ITestContext testContext) { executeTest(testContext); } @Override protected Object[][] getParameterValues() { return new Object[][] { { "Hello World!" }, { "Hallo Welt!" }, { "Hallo Citrus!" }, }; } } |
We have to use the Citrus data provider (citrusDataProvider) along with a named parameter annotation (message) on the test method. Just add the annotations to the Citrus test method as shown in the example above. Next thing we override the method getParameterValues() in order to provide the actual parameter values. As you can see we provide three static values for the “message” parameter.
Inside the Citrus test you can use the test variable ${message} as usual. TestNG and Citrus automatically take care on creating the variable with respective value from data provider. The test case is very simple and looks like follows:
<testcase name="DataProviderITest"> <actions> <echo> <message>${message}</message> </echo> </actions> </testcase> |
As we have three static parameter values in our data provider the whole Citrus test is executed three times. Each time the data provider injects the respective test parameter and ${message} variable. The Citrus test report gives us the test results with all parameters.
echo Hello World!
echo Hallo Welt!
echo Hallo Citrus!
CITRUS TEST RESULTS
DataProviderITest('Hello World!') .................................. SUCCESS
DataProviderITest('Hallo Welt!') ................................... SUCCESS
DataProviderITest('Hallo Citrus!') ................................. SUCCESS
Total number of tests: 3
Skipped: 0 (0.0%)
Failed: 0 (0.0%)
Success: 3 (100.0%) |
We can also use multiple test parameters at a time with all values coming from data provider. How about reading the parameter values from external property files or database?! I use this feature a lot for preparing my Citrus tests with dynamic parameter values from external resources and I bet you will enjoy this feature as much as I do. For more information on TestNG data providers please also have a look at the official documentation (http://testng.org/doc).
Tags: Citrus, testng
Filed under Citrus |
2 Responses to “Use TestNG data provider with Citrus”
-
Cedric Says:
June 17th, 2011 at 19:18Hi,
There are a few problems with your code:
1) You should not use both @Parameter and @DataProvider. Pick one.
2) Your test method should accept a parameter of type String, on top of ITestContext.
As it is, I have the impression that the code you are showing in this article is different from the one you ran.
[Reply]



Christoph Reply:
June 22nd, 2011 at 9:27
@Cedric,
1) I use @Parameters annotation here too, because I need to evaluate the names of test variables for setting those in Citrus test context. I should use a custom Citrus annotation like @TestVariables here to make things more clearly.
2) The code is definitely working as shown without the explicit method parameter of type String. TestNG accepts this, but only if you are working with a single parameter. By the time you use two or more parameters you need to add method parameters as you mentioned. So the code would look like this:
Thanks a lot for your comments they were really helpful for me.
[Reply]