maven Cobertura reports not working as expected - mockito

I am using maven with cobertura plugin for my application. The test class what I have written is for controller and running fine without any issues in eclipse.There are so many dependencies on other classes.I am using mockito and powermockito both .
When I run
mvn clean test
from command prompt it shows all the test cases that are passed.I have around 9 test cases, but when I run
mvn cobertura:cobertura
it shows only one test case run
Tests run: 1, Failures: 0, Errors: 1
The error what it is showing is in different class[assume it as X class] which I have not modified but I am mocking this X class in my test class.
1)
Does cobertura depends on other classes also apart from main test
class for code coverage? If so what I need to do .
2)
I am not using injectmocks in my test class instead I am using new
operator to call the controller class.Since inject mocks was calling real methods I have not used this.
Any help would be appreciated

Related

Jest test coverage gives 100% coverage for modules that have no tests at all

I'm experiencing 2 issues with Jest test coverage:
1- Jest reports 100% (Stmts, Branch, Func, and Lines) coverage for modules with no tests at all but required by tested modules
2- Jest reports 100% Branch coverage for modules with no tests nor required by tested modules
In the image:
sum is a tested module
toNum is not tested but required by sum
sub is neither tested nor required by tested modules
I've create a repo that reproduces the issue:
https://github.com/hossamnasser938/Reproduce-Jest-Coverage-Issue
Here is my package.json:
Here is my folder structure:
How do I remove the entry for sub.js from the coverage report? I really appreciate your help.
1- Jest reports 100% (Stmts, Branch, Func, and Lines) coverage for modules with no tests at all but required by tested modules
This is expected behavior when you configure Jest to collect coverage. The required modules are still executed by your test, despite them not having a test of their own. The documentation for collectCoverage states
this retrofits all executed files with coverage collection statements
Your test is executing all of the code from the required module, so the module is covered completely, 100% by your test code. Even though the module is executed by another test, you should still write a separate test for the required module to ensure it works in isolation as a unit.
2- Jest reports 100% Branch coverage for modules with no tests nor required by tested modules
Again this is expected behavior given your configuration value for collectCoverageFrom which includes sub.js, and your implementation of sub which has no branching, so Branch statements would be 100% executed (because there are none).
If you want Jest to stop reporting on sub.js then you have a couple of options:
Remove the configuration for collectCoverageFrom entirely, and
that will remove the entry for sub.js from the coverage report
table.
Modify your value for collectCoverageFrom to specifically
ignore sub.js:
collectCoverageFrom: ['src/**/*.js', '!src/**/sub.js']
Personally, I would leave everything as-is to serve as a reminder that sub.js still needs to be tested with adequate coverage.

Test results for skipped tests - cucumber and webdriver io

I am using allure to generate my test report and I noticed I had no "skipped" tests being reported - I knew I had some skipped tests. Digging through the code, the skipped tests are not being run (which is correct) but there is no result being generated. I was sure that using mocha I would still get a result file for skipped tests.
I am using the latest (as of now) webdriverio plugin as well as cucumber 7.0.0
Does anyone know if there is a way to "force" a result to be created for a skipped test?

How to use Spock from the Command Line

