Remove Duplicates Tests ๐งนโ
Why Deduplication? โ๏ธโ
When developing or maintaining a software, it is common for test suites to grow in size. This often results in redundancy, as many test cases cover the same functions or scenarios. This is where Test Deduplication comes into play.
It simplifies the testing process by removing redundant test cases, which saves time and resources while keeping the testcases which adds value to the overall coverage of the application.
Usage ๐ ๏ธโ
To detect duplicate tests, simply run the below command, like so:
keploy dedup -c "<CMD_TO_RUN_APP>" -t="<TESTSETS_TO_RUN>"
For Node Applicationsโ
1. Pre-requsite
Install the keploy/sdk
and nyc
package : -
npm i @keploy/sdk nyc@15.0.0
Add the the following on top of your main application js file (index.js/server.js/app.js/main.js) : -
const kmiddleware = require('@keploy/sdk/dist/v2/dedup/middleware.js')
app.use(kmiddleware())
2. Run Deduplication
keploy dedup -c "<CMD_TO_RUN_APP>" --delay 10 -t="<TESTSETS_TO_RUN>"
Exampleโ
Let's use the expresss-mongoose application to test dedup feature. In our src/app.js
file we need to have imported and initialized @keploy/sdk
package, so now let's run the de-duplication command to check : -
keploy dedup -c "node src/app.js" -t "test-set-1"
Voila! Keploy will now detect duplicate tests .
For Java Applicationsโ
1. Pre-requsite
Put the latest keploy-sdk in your pom.xml file : -
<dependency>
<groupId>io.keploy</groupId>
<artifactId>keploy-sdk</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Now that we have added keploy-sdk, let's import it in our main class : -
import io.keploy.servlet.KeployMiddleware;
@Import(KeployMiddleware.class)
public class SamplesJavaApplication {
public static void main(String[] args) {
SpringApplication.run(SamplesJavaApplication.class, args);
}
}
2. Run Deduplication
We need to create Jar file via : -
mvn clean install -DskipTests
Once we have our jar file ready, we can run following command : -
keploy dedup -c "java -javaagent:<PATH_TO_JacocoAgent>=address=*,port=36320,destfile=jacoco-it.exec,output=tcpserver -jar <PATH_TO_JAR_FILE>" --delay 10 -t="test-set-0"
Voila! Keploy will now detect duplicate tests .
For Python Applicationsโ
Deduplication works only on test mode there are no special instructions to record your tests.
1. Pre-requsite
Put the latest keploy-sdk in your file : -
pip install keploy coverage requests fastapi
In your main app file add the following with along with the other imports. And add Keploy's middleware along with the other middlewares for your app based on your framework:
- In FastAPI -
# existing imports
from keploy import FastApiCoverageMiddleware
app.add_middleware(FastApiCoverageMiddleware)
# existing functions
- In Flask -
# existing imports
from keploy import FlaskCoverageMiddleware
app.wsgi_app = FlaskCoverageMiddleware(app.wsgi_app)
# existing functions
- In Django - Open
settings.py
and add the middleware class to the MIDDLEWARE list.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'keploy.DjangoCoverageMiddleware', # Add keploy middleware here
],
# Other settings
2. Run Deduplication
Run keploy with test-sets in which you want to check for the duplicate testcases :
keploy dedup -c "<command to run your Python app>" --delay "<time required for your application to start>"
Exampleโ
Let's use the flask-mongo application to test dedup feature. In our app.py
file we need to have imported and initialized keploy
package, since this is a flask application we can follow above flask approach. Once we have added package, let's run the de-duplication command to check : -
keploy dedup -c "python3 app.py" -t "test-set-1"
Remove Duplicate Testsโ
You can simply remove duplicate tests with :
keploy dedup --rm