I'm learning on JUnit5 with Kotlin, and I wrote a test code with special names as function names using the back tick but nothing was displayed for passed tests. I used the annotation of #DisplayName("something") that didn't display any name of passed tests.
i always get this empty list of tests
here is the code
#DisplayName("A special test case")
class DisplayNameTest {
#Test
#DisplayName("First Test")
fun `First Test with normal name`() {}
#Test
#DisplayName("(╯°Д°)╯")
fun `Second Test with Special Characters`() {}
#Test
#DisplayName("😊")
fun `Test name including an Emoji`() {}
}
Click that check mark above the results to show passing tests
It's just that usually, you only care about the failing ones!
Related
Im using Spock for my tests and I have multiple classes to test. I want to tell Spock to test each class in specific order. Is there a way to do this? I noticed TestNG has got #AfterTest annotation, so does Spock have something similar?
You can't specify Spock test classes execution order. However Spock allows you to specify methods execution order when you add #Stepwise annotation to your test class - in this case Spock will execute all methods in order from top to bottom and if one method fails it will ignore remaining methods in this test class.
Indicates that a spec's feature methods should be run sequentially in their declared order (even in the presence of a parallel spec runner), always starting from the first method. If a method fails, the remaining methods will be skipped. Feature methods declared in super- and subspecs are not affected.
#Stepwise is useful for specs with (logical) dependencies between methods. In particular, it helps to avoid consecutive errors after a method has failed, which makes it easier to understand what really went wrong.
Reference: http://spockframework.org/spock/javadoc/1.1/spock/lang/Stepwise.html
#Stepwise
class StepwiseExample extends Specification {
def "first test method to run"() {
// ....
}
def "second test method to run"() {
// ....
}
def "if previous method failed this one will be ignored"() {
// ....
}
}
Using org.junit.runners.Suite
Jeff Scott Brown gave a good comment about JUnit's #Suite.SuiteClasses. In this case you create a class where you can aggregate your test classes (specifications) into a single test suite and those classes will be executed in the order they have been defined in the suite. Consider following example:
import org.junit.runner.RunWith
import org.junit.runners.Suite
#RunWith(Suite)
#Suite.SuiteClasses([
Test2Specification,
Test1Specification
])
class TestSuiteSpecification { }
This suite executes two specifications Test2Specification and Test1Specification in the defined order:
How can you get a jenkins groovy script to produce a junit xml results file? I'm doing this purely for the purpose of generating junit results with a specific number of passed/failed and skipped test cases. I need this so that I have a set of test data to test against for another application. This other app goes out to various jenkins jobs and analyzes the junit results from the job's json output. I want to point my functional tests at this jenkins job for testing. (I can't use my real continuous integration jobs because that wouldn't be deterministic).
I've got a basic groovy test case like what's below. It runs but doesn't produce junit output. I didn't expect it to, but I'm also not sure how to get it to generate one.
class BunchOfTests extends GroovyTestCase {
void testOne(){}
void testTwo(){fail()}
}
I also played around with writing code that prints the junit results xml but it's getting lengthy and quite ugly. I've seen the threads on here about what the junit results xsd looks like but I'm thinking there's got to be an easier route to generating some results without needing a pre-made results file. 10 results or so ought to be enough for what I need.
Generally unit testcases in groovy has to be written as below
import groovy.util.GroovyTestCase
class sampleTest extends GroovyTestCase {
assertEquals(true, val);
}
Incase you want only Junit reporting in groovy.
How would I produce JUnit test report for groovy tests, suitable for consumption by Jenkins/Hudson?
I am a beginner in soapui testing. Hopefully you can help me solving this problem.
In my test project I have a test suites which contains several test cases. Multiple test case will start the same test case. To run this test case I need some property values to be transferred to this test case.
I tried to achieve this in two ways. But I failed in both.
I tried to call the test case and set the needed properties in the test case. I start the test case from a Groovy script. But I couldn't find a good example how to set the properties in the called test case.
I tried to get the property values of the calling parent test case inside the called test case. It looks like the parent test case that called the test case isn't available in the context of the running test case.
The test cases, that will call the same test case, will be run in parallel. So, I think it isn't a solution to first set the property values and then start the test case, because they will be overwritten by the other test cases that run at the same time. Also using test suite properties for these values won’t work because of running the test cases in parallel.
My test project looks like this.
MyProject
TestSuite_APLtests
TestCase_user_01
Properties test step
Run_test <groovy script>
Step_01
…..
TestCase_user_02
Properties test step
Run_test <groovy script>
Step_01
…..
TestCase_General
Properties test step
POST sessions
Step_01
…..
The ‘Properties test step’ of each ‘TestCase_user_’ contains a user and password needed in test case ‘TestCase_General’ and will be different for each test case.
In the ‘Run_test’ groovy script of each ‘TestCase_user_’ the test case ‘TestCase_General’ is started by using:
def myTestSuite = testRunner.testCase.testSuite.project.getTestSuiteByName("TestSuite_APLtests")
def myTestCase = myTestSuite.getTestCaseByName("TestCase_General")
myTestCase.run(null, false)
How can I add the properties user and password to the run comment that starts the test case?
If I try to get the property values with a groovy script in test case ‘TestCase_General’ I don’t know how to determine which test case has called ‘TestCase_General’. I found some posts on internet that suggests to use: context.getProperty("#CallingRunTestCaseStep#") to determine the calling test case. But this value is null. And when I try to check if the calling test case is available in the context by using: context.hasProperty("#CallingRunTestCaseStep#") this is false, so this doesn't work to find the calling test case.
Can someone tell me what the solution will be to get this working.
Thanks,
You can set Test Case properties from groovy script with setPropertyValue(name,value) method, however if you run the Test Cases in parallel, this properties as you said will be overwritten for each Test Case calling TestCase_General. So instead of use setPropertyValue you can pass the context properties through the run(StringToObjectMap properties, boolean async) method in the WsdlTestCase.java class. Your groovy code to call TestCase_General could be:
import com.eviware.soapui.support.types.StringToObjectMap
// get test suite
def myTestSuite = testRunner.testCase.testSuite.project.getTestSuiteByName("TestSuite_APLtests")
// get your test case
def myTestCase = myTestSuite.getTestCaseByName("TestCase_General")
// set the user and password properties in the context
context.setProperty("user","userTestCaseN")
context.setProperty("password","passwordTestCaseN")
// run the testCase passing the context
def contextMap = new StringToObjectMap( context )
myTestCase.run(contextMap,false);
To access the context properties in the groovy script of your TestCase_General use this code:
context.getProperty("userPassword")
Or if you prefer to use context.expand:
context.expand('${#user}')
Note that the use of # depends on how you are accessing the properties.
If you also need to use the context properties in the SOAP Test Request of your TestCase_General use this way ${#propetryName} i.e:
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Header/>
<Body>
<request>
<user>${#user}</user>
</request>
</Body>
</Envelope>
Hope this helps,
I am writing tests in Groovy for my Java application, and facing a strange problem, which can be explained with the below lines of code. Instead of printing /hello/world, it prints /ello/orld.
// File MyClassTest.groovy
import org.junit.Test
class MyClassTest {
#Test
void fun(){
println("/hello/world")
}
}
Output:
/ello/orld
When I write a separate hello.groovy within the same project in IntelliJ IDEA, it prints /hello/world without any problem.
// File hello.groovy
println("/hello/world")
Output:
/hello/world
I am not providing any VM arguments in either case.
Anyone have any idea why this can happen?
I'd like an equivalent function to nUnit's Category attribute for test cases.
I have inherited a large number of C++ test cases, some of which are unit tests and some of which are longer-running integration tests, and I need to set up my continuous integration build process to ignore the integration test cases.
I would prefer to simply tag all the integration test cases and instruct cppunit to exclude them during CI builds.
Am I overlooking a feature of cppunit or is there an alternative way to achieve this?
There are no native test category attributes. CppUnit is a bit simpler than that. CppUnit doesn't even come with a command-line test runner for your app. You have to write your own simple main() function that executes a TestRunner.
Here's the canonical example.
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
int main( int argc, char **argv)
{
CppUnit::TextUi::TestRunner runner;
CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest( registry.makeTest() );
bool wasSuccessful = runner.run( "", false );
return wasSuccessful;
}
A TestSuite is a collection of TestCases. A TestRunner executes a collection of TestSuites. Notice that in this example it gets the TestSuites from the TestFactoryRegistry, which you populate by using a macro call to CPPUNIT_TEST_SUITE_REGISTRATION(MyTestSuite). But the TestCases are still your test classes.
You can certainly implement these attributes yourself, just as you would extend any class with a facade. Derive your new class from TestSuite. Add attributes to your tests that you could select on, then populate your TestRunner executing "just unit tests" or "just integration tests" or whatever you want.
For that matter, a TestRunner can select tests to execute based on name. If you named all your integration tests with a prefix like ITFoo, ITBar, etc., you could select all tests that begin with "IT".
There are dozens of ways to solve your problem, but you'll have to do it yourself. If you can write code worthy of unit testing, it shouldn't be a big deal for you. :-)