我试图使用XUnit.net作为自定义本土测试调度程序的替代品.本地调度程序的一个特性是,对于长时间运行的测试,它会在测试通过后立即将测试结果(通过/失败以及导致失败的异常)输出到数据库失败.
因为可能存在大量长时间运行的测试,所以在运行过程中查看测试进度非常有用(在完成所有测试后,不必等待完整的测试通过,直到看到所有结果为止可能需要时间).
XUnit.net源代码来自:https://github.com/xunit/xunit
我看了一眼并看到了BeforeAfterTestAttribute,但是"After"方法并没有提供对测试结果的访问,只是对测试方法的访问.我想要类似的东西,但也可以访问测试结果,这样我就可以立即向数据库报告结果(而不是等待完整的测试套件完成).
似乎(从源代码)可以访问实际测试结果的唯一东西是TestRunner,但据我所知,测试运行器没有可扩展性模型.
我提出的一个可能的解决方法如下:
[Fact]
TestMethod()
{
//This method takes a lambda, handles exceptions, uploads to my
//database, and then rethrows.
RunTestWithExtraLogging(() =>
{
//Actual test goes here
}
}
Run Code Online (Sandbox Code Playgroud)
上述解决方案并不理想,因为它要求每个测试的作者调用"RunTestWithExtraLogging"方法.
PS:我愿意考虑一个不同的测试框架(xUnit.net除外),如果它支持这个...
我想实现一个类似于以下内容的泛型方法:
private <T> void addToSize(ArrayList<T> list, Class<T> type, int size) {
int currentSize = list.size();
for(int i = currentSize; i < size; i++) {
try {
list.add(type.newInstance());
} catch (InstantiationException e) {
logger.error("", e);
} catch (IllegalAccessException e) {
logger.error("", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的方法适用于这样的事情:
ArrayList<Integer> test = new ArrayList<Integer>();
addToSize(test, Integer.class, 10);
Run Code Online (Sandbox Code Playgroud)
但我也想让它为...工作
ArrayList<ArrayList<Integer>> test = new ArrayList<ArrayList<Integer>>();
addToSize(test, ArrayList.class, 10); //Is this possible?
Run Code Online (Sandbox Code Playgroud)
这可能吗?