无法使用 JUnit 和 Eclipse 运行单个单元测试以进行数据驱动测试
为了清楚起见,我已经将问题从所有不必要的复杂性中剥离出来,并附加了两个文件。实际上,我想从数据库加载测试所需的输入。在示例中,我在 Suites
类中使用了 suites 映射,而不是查询的结果。我还在 TestOverride
的 run 方法中进行了相当复杂的比较,而不是简单的比较。基本上,这就是我如何以动态方式解决使用数据库(套件映射)中的测试创建测试套件的问题。另外,当我用 eclipse 运行它时,我可以看到测试名称,这一点很重要。
如果您运行 Suites
(只需右键单击它并在 eclipse 上运行 JUnit),它就可以正常工作。唯一通过的测试是 test4。但是,我希望能够使用这种类型的构造运行单个测试(单个套件也很好,但我会对单个测试感到满意)。换句话说,运行所有套件后,我想转到 JUnit 窗口,右键单击单个测试并运行它。如果我这样做,那是行不通的。我不知何故认为测试在第一次运行后存储在某个地方,并且我可以稍后使用它们。
我正在使用 eclipse 3.6 和 JUnit 4.0
有什么想法吗?我不对参数化类使用注释,因为所有内容都必须在编译前已知(并且我从数据库获取输入)。我还在论坛中看到,用这种方法重命名测试用例是一个很大的问题。
import java.util.HashMap;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestSuite;
public class Suites {
public static Test suite() {
Map<String, String[]> suites = new HashMap<String, String[]>();
suites.put("suite1", new String[]{"test1", "test2"});
suites.put("suite2", new String[]{"test3", "test4"});
TestSuite all = new TestSuite("All Suites");
for(Map.Entry<String, String[]> entry : suites.entrySet()) {
TestSuite suite = new TestSuite(entry.getKey());
for(String testName : entry.getValue()) {
suite.addTest(
new TestOverride(
testName
)
);
}
all.addTest(suite);
}
return all;
}
}
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import junit.framework.TestResult;
public class TestOverride extends TestCase {
private String name;
public TestOverride(
String name)
{
this.name = name;
}
@Override
public void run(TestResult result) {
result.startTest(this);
if (this.name.equals("test4")) {
result.endTest(this);
} else {
result.addFailure(this, new AssertionFailedError("Not test4"));
}
}
@Override
public String getName() {
return name;
}
}
I've stripped the problem from all unnecessary complexity and attached two files for clarity's sake. In actuality, I want to load the required input for testing from a database. In the example I have the suites map in the Suites
class, instead of the result from the query. I also have a rather complex comparison instead of the simple one in the run method of TestOverride
. Basically that's how I solved creating test suites with their tests from the database (suites map) in a dynamic way. In addition, it is important that I can see the test name when I run it with eclipse.
If you run Suites
(just right click on it and JUnit-run on eclipse) it works fine. The only test that passes is test4. However, I would like to be able to run a single test with this type of construct (a single suite would be nice as well, but I would be happy with a single test). In other words, after running all suites, I would like to go to the JUnit window, right click on a single test and run it. If I do it it doesn't work. I somehow thought the tests were stored somewhere after the first run and that I could use them later.
I am using eclipse 3.6 and JUnit 4.0
Any ideas? I don't use annotations for parametrized classes because everything has to be known before compile time (and I take the input from a database). I've also seen in the forums that it's quite a problem renaming the test cases with that approach.
import java.util.HashMap;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestSuite;
public class Suites {
public static Test suite() {
Map<String, String[]> suites = new HashMap<String, String[]>();
suites.put("suite1", new String[]{"test1", "test2"});
suites.put("suite2", new String[]{"test3", "test4"});
TestSuite all = new TestSuite("All Suites");
for(Map.Entry<String, String[]> entry : suites.entrySet()) {
TestSuite suite = new TestSuite(entry.getKey());
for(String testName : entry.getValue()) {
suite.addTest(
new TestOverride(
testName
)
);
}
all.addTest(suite);
}
return all;
}
}
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import junit.framework.TestResult;
public class TestOverride extends TestCase {
private String name;
public TestOverride(
String name)
{
this.name = name;
}
@Override
public void run(TestResult result) {
result.startTest(this);
if (this.name.equals("test4")) {
result.endTest(this);
} else {
result.addFailure(this, new AssertionFailedError("Not test4"));
}
}
@Override
public String getName() {
return name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为不可能实现你想要的目标。据我所知(和经验),只有“真正的”junit 方法(即现有类中的实际方法)可以从 junit 窗口执行(这在使用参数化测试时很容易重现。特定的测试无法运行这里也再次)。
也许您应该尝试生成用于测试的 java 代码(并编译它)。
I don't think it is possible to achieve what you'd like. As far as I know (and experienced) only "real" junit-methods (that are actual methods in existing classes) can be executed from the junit window (this is easily reproduced when using parameterized tests. The specific Tests can't be run here again either).
Perhaps you should try to generate the java code for the tests(and compile it).
如果您重写
runTest()
并从TestCase
类创建TestSuite
,就会容易得多。这是一个有效的示例:
http://mrlalonde.blogspot.ca/2012/08 /data-driven-tests-with-junit.html
It's much easier if you override
runTest()
and create theTestSuite
from yourTestCase
class.Here is an example that works:
http://mrlalonde.blogspot.ca/2012/08/data-driven-tests-with-junit.html