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

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

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++;

}

但是在几个项目上运行代码后,我意识到找到包含Unit或EasyMock或Mockito的测试类的想法并不是查找测试类的最佳实践,因为有几个项目使用自己的测试方法!那么问题是有一个比我更好的方法来定义测试类吗?

谢谢

最佳答案 不能你只是

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值