[英]Set up JUnit test database
void insertAllRecords(){
insertRecord("tom", 100);
insertRecord("jake", 200);
insertRecord("tim", 300);
insertRecord("andy", 400);
insertRecord("mike", 500);
}
I am writing JUnit
test for something that has a test database. 我正在为具有测试数据库的东西编写
JUnit
测试。 It starts as an empty database. 它从一个空数据库开始。 I want to test different things, but I don't want to have duplicated
insertRecord
with same params. 我想测试不同的东西,但是我不想使用相同的参数复制
insertRecord
。
Is there a simple way to specify which line(s) to execute? 是否有一种简单的方法来指定要执行的行? For example, I only want to add tom and andy in one of the
@test
function. 例如,我只想在
@test
函数之一中添加tom和andy。
I KNOW IN EACH TEST METHOD, THE DATABASE STARTS AS 0. I want to be able to: 我知道每种测试方法中的数据库都从0开始。我希望能够:
But I don't want to put individual insert statement in every test. 但是我不想在每个测试中都放置单独的插入语句。 I want to be able to call insertAllRecords with some parameters.
我希望能够使用一些参数来调用insertAllRecords。
Thanks 谢谢
As far as I know, JUnit does not support the behaviour that you want. 据我所知,JUnit不支持您想要的行为。 You might want to take a look at TestNG which allows test specific setup with
@BeforeMethod
and @AfterMethod
annotations. 您可能想看看TestNG ,它允许使用
@BeforeMethod
和@AfterMethod
批注进行特定于测试的设置。 And it also allow more flexible test data using parameterized data with @Parameter
or @DataProvider
annotations. 此外,它还可以使用带有
@Parameter
或@DataProvider
批注的参数化数据来提供更灵活的测试数据。
convert your Junit Class to an abstract class with the abstract method abstract void insertAllRecords();
使用抽象方法将您的Junit类转换为抽象类
abstract void insertAllRecords();
Then extend this class in the cases, you need. 然后在需要的情况下扩展此类。 For example
例如
class TestWithTomClass extends AbstractTestClass{
public void insertAllRecords(){
insertRecord("tom", 100);
}
@Test
public void myTestWithTom(){
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.