Stop Suite Execution After First Failure In TestNG - ant

I am using Ant to execute a set of TestNG tests as follows:
<testng suitename="functional_test_suite" outputdir="${basedir}/target/"
classpathref="maven.test.classpath" dumpCommand="false" verbose="2"
haltonfailure="true" haltonskipped="false" parallel="methods" threadCount="2">
<classfileset dir="${basedir}/target/test-classes/">
<include name="**/*Test.class" />
</classfileset>
I would like for the tests to stop immediately after the first failure. haltonfailure does not seem to do the trick, it just halts the ant build if the whole suite has test failures. Is there any way I can halt the suite execution on first failure?
Thanks

You can set dependencies on your individual test methods. testng dependencies. This will only run test methods if desired dependencies passed.

You can use a suite listener for this purpose.
public class SuiteListener implements IInvokedMethodListener {
private boolean hasFailures = false;
#Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
synchronized (this) {
if (hasFailures) {
throw new SkipException("Skipping this test");
}
}
}
#Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.isTestMethod() && !testResult.isSuccess()) {
synchronized (this) {
hasFailures = true;
}
}
}
}
#Listeners(SuiteListener.class)
public class MyTest {
#Test
public void test1() {
Assert.assertEquals(1, 1);
}
#Test
public void test2() {
Assert.assertEquals(1, 2); // Fail test
}
#Test
public void test3() {
// This test will be skipped
Assert.assertEquals(1, 1);
}
}

Related

Is there a way to override a closure in a Spock test?

While this is specific to Jenkins code this question I think is more generally about the metaprogramming features of Groovy.
This is what my tests looks like:
// MyDeclarativePipelineTest.groovy
class MyDeclarativePipelineTest extends DeclarativePipelineTest {
#Override
#Before
public void setUp() throws Exception {
super.setUp();
helper.registerAllowedMethod("parameters", [ArrayList.class], null)
}
}
// someTests.groovy
class someTests extends MyDeclarativePipelineTest {
#Override
#Before
public void setUp() throws Exception {
super.setUp();
}
#Test
public void pipeline_should_execute_without_errors() throws Exception {
addParam('MYPARAM', 'sdfsdfsdf')
def script = loadScript("Jenkinsfile")
script.run()
assertJobStatusSuccess()
}
}
// Jenkinsfile
pipeline {
stages {
stage('Build') {
steps {
If(true) {print("sdfsdfsd")} // should fail because not wrapped in script{} step
echo 'Building..'
}
}
}
}
I'm using this library in my Spock tests: JenkinsPipelineUnit
I want to overrided the steps{} closure in my MyDeclarativePipelineTest class
The steps closure is defined in this class StageDeclaration.groovy
StageDeclaration is used in the GenericPipelineDeclaration class like this:
def stage(String name,
#DelegatesTo(strategy = DELEGATE_FIRST, value = StageDeclaration) Closure closure) {
this.stages.put(name, createComponent(StageDeclaration, closure).with { it.name = name; it })
}
And then finally the GenericPipelineDeclaration is used in DeclarativePipelineTest like this:
def pipelineInterceptor = { Closure closure ->
GenericPipelineDeclaration.binding = binding
GenericPipelineDeclaration.createComponent(DeclarativePipeline, closure).execute(delegate)
}
I'm extending DeclarativePipelineTest with my own class MyDeclarativePipelineTest
Is there a way I can override the steps closure inside of MyDeclarativePipelineTest? I want it to throw an error if raw Groovy code is defined in the steps{} closure that isn't wrapped in a script{} closure.

Files/Paths/FileUtils - Mock several static classes in one test

