侧扫声呐的应用_适用于多模块android应用的声纳

侧扫声呐的应用

Writing test cases is an essential part of the development cycle. It helps to detect potential bugs early in the lifecycle. Critical code reviews are necessary to ensure proper code practices and observe code smells. Code coverage gives us a good idea about how much code is covered by the test cases. SonarQube report informs all of the above.

编写测试用例是开发周期的重要组成部分。 它有助于在生命周期的早期发现潜在的错误。 重要的代码审查对于确保正确的代码惯例和观察代码气味是必要的。 代码覆盖率让我们很好地了解了测试用例覆盖了多少代码。 SonarQube报告告知以上所有内容。

It is quite straight forward to get coverage of a single module Application. But, most of the enterprise Android Applications are divided into submodules. Let’s see how can we get the combined code coverage of an entire project consisting of various submodules.

获得单个模块应用程序的覆盖范围非常简单。 但是,大多数企业Android应用程序都分为子模块。 让我们看看如何获​​得由各种子模块组成的整个项目的组合代码覆盖率。

Image for post
Multi-module Project Structure
多模块项目结构

Here, similar to app/build.gradle, every submodule has build.gradle. e.g., features/feature1/build.gradle. app module calls methods in submodules.

在这里,类似于app / build.gradle ,每个子模块都有build.gradle 。 例如features / feature1 / build.gradle。 应用模块调用子模块中的方法。

Each submodule has the below hierarchy.

每个子模块具有以下层次结构。

Image for post
Structure of the submodule
子模块的结构

In build.gradle file, add below Gradle task. Task will be of type JacocoReport. This Gradle task will be responsible for generating test execution data, which in turn will generate .exec file.

build.gradle文件中,在Gradle任务下面添加。 任务将为JacocoReport类型 该Gradle任务将负责生成测试执行数据,而测试执行数据又将生成.exec文件。

apply plugin: 'kotlin-kapt'
apply plugin: 'jacoco'


task codeCoverageReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug")
    def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug")
    sourceDirectories = files('src/main/kotlin')
    executionData = files("${buildDir}/jacoco/testDebugUnitTest.exec")
    classDirectories = files([debugTree], [kotlinDebugTree])
}

Each submodule’s build.gradle will have this task in it. .exec file for each submodule will be stored in its respective jacoco/ folder. Now, we have the test execution data for all the modules.We need to add up all test execution data and generate combined code coverage. For that, we need to make changes in parent level build.gradle. Let’s modify parent level build.gradle.

每个子模块的build.gradle中都将包含此任务。 每个子模块的.exec文件将存储在其各自的jacoco /文件夹中。 现在,我们有了所有模块的测试执行数据,我们需要将所有测试执行数据加起来并生成合并的代码覆盖率。 为此,我们需要在父级build.gradle中进行更改。 让我们修改父级build.gradle

apply plugin: 'jacoco'
apply plugin: "org.sonarqube"


dependencies {
 classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:$versions.test.sonargradle"
 classpath "org.jacoco:org.jacoco.core:$versions.test.jacoco"
}


task rootCodeCoverageReport(type: JacocoReport, dependsOn: ['features:feature1:codeCoverageReport',
                                                            'features:feature2:codeCoverageReport',
                                                            'features:feature3:codeCoverageReport'
]) {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }
    sourceDirectories = files(coverageMainSourceDirs)
    classDirectories = files(coverageClassDirectories, coverageKotlinClassDirectories)
    executionData = files(["features/feature1/build/jacoco/testDebugUnitTest.exec",
                           "features/feature2/build/jacoco/testDebugUnitTest.exec",
                           "features/feature3/build/jacoco/testDebugUnitTest.exec"
])
}


def coverageMainSourceDirs = ['features/feature1/src/main/kotlin',
                              'features/feature2/src/main/kotlin',
                              'features/feature3/src/main/kotlin'
]


def coverageKotlinClassDirectories = [fileTree(dir: 'features/feature1/build/tmp/kotlin-classes/debug'),
                                      fileTree(dir: 'features/feature2/build/tmp/kotlin-classes/debug'),
                                      fileTree(dir: 'features/feature3/build/tmp/kotlin-classes/debug')
]


def coverageClassDirectories = [fileTree(dir: 'features/feature1/build/intermediates/classes/debug'),
                                fileTree(dir: 'features/feature2/build/intermediates/classes/debug'),
                                fileTree(dir: 'features/feature3/build/intermediates/classes/debug')
]


