如何在Scala上做增量覆盖率(incremental coverage)计算?

如何在Scala上做增量覆盖率(incremental coverage)计算?

Created: June 24, 2022 10:56 AM Last Edited Time: June 24, 2022 11:05 AM Tag: Scala, Spark Type: Sharing Blog

在Scala上写单测

下面是之前写的英文版简述,直接粘贴过来了。 你还可以参考:Spark-Scala单元测试实践 - 码农教程 (manongjc.com)

Unit Tests

Unit testing is a powerful tool for software quality -- and has been for decades. Unit tests provide a fundamental check that an application meets its software design specifications and behaves as intended.

What tools are used?

UnitTest Tool: ScalaTest

Mock Tool: Mockito Scala

HTTP Mock Tool: WireMock

Code Coverage Tool: Scoverage

How to write tests?

First, you need to know that writing a single test is easy. More information on ScalaTest

Tips:

  1. You can mock functions by Mockito Scala
  2. You can mock http service by WireMock
  3. When there are public functions, make full use of before, after and withFixture.

重点来了,如何做增量覆盖率测试?

我们用 Scoverage 做覆盖率测试,但是它不支持增量覆盖率测试,所以我们需要手动改造一下。 如果你用 jacoco做的覆盖率测试,可以试试 jacoco: jacoco二开,支持增量代码覆盖率 (gitee.com) 这篇文章。

大概的思路是:

  1. Calculate the number of files and lines with changes through git diff
  2. Using the feature of the scoverage, comments are added before and after the number of lines that have changed. see: https://github.com/scoverage/scalac-scoverage-plugin

贴上python脚本: HandleIncrementalCoverage.py

#!/usr/bin/env python

"""
Handle code for Incremental Coverage
Tips: only active with scoverage
Principle:
  1. Calculate the number of files and lines with changes through git diff
  2. Using the feature of the scoverage, comments are added before and after the number of lines that have changed.
     see: <https://github.com/scoverage/scalac-scoverage-plugin>
"""
import os
import re
import subprocess
import sys

def getChangedLineInfoFromDiffLines(lines):
    """
    args : Lines, the description of a file output by the git-diff command
    returns : List[(changedLineStart, changedLineEnd)], Front-closed and back-open interval
    """
    changedLineInfo = []
    # Get line change information according to "@"
    # Matching [0]: "," + the number of rows deleted from here;
    # [1]: the number of 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值