Edy Bourne
Edy Bourne

Reputation: 6187

How to pass parameter values directly into TestNG, JUnit or other framework method?

Basically I'm trying to accomplish what in C#'s NUnit is very simple, but using a Java test framework.

What I want to accomplish in Java would look like this in C#'s Nunit:

[Test]
[TestCase(ProcessTypes.A, "a", false, true, false), Category("Smoke")]
[TestCase(ProcessTypes.A, "A", false, false, true), Category("Functional")]
[TestCase(ProcessTypes.A, "*", false, false, false), Category("Functional")]
public void ProcessIsResponding(ProcessTypes ProcessType, string password, bool lengthPass, bool lowercasePass, bool uppercasePass) {
    ...
    // perform test using parameters
    ...
}

In this case, I write a test method and use annotations to define the parameter values, and the values only need to match the test method arguments. Then it is invoked for each TestCase annotation, each time with a different set of parameter values. That is so much more practical than having to annotate the test with parameter names and put value in an external XML file... DataProviders then are extremely cumbersome when you have a large number of tests.

It seems like TestNG was the most promising framework out there, but it has the whole "parameter values in an xml file" architecture, which is awful.

I'm willing to put in the effort to witch frameworks if anyone else would have this kind of structure.

Can this be accomplished with any other test framework for Java out there?

Upvotes: 1

Views: 7451

Answers (2)

piotrek
piotrek

Reputation: 14540

in java annotations you can use only simple types. in junit you can do parameterized testing with zohhak. it let's you write

@TestWith({
    "clerk,      45'000 USD, GOLD",
    "supervisor, 60'000 GBP, PLATINUM"
})
public void canAcceptDebit(Employee employee, Money money, ClientType clientType) {
    assertTrue(   employee.canAcceptDebit(money, clientType)   );
}

i'm not aware if there is something similar in testNG but it's rather easy to implement using custom dataprovider that scans annotations of a current test method

you can also check spock. it let's you write tests in groovy (and run them as junit) therefore you have more powerful syntax

Upvotes: 1

Alexandre Santos
Alexandre Santos

Reputation: 8338

All of the test frameworks you mentioned allow for loading of properties on startup, which can be used instead of parameters. The properties can be read from the command line, from environment vars, from properties files, etc.

TestNG also allows for parameters, such as

import org.testng.annotations.Parameters; import org.testng.annotations.Test;

public class ParameterizedTest1 {
    @Test
    @Parameters("myName")
    public void parameterTest(String myName) {
        System.out.println("Parameterized value is : " + myName);
    }
}

take a look: http://www.tutorialspoint.com/testng/testng_parameterized_test.htm

Upvotes: 1

Related Questions