I am writing a set of groovy scripts to be used as part of a Jenkins Pipeline Library. Currently I am using plain old JUnit to test them but would like to switch to Spock. I simply run the tests from the command line by invoking the following groovy script.
import groovy.util.AllTestSuite
import junit.textui.TestRunner
System.setProperty(AllTestSuite.SYSPROP_TEST_DIR, "./tests")
System.setProperty(AllTestSuite.SYSPROP_TEST_PATTERN, "**/*Test.groovy")
TestRunner.run(AllTestSuite.suite())
I am trying to figure what the equivalent script would be to run Spock specifications. My first attempt was to switch the SYSPROP_TEST_PATTERN to "**/*Spec.groovy. I have one ...Spec.groovy file written and sitting under ./tests that looks like this:
#Grab(group='org.spockframework', module='spock-core', version='1.0-groovy-2.3')
import spock.lang.*
class UtilsSpec extends Specification {
def "Just testing"() {
expect:
1 + 1 == 2
}
}
When I invoke my groovy script though I get:
java.lang.RuntimeException: Don't know how to treat
/SourcCode/jenkins/pipeline-utils/tests/JustTestingSpec.groovy as a
JUnit test
That makes sense. I need to be using Sputnik but I've looked at the Spock and Sputnik source, and the Spock example project but these all assume you are using maven or gradle. I can't figured out the right way to invoke Sputnik directly. Any ideas?
Even though what you ask is possible and BalRog has already suggested a solution, in the long run it is better if you just use Gradle or Maven to run your tests from command line.
All Jenkins tutorials you will encounter will talk about Maven and/or Gradle and thus it would make much more sense to use a build system than custom scripts.

Cucumber/gradle example not generating report?

I'm investigating using gradle and cucumber together, and found this lovely example in cucumber's github.
So, I cloned the repository and ran it myself. It failed, as it's configured to do, but I couldn't find the HTML or JSON report that it appears to be configured to output. I say appear because I'm brand new to cucumber, but this class would seem to indicate where it'll put it:
#RunWith(Cucumber.class)
#Cucumber.Options(format = {"pretty", "html:build/cucumber-html-report", "json-pretty:build/cucumber-report.json"})
public class RunCukesTest {
}
However, it's not appearing in the build directory after running gradle cucumber.There's no cucumber-html-report directory, not is there a cucumber-report.json file. I'm running it with Java 7 and Gradle 1.6, if it matters.
Ideas? Is this a known issue with the Cucumber/Gradle integration?
The class name changed depending on the version of Cucumber you are using. It changed from json-pretty to json.
When running the 'cucumber' task on this example the generated cucumber report is located at 'build/cucumber-html-report/index.html'. Running the 'test' task fails as it seems that gradle has problems to create the test report for the cucumber created tests (file name contains spaces) I need to dig a bit into this to see how this can be fixed in gradle.
cheers,
René
The cucumber-jvm-example doesn't do reporting using gradle cucumber, but does do it with gradle test. However, gradle test will have a couple issues, namely showing a "null" test of sorts.
A workaround to this, if need be, is to add the formats to the args of the javaexec that runs cucumber. For example, in build.gradle:
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--format', 'html:cucumber-html-report', '-f', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
}
I had an error with that very same line (taken from this tutorial).
In order to resolve, had to change the third parameter from "json-pretty" to just "pretty"
So this is my final code line:
#CucumberOptions(format = {"pretty", "html:target/cucumber-html-report", "pretty:target/cucumber-report.json"})
BTW,
#Cucumber.Options is deprecated, we should use CucumberOptions

How can create a Junit4 Suite with Groovy?

I have
#RunWith(Suite.class)
#Suite.SuiteClasses( [
First.class,Second.class
])
public class MySuite{
}
But eclipse doesn't give me a "Run As Junit4 Test". All the individual test classes work fine with GUnit, the groovy unit test runner built into eclipse.
Any thoughts?
The only way I found to do this was to create a java class with a #Suite.SuiteClasses annotation as usual. You can put in the .class names of the Groovy classes you have for tests and it works well.
Kind of lame if you don't already have some java code to do it this way but I had a mix of Java and Groovy so it wasn't as big a deal.
The other option is to not use Suites at all and to use name patterns to find and run tests. Both ant and maven support this sort of pattern matching. I find it much easier than making sure I keep the suites up to date and Eclipse can just run all tests in a package if I want to do that.
Thanks for the help.
#Suite.SuiteClasses accepts Class[] as its parameter.
You may try:
#RunWith(Suite.class)
#Suite.SuiteClasses([First.class, Second.class] as Class[])
public class MySuite {
}

Resources