当前位置: 首页 > 知识库问答 >
问题:

如何识别项目中的测试类

何骞尧
2023-03-14

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

     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的测试类的想法并不是寻找测试类的最佳实践,因为几个项目使用自己的测试方法!所以问题是有比我更好的方法来定义测试类吗?

谢啦

共有3个答案

竺展
2023-03-14

不确定您想要实现什么,但测试和实际代码应该分开。你可以使用emma、cobertura、sonar等插件来覆盖代码。

然而,如果你正在做一些运动,那么请看以下选项。

测试类可以有一个测试前缀或后缀。

测试类实现/扩展了一个接口,例如TestCase。

测试类在顶部使用注释,例如@TestCase。

测试类具有注释的方法,例如@Test。

斜淳
2023-03-14

在java中,通常生产代码保存在src/main/java中,测试代码保存在src/test/java中

史默
2023-03-14

难道你不能直接加载类并把它们作为参数传递给像这样的测试运行程序吗

组织。朱尼特。跑步者JUnitCore。运行类(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();
    }
}

就我而言

测试课程:

1.

生产类:

2.

 类似资料:
  • 我对maven和testng完全陌生。我使用maven作为构建工具,并将testng作为我的测试框架。我没有遵循标准的maven项目结构。现在我希望我的在我的项目中执行测试用例。问题是,如何知道要考虑执行哪些测试用例?

  • 问题内容: 该命令仅覆盖一个目录中的文件。 我想要整个项目,这意味着测试应覆盖dir中的所有文件以及该dir下的所有千岁树dir 。 这样做的命令是什么? 问题答案: 这应该在当前目录及其所有子目录中运行所有测试: 这应该针对给定的特定目录运行所有测试: 这应该以前缀为的导入路径运行所有测试: 这应该运行所有带有前缀的测试导入路径: 这应该在$ GOPATH中运行所有测试:

  • 问题内容: 我对Maven和Testng完全陌生。我正在使用maven作为构建工具,并使用testng作为我的测试框架。我没有遵循标准的Maven项目结构。现在,我要在项目中执行测试用例。问题是,如何知道要考虑执行哪些测试用例? 问题答案: 如果将其放在一个位置,则需要设置maven-surefire-plugin配置的参数: 所有这些在Maven Surefire插件文档中都有详细记录

  • 我正在创建一个项目,使用composer获取所有依赖项。我也使用phpunit,我刚刚将我的项目迁移到phpunit 6.0。在此更改之前,我的phpunit始终工作正常。 在此之前,我使用的是版本。 我目前需要我的如下所示: 我让我的所有测试扩展类,而不是类。 我的配置保持不变,如下所示: 我的目录结构如下所示: 在我的项目根目录中打开终端并输入命令时,它会给出以下消息: PHPUnit 3.7

  • testng.xml TestNG将扫描所有类路径以寻找测试类(PackageUtils#findClassesInPackage)。 不需要的测试来自依赖项。 如何配置TestNG以只在maven项目测试目录中查找测试类(默认编译为)?

  • 从这里引用:https://www.ampproject.org/docs/get_started/about-amp.html AMP是一种为快速呈现的静态内容构建网页的方法。运行中的放大器由三个不同部分组成: AMP超文本标记语言 AMP JS AMP CDN AMP HTML是一种HTML,它对可靠的性能有一些限制,并对在基本HTML之外构建丰富内容有一些扩展。AMP JS库确保AMP HT