I have the latest version of JaCoCo with Gradle(the latest version). How can I exclude data classes from test coverage?
Starting from JaCoCo v0.8.2, you can exclude classes and methods by using a Generated annotation, JaCoCo will ignore them.
#ExcludeGenerated
data class User(val id: Int)
class Something {
#ExcludeGenerated
fun ignoreMe() { }
}
#Retention(AnnotationRetention.RUNTIME)
#Target(
AnnotationTarget.CLASS,
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.CONSTRUCTOR
)
annotation class ExcludeGenerated
https://github.com/jacoco/jacoco/releases/tag/v0.8.2
Classes and methods annotated with annotation whose retention policy is runtime or class and whose simple name is Generated are filtered out during generation of report (GitHub #731).
Related
I have test class MyTest:
package com.my.package;
...
#IntegrationTest
public class MyTest extends ServicelayerTest {
#Test
public myTest1() {...}
#Test
public myTest2() {...}
}
I only need to run the myTest1() test via ant.
To run all integration tests from the class, I can use
ant integrationtests -Dtestclasses.packages='com.my.package.MyTest'
How to run only one myTest1() ? Maybe I can use something like
ant integrationtests -Dtestclasses.packages='com.my.package.MyTest#myTest1()' ?
There is no filter that lets you run JUnit by method name.
Check the Filters documentation on what is supported: https://help.sap.com/docs/SAP_COMMERCE/d0224eca81e249cb821f2cdf45a82ace/f7f454a4f5254944a366db9bdf129be6.html
Currently, I'm porting my Spring Boot build.gradle.kts configuration to the Kotlin/MP stack. I don't know what to do with one part of the liquibaseRuntime configuration. The original config looks like:
// other dependencies omitted
liquibaseRuntime("org.liquibase:liquibase-core")
liquibaseRuntime("org.liquibase.ext:liquibase-hibernate5:3.8")
liquibaseRuntime(sourceSets.getByName("main").compileClasspath)
liquibaseRuntime(sourceSets.getByName("main").output)
liquibaseRuntime("org.postgresql:postgresql")
liquibaseRuntime("org.springframework.boot:spring-boot:$springBootVersion")
Some part of this config possibly can be replaced with:
sourceSets {
val jvmMain by getting {
dependencies {
configurations["liquibaseRuntime"].dependencies.addAll(listOf(
DefaultExternalModuleDependency("org.liquibase", "liquibase-core", null, "default"),
DefaultExternalModuleDependency("org.liquibase.ext", "liquibase-hibernate5", "3.8", "default"),
DefaultExternalModuleDependency("org.postgresql", "postgresql", null, "default"),
DefaultExternalModuleDependency("org.springframework.boot", "spring-boot", "2.2.4.RELEASE", "default")
// DefaultSelfResolvingDependency(configurations["compileClasspath"])
))
I've got stuck with these two and don't know what to do:
liquibaseRuntime(sourceSets.getByName("main").compileClasspath)
liquibaseRuntime(sourceSets.getByName("main").output)
They add instances of the dependency class DefaultSelfResolvingDependency (they also seem to be wrapped with some proxy). Looking through the liquibase-gradle plugin code didn't help.
So, how should I port these two dependencies?
Not familiar with with the Liquibase Gradle plugin. My assumption is you have applied the plugin in the following manner:
plugins {
id("org.liquibase.gradle") version "2.0.2"
}
Then you should be able to do what you have normally:
dependencies {
liquibaseRuntime("org.liquibase:liquibase-core")
liquibaseRuntime("org.liquibase.ext:liquibase-hibernate5:3.8")
liquibaseRuntime("org.postgresql:postgresql")
liquibaseRuntime("org.springframework.boot:spring-boot:$springBootVersion")
}
If for some reason that didn't work out-of-the-box, then you need to help Gradle's Kotlin DSL by explicitly retrieving a reference of the configuration:
val liquibaseRuntime by configurations
dependencies {
liquibaseRuntime("org.liquibase:liquibase-core")
liquibaseRuntime("org.liquibase.ext:liquibase-hibernate5:3.8")
liquibaseRuntime(sourceSets.getByName("main").compileClasspath)
liquibaseRuntime(sourceSets.getByName("main").output)
liquibaseRuntime("org.postgresql:postgresql")
liquibaseRuntime("org.springframework.boot:spring-boot:$springBootVersion")
}
You could also do the following as well:
dependencies {
"liquibaseRuntime"("org.liquibase:liquibase-core")
"liquibaseRuntime"("org.liquibase.ext:liquibase-hibernate5:3.8")
// ...
}
Reference: Understanding what to do when type-safe model accessors are not available
Now these two lines do not make sense to me.
liquibaseRuntime(sourceSets.getByName("main").compileClasspath)
liquibaseRuntime(sourceSets.getByName("main").output)
According to the API documentation for DependencyHandler, there are certain allowed notations. A sourceSet is not one of them. So not sure what to do there.
Proguard has options for -keep-ing a class based on its package name/hierarchy.
Is it possible to -keep based on the source file's actual path?
Example:
java/com/a/b/c/Class.java contains package com.a.b.c.Class
tests/com/a/b/c/ClassTest.java contains package com.a.b.c.ClassTest
There may be a large number of "*Test" classes and I want to Proguard -keep everything under tests/* for testing purposes. It should not keep any classes which happen to match "*Test" outside of the tests/* directory.
It doesn't seem like this would be possible with package matching since it has the same package as those classes under java/*
No, this is not possible. ProGuard does not consider the file path when applying rules.
You could use annotations for your test classes, e.g.
#TestClass
public class MyTest {
...
}
and then add a configuration like this:
-keep #TestClass class * { *; }
Is there a way to (easily) generate a HTML report that contains the tests results ? I am currently using JUnit in addition to Selenium for testing web apps UI.
PS: Given the project structure I am not supposed to use Ant :(
I found the above answers quite useful but not really general purpose, they all need some other major build system like Ant or Maven.
I wanted to generate a report in a simple one-shot command that I could call from anything (from a build, test or just myself) so I have created junit2html which can be found here: https://github.com/inorton/junit2html
You can install it by doing:
pip install junit2html
Alternatively for those using Maven build tool, there is a plugin called Surefire Report.
The report looks like this : Sample
If you could use Ant then you would just use the JUnitReport task as detailed here: http://ant.apache.org/manual/Tasks/junitreport.html, but you mentioned in your question that you're not supposed to use Ant.
I believe that task merely transforms the XML report into HTML so it would be feasible to use any XSLT processor to generate a similar report.
Alternatively, you could switch to using TestNG ( http://testng.org/doc/index.html ) which is very similar to JUnit but has a default HTML report as well as several other cool features.
You can easily do this via ant. Here is a build.xml file for doing this
<project name="genTestReport" default="gen" basedir=".">
<description>
Generate the HTML report from JUnit XML files
</description>
<target name="gen">
<property name="genReportDir" location="${basedir}/unitTestReports"/>
<delete dir="${genReportDir}"/>
<mkdir dir="${genReportDir}"/>
<junitreport todir="${basedir}/unitTestReports">
<fileset dir="${basedir}">
<include name="**/TEST-*.xml"/>
</fileset>
<report format="frames" todir="${genReportDir}/html"/>
</junitreport>
</target>
</project>
This will find files with the format TEST-*.xml and generate reports into a folder named unitTestReports.
To run this (assuming the above file is called buildTestReports.xml) run the following command in the terminal:
ant -buildfile buildTestReports.xml
Junit xml format is used outside of Java/Maven/Ant word.
Jenkins with http://wiki.jenkins-ci.org/display/JENKINS/xUnit+Plugin is a solution.
For the one shot solution I have found this tool that does the job:
https://www.npmjs.com/package/junit-viewer
junit-viewer --results=surefire-reports --save=file_location.html
--results= is directory with xml files (test reports)
I found xunit-viewer, which has deprecated junit-viewer mentioned by #daniel-kristof-kiss.
It is very simple, automatically recursively collects all relevant files in ANT Junit XML format and creates a single html-file with filtering and other sweet features.
I use it to upload test results from Travis builds as Travis has no other support for collecting standard formatted test results output.
There are multiple options available for generating HTML reports for Selenium WebDriver scripts.
1. Use the JUNIT TestWatcher class for creating your own Selenium HTML reports
The TestWatcher JUNIT class allows overriding the failed() and succeeded() JUNIT methods that are called automatically when JUNIT tests fail or pass.
The TestWatcher JUNIT class allows overriding the following methods:
protected void failed(Throwable e, Description description)
failed() method is invoked when a test fails
protected void finished(Description description)
finished() method is invoked when a test method finishes (whether passing or failing)
protected void skipped(AssumptionViolatedException e, Description
description)
skipped() method is invoked when a test is skipped due to a failed assumption.
protected void starting(Description description)
starting() method is invoked when a test is about to start
protected void succeeded(Description description)
succeeded() method is invoked when a test succeeds
See below sample code for this case:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class TestClass2 extends WatchManClassConsole {
#Test public void testScript1() {
assertTrue(1 < 2); >
}
#Test public void testScript2() {
assertTrue(1 > 2);
}
#Test public void testScript3() {
assertTrue(1 < 2);
}
#Test public void testScript4() {
assertTrue(1 > 2);
}
}
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class WatchManClassConsole {
#Rule public TestRule watchman = new TestWatcher() {
#Override public Statement apply(Statement base, Description description) {
return super.apply(base, description);
}
#Override protected void succeeded(Description description) {
System.out.println(description.getDisplayName() + " " + "success!");
}
#Override protected void failed(Throwable e, Description description) {
System.out.println(description.getDisplayName() + " " + e.getClass().getSimpleName());
}
};
}
2. Use the Allure Reporting framework
Allure framework can help with generating HTML reports for your Selenium WebDriver projects.
The reporting framework is very flexible and it works with many programming languages and unit testing frameworks.
You can read everything about it at http://allure.qatools.ru/.
You will need the following dependencies and plugins to be added to your pom.xml file
maven surefire
aspectjweaver
allure adapter
See more details including code samples on this article:
http://test-able.blogspot.com/2015/10/create-selenium-html-reports-with-allure-framework.html
I have created a JUnit parser/viewer that runs directly in the browser. It supports conversion to JSON and the HTML report can be easily reused.
https://lotterfriends.github.io/online-junit-parser/
If you are still missing a feature feel free to create an issue on Github. :)
I have Grails test:
class GormTests extends GroovyTestCase {
static transactional = false
...
}
I'm trying to mark method as transactional by org.springframework.transaction.annotation.Transactional annotation but following exception arises:
org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class GormTests]: Common causes of this problem include using a final class or a non-visible class; nested exception is net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
How can I fix it?
#Transactional is only for Spring beans - test classes aren't beans. Your best bet is to split the test class into two, one with non-transactional tests and one with.