我想通过将它们放置在外部资源中来防止对每个测试重复执行BeforeClass,AfterClass和Rule。但是该规则并没有生效。
我有大量的测试(超过1000个测试)分布在多个类别(大约60个)中。我想添加将失败案例的信息(输入,期望值和实际值)保存到csv文件的功能。因此,我定义了一个BeforeClass,在其中将csv文件的第一行添加到列表中。在AfterClass中,我将保存csv行的列表内容写入文件。覆盖失败方法的规则将在每次测试失败时将测试信息添加到列表中。为了能够将每个失败信息传递给覆盖的失败方法,我创建了一系列变量和一个setter方法,可以通过测试访问这些方法。但是,该规则无效或不可访问。请查看代码:
这是我用作ExternalResource的类
public class TestConfs extends ExternalResource {
private static Logger log = LoggerFactory.getLogger(TestConfs.class);
private static int fActual = -1;
private static List<String> failedTests = new ArrayList<String>();
private static int fInput = -1;
private static int fExpected = -1;
private static String fTest = new String();
// This method sets the viaralbes to be stored in the failedTests list.
public void set_config_variables(int input_value, int expected_value, int actual_value, String caller){
fActual = actual_value ;
fInput = input_value ;
fExpected = expected_value ;
fTest = caller ;
}
// The beforeClass just writes the first row of the failedTests list.
@BeforeClass
public static void beforeClass() {
failedTests.add("input,expected,actual,test");
}
// The AfterClass writes the content of the failedTests to a csv file if there are any.
@AfterClass
public static void afterClass() {
// This if clause prevents craetion of any file if there are no failed tests.
if (failedTests.size() > 1) {
try {
File f = new File("./src/exclusions/TestFailures/" + fTest + "_FailedCases.csv");
f.getParentFile().mkdirs();
FileWriter writer = new FileWriter(f);
for (String str : failedTests) {
writer.write(str + "\n");
}
writer.close();
} catch (IOException e) {
log.info("An error happened while writing the failed cases to file: ", e);
}
}
}
// In case there are any failures, this rule writes the content of the vairables to the failedTests list.
@Rule
public TestWatcher watchMe = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
failedTests
.add(String.format("%s,%s,%s", fInput, fExpected, fActual));
}
};
}
这是一个包含2个测试用例的示例测试。
@RunWith(Theories.class)
public class TestTest {
// The data point passes a bunch of integers to the test.
@DataPoints("positions")
public static List<Integer> positionsList = TestSignals.xPositions;
// This is how I am trying to implement the rules I have also tried doing this:
// @ClassRule
// public static final ExternalResource tres = new TestConfs();
// but if I do that I cannot access set_conf_vars()
@ClassRule
public static final TestConfs tres = new TestConfs();
@Theory
public void testDeviceDispositionXStep1(@FromDataPoints("positions") int xP) {
int disposition = stepWise.move_x(xP);
int expectedValue = disposition.getExpectedPosition();
int actualValue = disposition.findCurrentPosition().get_x();
tres.set_conf_vars(disposition, expectedValue, actualValue, this.getClass().getSimpleName());
assertEquals(expectedValue, actualValue);
}
@Theory
public void testDeviceDispositionXMetric1(@FromDataPoints("positions") int xP) {
int disposition = metricWise.move_x(xP);
int expectedValue = disposition.getExpectedPosition();
int actualValue = disposition.findCurrentPosition().get_x();
tres.set_conf_vars(disposition, expectedValue, actualValue, this.getClass().getSimpleName());
assertEquals(expectedValue, actualValue);
}
}
我期望的是先运行BeforeClass和AfterClass,然后该规则生效,导致将错误写入文件。如果我将它们全部与测试放在一个文件中,则确实会发生这种情况,但是,我大约有60个文件,这并不理想。当我在单独的文件中使用外部资源时,规则要么不执行任何操作,要么不可访问。请查看@ClassRule之前的注释行,以查看何时无法访问它。我阅读了几乎所有关于如何使用外部规则的文章和文章,但没有人谈论如何重写外部规则失败的内容。我们非常感谢您的协助。