randunit, a single test generator for android/jvm

background

With continuous delivery becoming more and more popular today, single test, as an important part of ensuring CI quality, has also begun to be valued in China.

However, on the single test, everyone's attitude is quite contradictory. There are two main things to worry about:

I don't know where to start the first step from 0 to 1
What if you do it hard and find it useless
However, without this link, many serious compile time problems in the whole devops process will be delayed until the runtime is exposed, which hinders the project efficiency..

So there is a source of inspiration:

Automatically generate a series of smoke level single test cases, and can find serious problems
Low access cost, painless integration with existing processes
It can be effective for android (Huawei re mobile terminal)

What did you do

RandUnit is taken from random unit test. It will:

Search all relevant classes and methods to be tested according to the package name or entry class provided
Based on the search results, a series of statements are generated for each method for testing
Like the routine single test process, run these statements on junit to get the test results
All this requires only one simple copy and paste:

import com.williamfzc.randunit.env.NormalTestEnv
import com.williamfzc.randunit.models.StatementModel
import com.williamfzc.randunit.scanner.ScannerConfig
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
class MinExampleTest(private val statementModel: StatementModel) {
    companion object {
        private val testEnv = NormalTestEnv()
        private const val packageName = "com.your.package"
        private val cases by lazy {
            val scannerConfig = ScannerConfig()
            scannerConfig.includeFilter.add(packageName)

            RandUnit.collectStatementsWithPackage(packageName, scannerConfig)
        }

        @JvmStatic
        @user3ters(name = "{0}")
        fun data(): Collection<StatementModel> {
            return cases
        }
    }

    @Test
    fun runStatements() {
        testEnv.runStatementInSandbox(statementModel)
    }
}

Because it is a legitimate junit use case, you can run it directly in the ide. Directly run with coverage:

As you can see, it helps you generate 500 + use cases and execute them, and achieves good coverage.

Solved what

Go back and see if the above three problems have been solved:

Automatic generation: the current strategy is to automatically search and generate a series of junit use cases after each compilation and execute them. There is no need to change the use cases if there are code changes
Low access cost: one paste can access. In addition, because it is a legitimate junit use case, it can also be used as a normal junit use case, for example, in a CI environment
Effective for android: thanks to the support of robolectric, randunint can be well applied to android projects without real machines. However, the above code needs to be fine tuned. If you are interested, you can see the project home page.
Questions like this:

override fun getCastOptions(context: Context?): CastOptions? {

    // oh, you import a non-existed class here!
    // it should cause a ClassNotFoundException
    Class.forName("import unknown class here!")

    ...
}

This type of problem cannot be found at compile time. The later the problem is exposed = = the higher the repair cost. After use, it will not be omitted after integration:

WARNING: error happened inside sandbox: java.lang.reflect.InvocationTargetException

java.lang.ClassNotFoundException: import unknown class here!

    at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:158)

When is randunint recommended

  • For a long time, no matter the size of the project, there are still a large number of projects under development without any unit tests. With the popularity of devops, continuous automated testing has almost become the most critical part of the whole agile process.
  • In this case, many businesses recognize the importance of this stage, but do not know where to start
  • The starting point of this project is to run unit tests at the lowest possible cost and at least fill the gaps here

Recommended use

Your project is still running naked or unit testing is not doing well

Not recommended

You want a tool that can thoroughly find problems and completely cover the whole project

Prototype and evolution direction

Some friends will definitely take evosuite and randoop against me, and in fact, the inspiration of this project comes from them. Interested can see: prototype and evolution direction

link

Specific introduction and project homepage: https://github.com/williamfzc...
Look at this: https://github.com/williamfzc...

Keywords: Android unit testing DevOps

Added by my800stuff on Fri, 21 Jan 2022 03:10:21 +0200