gpt4 book ai didi

java - 如何识别项目中的测试类

转载 作者:搜寻专家 更新时间:2023-11-01 03:34:21 25 4
gpt4 key购买 nike

我正在使用以下代码来识别项目中的测试类,整个思路是找到测试类并将测试代码量与生产代码进行比较!这是我的一段代码,负责查找测试类并计算行数:

     for (File f : list) {
if (f.isDirectory()) {
walk(f.getAbsolutePath());
}

if (f.getName().endsWith(".java")) {

System.out.println("File:" + f.getName());
countFiles++;

Scanner testScanner = new Scanner(f);
while (testScanner.hasNextLine()) {

String test = testScanner.nextLine();
if (test.contains("org.junit") || test.contains("org.mockito") || test.contains("org.easymock")) {
hasTestLines = true;
// break;
}
testCounter++;
}

但是在几个项目上运行代码后,我意识到寻找包含 UnitEasyMockMockito 的测试类的想法是寻找测试类不是最好的做法,因为有几个项目使用自己的测试方法!所以问题是有没有比我更好的方法来定义测试类?

谢谢

最佳答案

难道你不能 load classes并将它们作为参数传递像这样的测试运行器

org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);

并处理测试运行器的输出。

现在测试运行器在类中搜索测试方法。

如果该类不包含任何内容,它将不会成功,因此是生产类。 (假设所有测试类都成功!)

在我的实现中,我只计算类的数量,但是你可以扩展它来计算行数,然后比较它们。

这里是实现:

 public void compareTestAndProduction () {

// pattern to split the name of class from it's extension
String pattern = "(.*)(?=.class)";

// package to proove
String packageName = "stackoverflow.test";

// relative path of package
String packagePath = "stackoverflow/test";

// counter for number of test classes
int testCounter = 0;

// counter for number of production classes
int codeCounter = 0;

// classloader for test and production classes
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

try {
// load package resources to file
Enumeration enumeration = classLoader.getResources(packagePath);
URL url = (URL) enumeration.nextElement();
File classFiles = new File(url.getFile());

// read all subfiles in File
// which contains the package dir and all classes
for (File classFile : classFiles.listFiles()) {
String classNameWithExtension = classFile.getName();
// proov if name of class is no directory
if (classNameWithExtension.endsWith(".class")) {
// extend the class with the package name
// and get rid of the extension .class
String className = packageName + "." + classNameWithExtension.split("[.]")[0];

// load class
Class c = classLoader.loadClass(className);

// run the class with a test runnner
// which will search class for test methods
Result result = org.junit.runner.JUnitCore.runClasses(c.newInstance().getClass());

// if testmethods found
// and they are successful
// raise testcounter
if(result.wasSuccessful())
testCounter++;
else codeCounter++;


}
}
System.out.println("Test classes:\n" + testCounter);
System.out.println("Production classes:\n" + codeCounter);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

以我为例

Test classes:

1

Production classes:

2

关于java - 如何识别项目中的测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35897865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com