TestNG与构造函数

huangapple go评论75阅读模式
英文:

TestNG with a constructor

问题

以下是您要翻译的内容:

我对TestNG还不熟悉,不知道如果我的类有构造函数的话该如何进行测试。
请帮忙 TestNG与构造函数

所以,这是我的 .java 文件:

public class User {
    static class UserInfo {
        public int age;
        public String address;
        public int points;

        // 构造函数
        UserInformation(String Address, int age, int points) {
            this.age = age;
            this.Address = Address;
            this.points = points;
        }

        public int getAge() {
            return age;
        }

        public String getAddress() {
            return clinicAddress;
        }

        public int getPoints() {
            return points;
        }

        public int addPoints(int newPoints) {
            return points += newPoints;
        }
    }
}

我不知道如何进行测试。我阅读了关于 @DataProvider 的内容,但是我无法准确理解它的工作原理。

不管怎样,谢谢!

英文:

I am new with testNG and don't know how to work to a test if my class has a constructor.
Please Help me TestNG与构造函数

So, this is my .java

public class User {
    static class UserInfo{
        public int age;
        public String address;
        public int points;
        //Constructor
        UserInformation(String Address, int age, int points){
            this.age = age;
            this.Address = Address;
            this.points = points;
        }
    public int getAge(){return age;}
    public String getAddress(){return clinicAddress;}
    public int getPoints(){return points;}

    public int addPoints(int newPoints){
        return points += newPoints;
    }
}

I don't know how to do the test. I read about @DataProvider but I can't understant exactly how it works

Thanks either way!

答案1

得分: 0

你不需要对构造函数进行单元测试,因为它只是设置实例变量。编写单元测试是为了测试你的业务逻辑。

另外一点要注意的是,你应该将实例变量设为私有,并且只允许通过getter方法访问。

关于@DataProvider,当你的整个单元测试具有相同的代码行,只是输入或输出不同的时候,你可以使用它。在这种情况下,你可以将数据传递给数据提供者,并且可以通过使用不同的数据集来执行相同的测试方法,从而避免重复的单元测试代码。

对于你的addPoints()方法,我们可以使用@DataProvider编写测试用例,如下所示:

@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "Test with zero", 0 },
   { "Test with negative value", -1},
   { "Test with positive value", 1},
 };

然后你可以在测试方法上使用dataProvider,就像下面这样:

@Test(dataProvider = "test1")
public void verifyData1(String testCase, int number) {
   // 调用要测试的方法,并传入输入的数字
}

更多关于DataProvider的详细信息,请阅读https://testng.org/doc/documentation-main.html#parameters-dataproviders。

英文:

You don't need to unit test Constructor as it is just setting instance variables. You write unit tests to test your business logic.

On a side note, you should make your instance variables private and only allow access through getters.

And regarding @DataProvider, you use it when your whole unit test has the same lines of code but just differ in terms of input or output. In that case, you can pass data to your data provider and can run multiple test cases by executing the same test method with a different set of data and can avoid duplication of unit test code.

For your addPoints() method,we can write test cases with @DataProvider as below :

@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "Test with zero", 0 },
   { "Test with negative value", -1},
   { "Test with positive value", 1},
 }

And then you use dataProvider on your test method like below :

@Test(dataProvider = "test1")
public void verifyData1(String testCase, int number) {
   //call your method to being tested with input number
}

For more detail on DataProvider,read https://testng.org/doc/documentation-main.html#parameters-dataproviders

huangapple
  • 本文由 发表于 2020年10月8日 10:22:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64254888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定