英文:
TestNG with a constructor
问题
以下是您要翻译的内容:
我对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
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论