Skip to main content
Version: 2.0.0

Merge Unit Test Coverage Data

To get the coverage report, first make sure all the requirements are met and then run Keploy test as usual with your application command:

keploy test -c "your_application_command"

After successful execution of this command, A coverage report would be generated inside the test-run folder of keploy/reports.

keploy
├── reports
│ └── test-run-0
│ ├── coverage.yaml
│ └── test-set-0-report.yaml
└── test-set-0
├── mocks.yaml
└── tests
├── test-1.yaml
└── test-2.yaml

Note: In case of java application, before running test subcommand, you need to clean the project by removing any previously generated file, and run install command.

mvn clean install -Dmaven.test.skip=true

🛠️ Language Specific Requirements

Programming LanguagePrerequisites
go1. The application should have a graceful shutdown to stop the API server on SIGTERM or SIGINT signals. Refer appendix for basic implementation of graceful shutdown function.
2. The go binary should be built with -cover flag.
pythonPython 3 and above
coverage.py
javascriptnyc
javaJacoco 0.8.8

Graceful Shutdown

It is important that the application is shutdown gracefully. In case of Golang, function for graceful shutdown:

func GracefulShutdown() {
stopper := make(chan os.Signal, 1)
// listens for interrupt and SIGTERM signal
signal.Notify(stopper, os.Interrupt, os.Kill, syscall.SIGKILL, syscall.SIGTERM)
go func() {
select {
case <-stopper:
os.Exit(0)
}
}()
}

func main() {

port := "8080"

r := gin.Default()

r.GET("/:param", getURL)
r.POST("/url", putURL)
// should be called before starting the API server from main()
GracefulShutdown()

r.Run()
}

Usage

To get the coverage data for your unit tests:

coverage run --data-file=.coverage.unit test_program.py

Here, test_program.py is the unit test program you want to run, and --data-file is set to .coverage.unit becuase, by default, raw coverage data would be written to .coverage which is where coverage data for keploy tests is present, so to avoid overwritting we pass a new file through data-file flag.

Note: If you face any problems with running the coverage library, you can refer to the documentation for the same here

Combine And Get Report

To combine the coverage from the unit tests, and Keploy's API tests we can use the command below:

coverage combine

Make sure to run this command before starting a new test run to avoid getting multiple coverage files.

Finally, to generate the coverage report for the test run, you can run:

coverage report

and if you want the coverage in an html file, you can run:

coverage html