I have to mock the below method:
public static void cleanAndCreateDirectories(#NonNull final Path path) throws IOException {
// If download directory exists(should not be symlinks, clear the contents.
System.out.println(path);
if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
System.out.println("lol");
FileUtils.cleanDirectory(path.toFile());
} else {
// Eager recursive directory creation. If already exists then doesn't do anything.
Files.createDirectories(path);
}
}
I have tried doing like this but it doesn't work:
#Test
public void cleanAndCreateDirectoriesPathExistsHappyCase() throws IOException {
PowerMockito.mockStatic(Paths.class);
PowerMockito.when(Paths.get("/dir/file")).thenReturn(FileSystems.getDefault().getPath("/dir/file"));
PowerMockito.mockStatic(Files.class);
PowerMockito.when(Files.exists(Paths.get("/dir/file"), LinkOption.NOFOLLOW_LINKS)).thenReturn(true);
File file = new File("/dir/file");
Mockito.when(Paths.get("/dir/file").toFile()).thenReturn(file);
PowerMockito.mockStatic(FileUtils.class);
ArsDumpGeneratorUtil.cleanAndCreateDirectories(Paths.get("/dir/file"));
}
I am getting following exception message:
File cannot be returned by get()
[junit] get() should return Path
Am I doing something wrong? Please tell me the best practice for this.
Solved as follows:
#Test
public void cleanAndCreateDirectoriesPathExistsHappyCase() throws IOException {
PowerMockito.mockStatic(Files.class);
PowerMockito.when(Files.exists(Paths.get("/dir/Exist"), LinkOption.NOFOLLOW_LINKS)).thenReturn(true);
// Mock FileUtils.
PowerMockito.mockStatic(FileUtils.class);
PowerMockito.doNothing().when(FileUtils.class);
// Call internal-static method.
FileUtils.cleanDirectory(Matchers.any());
// Call target method.
ArsDumpGeneratorUtil.cleanAndCreateDirectories(Paths.get("/dir/Exist"));
// Check internal-static method call.
PowerMockito.verifyStatic(Mockito.times(1));
FileUtils.cleanDirectory(Matchers.any());
}
#Test
public void cleanAndCreateDirectoriesPathNotExistsHappyCase() throws IOException {
PowerMockito.mockStatic(Files.class);
PowerMockito.when(Files.exists(Paths.get("/dir/NotExist"), LinkOption.NOFOLLOW_LINKS)).thenReturn(false);
PowerMockito.when(Files.createDirectories(Paths.get("/dir/NotExist"))).thenReturn(Paths.get("/dir/NotExist"));
// Call target method.
ArsDumpGeneratorUtil.cleanAndCreateDirectories(Paths.get("/dir/NotExist"));
// Check internal-static method call.
PowerMockito.verifyStatic(Mockito.times(1));
Files.createDirectories(Matchers.any());
}

PowerDesigner addin develop

Anyone knows how to develop an add-in for PowerDesigner? I was reading the document of PowerDesigner about how to create an ActiveX Add-in, it says "The ActiveX must implement a specific interface called IPDAddIn to become a PowerDesigner add-in.". But I don't know where the interface IPDAddIn is, and how to implement it ?
Here is the online document
I have this old example, which could give some ideas, even if not everything it up-to-date.
using PdAddInTypLib;
namespace MineSpace
{
[ComVisible(true)]
[Guid("A6FA0D26-77E8-4DD3-B27E-F4050C3D5188")]
public class Launcher : IPdAddIn {
// Main() manages the console or GUI interface
// the PdAddIn interface is managed by an instance of Launcher
[ComVisible(false)]
[STAThread]
public static void Main(String[] args) {
}
public Launcher() {
_app = null;
}
// IPdAddIn implementation
public void Initialize(Object anApplication) {
try {
_app = (PdCommon.Application)anApplication;
}
catch (Exception e) {
// process
}
}
public void Uninitialize() {
}
public String ProvideMenuItems(String aMenu, Object anObj) {
return "";
}
public int IsCommandSupported(String aMenu, Object anObj, String aCommand) {
return 0;
}
public void DoCommand(String aMenu, Object anObj, String aCommand) {
}
private PdCommon.Application _app;
}
}
with the corresponding part in the class declaration:
[HKEY_CLASSES_ROOT\MyPlugin.Launcher]
#="MyPlugin.Launcher"
[HKEY_CLASSES_ROOT\MyPlugin.Launcher\CLSID]
#="{13749EFC-1ADA-4451-8C47-FF0B545FF172}"
[HKEY_CLASSES_ROOT\CLSID\{13749EFC-1ADA-4451-8C47-FF0B545FF172}]
#="MyPlugin.Launcher"
[HKEY_CLASSES_ROOT\CLSID\{13749EFC-1ADA-4451-8C47-FF0B545FF172}\InprocServer32]
#="C:\windows\System32\mscoree.dll"
"ThreadingModel"="Both"
"Class"="MyPlugin.Launcher"
"Assembly"="MyPlugin, Version=1.0.1402.33688, Culture=neutral, PublicKeyToken=null"
"RuntimeVersion"="v1.0.3705"
[HKEY_CLASSES_ROOT\CLSID\{13749EFC-1ADA-4451-8C47-FF0B545FF172}\ProgId]
#="MyPlugin.Launcher"
[HKEY_CLASSES_ROOT\CLSID\{13749EFC-1ADA-4451-8C47-FF0B545FF172}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}]
And the corresponding code to declare the add-in in PowerDesigner. If the File value is present, PowerDesigner could call DllRegisterServer on it, if the component is not yet registered.
[HKEY_LOCAL_MACHINE\SOFTWARE\Sybase\PowerDesigner 10\Addins\MyPlugin Launcher]
"Enable"="No"
"Class"="MyPlugin.Launcher"
"Type"="ActiveX"
"File"="d:\\myplugin\\myplugin.exe"

Immediate JUnit test logging with <junit> Ant Task

