我使用的是JTest 9.5。我想问,是否有可能在每个单元测试之前执行相同的准备例程,就像我在JUnit中使用@Before和@After注释一样?如果是,那怎么做?假设我在JTest中有以下单元测试类:
public class TestArrayFileManager extends PackageTestCase {
FileManager fileMngr;
public TestArrayFileManager(String name)
{
super(name);
}
public Class getTestedClass()
{
return FileManager.class;
}
public void testFileManager1() throws Throwable
{
final String fileName = "InputFile.txt";
fileMngr = new FileManager(fileName);
fileMngr.doResetFile();
fileMngr.doReplaceNthElement(0, 3);
fileMngr.doReplaceNthElement(1, 9);
assertEquals(3, fileMngr.doReadNthElement(0L));
}
public void testFileManager2() throws Throwable
{
final String fileName = "InputFile.txt";
fileMngr = new FileManager(fileName);
fileMngr.doResetFile();
fileMngr.doReplaceNthElement(0, 3);
fileMngr.doReplaceNthElement(1, 9);
assertEquals(9, fileMngr.doReadNthElement(1L));
}
}注意我是如何重复相同的准备代码的。我怎样才能在每次考试之前完成它?
发布于 2013-11-20 08:05:14
为了在每个单元测试之前完成准备和结束任务,我只需要找出我必须添加的方法。这些是我在生成的单元测试文件中找到的方法,它们正在工作:
public void setUp() throws Exception {
super.setUp();
/*
* Add any necessary initialization code here (e.g., open a socket).
* Call Repository.putTemporary() to provide initialized instances of
* objects to be used when testing.
*/
// jtest.Repository.putTemporary("name", object);
}
/**
* Used to clean up after the test. This method is called by JUnit after
* each of the tests have been completed.
*
* @see junit.framework.TestCase#tearDown()
* @author Parasoft Jtest 9.5
*/
public void tearDown() throws Exception {
try {
/*
* Add any necessary cleanup code here (e.g., close a socket).
*/
} finally {
super.tearDown();
}
}发布于 2013-11-20 07:59:29
JTest对JUnit的补充和扩展,这意味着它没有提供JUnit的特性。为了实现您想要的结果,您必须在Junit中使用JTest。
您可以使用与JUnit一起使用现有的JTest测试用例来提供setUp和tearDown方法,使用@Before和@After注释。
如果要在Jtest中使用JUnit测试类,则需要:
执行这些步骤后,Jtest将在以正常方式运行测试时使用JUnit测试类。
https://stackoverflow.com/questions/20090190
复制相似问题