[英]JUnit parameterized Tests Getting the parameter after the test
So here's my dilemma. 所以这是我的难题。 I have been using Selenium, TestNG, and iText to generate nice PDF reports from the results of an automated Test run, but I was told recently that they didn't want competing libraries TestNG vs. JUnit, and was told to start using JUnit instead. 我一直在使用Selenium,TestNG和iText从自动测试运行的结果中生成漂亮的PDF报告,但是最近有人告诉我他们不想要竞争的库TestNG vs. JUnit,并被告知开始使用JUnit。 。
I am running these JUnit tests with parameters, and am wondering is there a way to access the parameter after/during the test run? 我正在使用参数运行这些JUnit测试,并且想知道是否可以在测试运行之后/期间访问参数? The parameters are Strings with browser names which is used to tell selenium which WebDriver to get, and it would be nice to know that a test passed/failed in a certain browser. 参数是带有浏览器名称的字符串,用于告诉selenium要获取哪个WebDriver,并且很高兴知道测试在某个浏览器中通过/失败。 JUnit seems to be very limited in the information you can access once a test run completes. 测试运行完成后,您可以访问JUnit的信息似乎非常有限。
I have a class which extends junit.framework.TestListener
which listens for the start/stop of each test, and here is where I can gather information about the test. 我有一个扩展junit.framework.TestListener
的类,它侦听每个测试的开始/停止,在这里我可以收集有关测试的信息。
currentTest
is of type BaseTestResult
which is a class I wrote that simply stores test results in a list. currentTest
的类型为BaseTestResult
,这是我编写的仅将测试结果存储在列表中的类。
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestListener;
import junit.framework.TestResult;
import utilities.reporting.BaseReporting;
import utilities.reporting.BaseTestResult;
import utilities.reporting.ResultsPerSuite;
public class BaseListener implements TestListener {
private ResultsPerSuite resultsPerSuite;
private BaseReporting baseReporter;
private BaseTestResult currentSuite;
private BaseTestResult currentTest;
private long startTime;
private long endTime;
private long suiteStartTime;
private long suiteEndTime;
public BaseListener() {
baseReporter = new BaseReporting();
resultsPerSuite = new ResultsPerSuite();
currentTest = new BaseTestResult(null, null);
}
public void startSuite(Test suite) {
suiteStartTime = System.currentTimeMillis();
currentSuite = new BaseTestResult(suite);
}
@Override
public void startTest(Test arg0) {
startTime = System.currentTimeMillis();
currentTest = new BaseTestResult(arg0);
}
@Override
public void addError(Test arg0, Throwable arg1) {
currentTest.addError(new BaseTestResult(arg0, arg1));
}
@Override
public void addFailure(Test arg0, AssertionFailedError arg1) {
currentTest.addFailed(new BaseTestResult(arg0, arg1));
}
@Override
public void endTest(Test arg0) {
endTime = System.currentTimeMillis();
currentTest.setRuntime(startTime - endTime);
// If both empty, then test passed, so add to passed results.
if (currentTest.getFailed().isEmpty()
&& currentTest.getErrors().isEmpty()) {
resultsPerSuite.addPassed(currentTest);
} else {
resultsPerSuite.addFailed(currentTest);
}
}
public void endSuite(TestResult testEventDriver) {
suiteEndTime = System.currentTimeMillis();
currentSuite.setRuntime(suiteEndTime - suiteStartTime);
resultsPerSuite.setSuite(currentSuite);
baseReporter.printToConsole(resultsPerSuite);
}
/**
* @return the allTestResults
*/
public ResultsPerSuite getAllTestResults() {
return resultsPerSuite;
}
}
I am not sure how to get it to do what you are looking for trivially from inside of the TestListener
without creating your own Runner
. 我不确定如何在不创建自己的Runner
情况下从TestListener
内部简单地完成它的工作。 But you might be able to handle it with a TestRule
. 但是您可以使用TestRule
处理它。
Since you appear to be using the listener to send the results to an external service, you might have better luck specifically by rigging in a TestWatcher
that communicates with your own listener. 由于您似乎正在使用侦听器将结果发送到外部服务,因此,特别是通过装配与您自己的侦听器进行通信的TestWatcher
,可能会更好。 It would have access to the local class member variables and could report on them fairly easily. 它可以访问本地类成员变量,并且可以很容易地报告它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.