I have the following in my build.xml:
<junit fork="yes" printsummary="yes" filtertrace="yes">
<classpath>...</classpath>
<test name="tests.AllTests"/>
<formatter type="plain" usefile="false"/>
</junit>
I would like the JUnit results to be reported for each test as soon as they complete, unfortunately the JUnit task only prints the test results after the whole test case has completed. The test case (AllTests) is rather large, so I have to wait quite a while for the output. Is there any way to make <junit> print individual test results immediately?
I followed dkatzel's suggestion to write my own JUnitResultFormatter. Here is the code for my JUnitFormatter:
package util;
import java.io.OutputStream;
import java.io.PrintStream;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
public class SimpleTestFormatter implements JUnitResultFormatter {
private PrintStream out = System.out;
#Override
public void addError(Test test, Throwable error) {
logResult(test, "ERR");
out.println(error.getMessage());
}
#Override
public void addFailure(Test test, AssertionFailedError failure) {
logResult(test, "FAIL");
out.println(failure.getMessage());
}
#Override
public void endTest(Test test) {
logResult(test, "PASS");
}
#Override
public void startTest(Test test) { }
#Override
public void endTestSuite(JUnitTest testSuite) throws BuildException { }
#Override
public void setOutput(OutputStream out) {
this.out = new PrintStream(out);
}
#Override
public void setSystemError(String err) {
// don't echo test error output
}
#Override
public void setSystemOutput(String out) {
// don't echo test output
}
#Override
public void startTestSuite(JUnitTest testSuite) throws BuildException { }
private void logResult(Test test, String result) {
out.println("[" + result + "] " + String.valueOf(test));
out.flush();
}
}
And this is how I use it in the Ant script:
<junit fork="yes" printsummary="yes" filtertrace="yes">
<classpath>...</classpath>
<test name="tests.AllTests"/>
<formatter classname="util.SimpleTestFormatter" usefile="false"/>
</junit>
Note that the class must be compiled with ant.jar and ant-junit.jar on the classpath.
I believe the built-in JUnit formatters buffer everything until the end. You should be able to write your own formatter implementation which extends org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter so that it flushes to STDOUT immediately, but I've never done it.
You can use this blog where the author makes a custom formatter and shows both the code and how to use it in the build.xml http://shaman-sir.wikidot.com/one-liner-output-formatter
There is also a similar SO question with similar info How do I configure JUnit Ant task to only produce output on failures?

JavaFX - waiting for task to finish

I have a JavaFX application which instantiates several Task objects.
Currently, my implementation (see below) calls the behavior runFactory() which performs computation under a Task object. Parallel to this, nextFunction() is invoked. Is there a way to have nextFunction() "wait" until the prior Task is complete?
I understand thread.join() waits until the running thread is complete, but with GUIs, there are additional layers of complexity due to the event dispatch thread.
As a matter of fact, adding thread.join() to the end of the code-segment below only ceases UI interaction.
If there are any suggestions how to make nextFunction wait until its prior function, runFactory is complete, I'd be very appreciative.
Thanks,
// High-level class to run the Knuth-Morris-Pratt algorithm.
public class AlignmentFactory {
public void perform() {
KnuthMorrisPrattFactory factory = new KnuthMorrisPrattFactory();
factory.runFactory(); // nextFunction invoked w/out runFactory finishing.
// Code to run once runFactory() is complete.
nextFunction() // also invokes a Task.
...
}
}
// Implementation of Knuth-Morris-Pratt given a list of words and a sub-string.
public class KnuthMorrisPratt {
public void runFactory() throws InterruptedException {
Thread thread = null;
Task<Void> task = new Task<Void>() {
#Override public Void call() throws InterruptedException {
for (InputSequence seq: getSequences) {
KnuthMorrisPratt kmp = new KnuthMorrisPratt(seq, substring);
kmp.align();
}
return null;
}
};
thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
When using Tasks you need to use setOnSucceeded and possibly setOnFailed to create a logic flow in your program, I propose that you also make runFactory() return the task rather than running it:
// Implementation of Knuth-Morris-Pratt given a list of words and a sub-string.
public class KnuthMorrisPratt {
public Task<Void> runFactory() throws InterruptedException {
return new Task<Void>() {
#Override public Void call() throws InterruptedException {
for (InputSequence seq: getSequences) {
KnuthMorrisPratt kmp = new KnuthMorrisPratt(seq, substring);
kmp.align();
}
return null;
}
};
}
// High-level class to run the Knuth-Morris-Pratt algorithm.
public class AlignmentFactory {
public void perform() {
KnuthMorrisPrattFactory factory = new KnuthMorrisPrattFactory();
Task<Void> runFactoryTask = factory.runFactory();
runFactoryTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent t)
{
// Code to run once runFactory() is completed **successfully**
nextFunction() // also invokes a Task.
}
});
runFactoryTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent t)
{
// Code to run once runFactory() **fails**
}
});
new Thread(runFactoryTask).start();
}
}

Resources