task getCoverageLocal(type: Exec, dependsOn: 'rootCodeCoverageReport') {
    group = "Reporting"
    commandLine "open", "${project.buildDir}/features/${project.name}/build/reports/jacoco/codeCoverageReport/html/index.html"
}

Here, we are adding a task rootCodeCoverageReport, which depends on codeCoverageReport tasks from submodules. This task will extract all the values of kotlinClasses and sourceDirectories from their submodules. Importantly, test execution data of submodules will be fetched from /jacoco paths of their submodules. Now, we have combined the code coverage data from all the submodules.

在这里,我们添加了一个任务rootCodeCoverageReport,它依赖于子模块中的codeCoverageReport任务。 此任务将从其子模块中提取kotlinClassessourceDirectories的所有值。 重要的是,将从子模块的/ jacoco路径中获取子模块的测试执行数据。 现在,我们已经合并了所有子模块的代码覆盖率数据。

The next part is integrating it with SonarQube.We need to define sonarqube properties. You can add org sonar.host.url and sonar.login. Add localhost url, if you want to run locally. Follow docs for sonarqube project set up guide.

下一部分是将其与SonarQube集成。我们需要定义声纳属性。 您可以添加组织sonar.host.urlsonar.login。 如果要运行本地,则添加localhost url 按照文档获取sonarqube项目设置指南。

sonarqube {
    properties {
        property "sonar.host.url", "http://localhost:9000/"
        property "sonar.login", "<SonarQube Token>"
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.projectKey", "Sixt_Ride_Driver_Android"
        property "sonar.projectName", "Sixt_Ride_Driver_Android"
        property "sonar.sources", "**/main/**"
        property "sonar.tests", "**/test/**"
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.coverage.exclusions", androidCoverageExclusion.join(",")
        property "sonar.exclusions", androidCodeExclusion.join(",")
        property "sonar.tags", "android"
        property "sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
        property "sonar.junit.reportsPath", "${project.buildDir}/test-results/testDebugUnitTest"
        property "sonar.java.binaries", "${project.buildDir}/tmp/kotlin-classes"
    }
}


def androidCoverageExclusion = [
        '**/databinding/**/*.*',
        '**/android/databinding/*Binding.*',
        '**/BR.*',
        '**/R.*',
        '**/R$*.*',
        '**/BuildConfig.*',
        '**/Manifest*.*',
        '**/*Module_*Factory.*'
]


def androidCodeExclusion = [
        '**/databinding/**/*.*',
        '**/android/databinding/*Binding.*',
        '**/BR.*',
        '**/R.*',
        '**/R$*.*',
        '**/BuildConfig.*',
        '**/Manifest*.*',
        '**/mockserver/**'
]


tasks.sonarqube.dependsOn "rootCodeCoverageReport" group "Reporting"

We don’t want to add generated classes as a part of code coverage. Also, there could be some classes that we may not wish to analyze. We can exclude such classes from code coverage by mentioning a list of classes in sonar.coverage.exclusions property. We can exclude code analysis of desired classes by using property sonar.exclusions.Finally, run the code coverage by calling sonarqube task. This will initiate a code coverage process.Run -> ./gradlew sonarqube

我们不想添加生成的类作为代码覆盖率的一部分。 同样,可能会有一些我们可能不想分析的类。 我们可以通过在sonar.coverage.exclusions属性中提及一个类列表,将这些类排除在代码范围之外 我们可以使用属性sonar.exclusions排除所需类的代码分析 最后通过调用sonarqube任务来运行代码覆盖率。 这将启动代码覆盖过程。 运行-> ./gradlew sonarqube

Image for post

Open the browser and enter a value for sonar.host.url. We can see our project listed in the Projects view. Further, if we detail it down, that gives us more information about classes, methods and conditions covered by test cases. Report also informs code smell and potential bugs.

打开浏览器,然后输入sonar.host.url的值 我们可以在“项目”视图中看到我们的项目。 此外,如果我们将其详细说明,则可以为我们提供有关测试用例涵盖的类,方法和条件的更多信息。 报告还告知代码气味和潜在的错误。

Now, we know the exact coverage percentage of the complete App code. Also, we have a better picture of an App code quality.

现在,我们知道了完整的应用程序代码的确切覆盖率。 同样,我们对应用程序代码质量有了更好的了解。

Feel free to post your thoughts on this article in the comment section below. Have a great day!

欢迎在下面的评论部分中发表您对本文的看法。 祝你有美好的一天!

翻译自: https://medium.com/@vichare.ketan89/sonarqube-for-multi-module-android-app-20e42147d4ff

侧扫声呐的应用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值