Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » Problem with Testlistener in headless Mode
Problem with Testlistener in headless Mode [message #512898] Mon, 08 February 2010 05:23 Go to next message
speedprincess is currently offline speedprincess
Messages: 5
Registered: February 2010
Junior Member
Hello,

First I like SWTBot, because it has a great api for do gui tests for rcp applications.

Now to my problem. During the test runs I want to collect the testresults to create a testprotocoll in a special style. I use eclipse 3.4.2 and junit 4.3.1

Therefor I have create a class Junit4TestListener, which extends RunListener. In this class I override all Methodes, currentla with simple system.out messages. Because I want to know, when the will be call.

In addition I have a Testrunner which extends the SWTBotApplicationLauncherClassRunner. In this class I register my listener.

In my testclass I add the line @RunWith(MyJunitTestRunner.class).

When I now run my tests in the eclipse IDE. The methode testRunStarted(), which should be called at the beginning of the test, will not be called. But that is ok. I can use the constructor instaed of this methode.

The problem ist when I run this test headless with ant. Because headless, the methode testRunFinished(), which should be called at the end of my tests, will not be called. So I can not finish my testprotocoll.

I add the code of my testclass, the listener and the runner.

Please can anybody try this in a plug-in and say whats the mistake. Or why the methode testRunFinished at the end of the test will not be invoked by a headless test.

Code Testlistener
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

public class Junit4TestListener extends RunListener {

	@Override
	public void testFailure(Failure failure) throws Exception {
		System.out.println("testFailure");
		super.testFailure(failure);
	}

	@Override
	public void testFinished(Description description) throws Exception {
		System.out.println("testFinished");
		super.testFinished(description);
	}

	@Override
	public void testRunFinished(Result result) throws Exception {
		System.out.println("testRunFinished");
		super.testRunFinished(result);
	}

	@Override
	public void testRunStarted(Description description) throws Exception {
		System.out.println("testRunStarted");
		super.testRunStarted(description);
	}

	@Override
	public void testStarted(Description description) throws Exception {
		System.out.println("testStarted");
		super.testStarted(description);
	}
}


Code TestRunner
import org.eclipse.swtbot.swt.finder.junit.SWTBotApplicationLauncherClassRunner;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;

public class MyJunitTestRunner extends SWTBotApplicationLauncherClassRunner {

	private static RunListener testListener = new Junit4TestListener();

	public MyJunitTestRunner(Class<?> klass) throws Exception {
		super(klass);		
	}

	@Override
	public void startApplication() {
	}
	
	public void run(RunNotifier notifier) {
		notifier.removeListener(testListener); 
		notifier.addListener(testListener);		
		super.run(notifier);		
	}

}


Code for the testclass
import static org.junit.Assert.assertNotNull;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import de.xcom.swtbot.headless.junit.MyJunitTestRunner;


@RunWith(MyJunitTestRunner.class)
public class BaseTest {
	
	 static SWTWorkbenchBot bot = new SWTWorkbenchBot();
	 
	 @BeforeClass
	 public static void setUp()
	 {
		 bot = new SWTWorkbenchBot();
		 try{
			 bot.viewByTitle("Welcome").close();			 
		 } catch (WidgetNotFoundException e) {			
		}
	 }
	 
	 @Test
	 public void openView() throws Exception
	 {
		 bot.viewByTitle("Outline").close();
		 bot.menu("Window").menu("Show View").menu("Outline").click();
		 assertNotNull(bot.viewByTitle("Outline"));
	 }


Re: Problem with Testlistener in headless Mode [message #513026 is a reply to message #512898] Mon, 08 February 2010 10:38 Go to previous messageGo to next message
Ketan Padegaonkar is currently offline Ketan Padegaonkar
Messages: 873
Registered: July 2009
Senior Member
Hi,

You should be extending from SWTBotJunit4ClassRunner and not
SWTBotApplicationLauncherClassRunner.

SWTBotApplicationLauncherClassRunner is (mostly) for plain swt
applications that need to be launched in another application.

-- Ketan

On 2/8/10 2:23 AM, speedprincess wrote:
> Hello,
>
> First I like SWTBot, because it has a great api for do gui tests for rcp
> applications.
>
> Now to my problem. During the test runs I want to collect the
> testresults to create a testprotocoll in a special style. I use eclipse
> 3.4.2 and junit 4.3.1
>
> Therefor I have create a class Junit4TestListener, which extends
> RunListener. In this class I override all Methodes, currentla with
> simple system.out messages. Because I want to know, when the will be call.
>
> In addition I have a Testrunner which extends the
> SWTBotApplicationLauncherClassRunner. In this class I register my listener.
>
> In my testclass I add the line @RunWith(MyJunitTestRunner.class).
> When I now run my tests in the eclipse IDE. The methode
> testRunStarted(), which should be called at the beginning of the test,
> will not be called. But that is ok. I can use the constructor instaed of
> this methode.
>
> The problem ist when I run this test headless with ant. Because
> headless, the methode testRunFinished(), which should be called at the
> end of my tests, will not be called. So I can not finish my testprotocoll.
> I add the code of my testclass, the listener and the runner.
> Please can anybody try this in a plug-in and say whats the mistake. Or
> why the methode testRunFinished at the end of the test will not be
> invoked by a headless test.
>
> Code Testlistener
> import org.junit.runner.Description;
> import org.junit.runner.Result;
> import org.junit.runner.notification.Failure;
> import org.junit.runner.notification.RunListener;
>
> public class Junit4TestListener extends RunListener {
>
> @Override
> public void testFailure(Failure failure) throws Exception {
> System.out.println("testFailure");
> super.testFailure(failure);
> }
>
> @Override
> public void testFinished(Description description) throws Exception {
> System.out.println("testFinished");
> super.testFinished(description);
> }
>
> @Override
> public void testRunFinished(Result result) throws Exception {
> System.out.println("testRunFinished");
> super.testRunFinished(result);
> }
>
> @Override
> public void testRunStarted(Description description) throws Exception {
> System.out.println("testRunStarted");
> super.testRunStarted(description);
> }
>
> @Override
> public void testStarted(Description description) throws Exception {
> System.out.println("testStarted");
> super.testStarted(description);
> }
> }
>
>
> Code TestRunner
> import
> org.eclipse.swtbot.swt.finder.junit.SWTBotApplicationLaunche rClassRunner;
> import org.junit.runner.notification.RunListener;
> import org.junit.runner.notification.RunNotifier;
>
> public class MyJunitTestRunner extends
> SWTBotApplicationLauncherClassRunner {
>
> private static RunListener testListener = new Junit4TestListener();
>
> public MyJunitTestRunner(Class<?> klass) throws Exception {
> super(klass);
> }
>
> @Override
> public void startApplication() {
> }
>
> public void run(RunNotifier notifier) {
> notifier.removeListener(testListener); notifier.addListener(testListener);
> super.run(notifier);
> }
>
> }
>
>
> Code for the testclass
> import static org.junit.Assert.assertNotNull;
> import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
> import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundExcep tion;
> import org.junit.BeforeClass;
> import org.junit.Test;
> import org.junit.runner.RunWith;
> import de.xcom.swtbot.headless.junit.MyJunitTestRunner;
>
>
> @RunWith(MyJunitTestRunner.class)
> public class BaseTest {
>
> static SWTWorkbenchBot bot = new SWTWorkbenchBot();
> @BeforeClass
> public static void setUp()
> {
> bot = new SWTWorkbenchBot();
> try{
> bot.viewByTitle("Welcome").close(); } catch (WidgetNotFoundException e) {
> }
> }
> @Test
> public void openView() throws Exception
> {
> bot.viewByTitle("Outline").close();
> bot.menu("Window").menu("Show View").menu("Outline").click();
> assertNotNull(bot.viewByTitle("Outline"));
> }
>
>
Re: Problem with Testlistener in headless Mode [message #513050 is a reply to message #513026] Mon, 08 February 2010 11:39 Go to previous messageGo to next message
speedprincess is currently offline speedprincess
Messages: 5
Registered: February 2010
Junior Member
Hello Ketan,

Thanks for the reply, but unfortunately thats not the solution for my problem. I still get only the messages for start and finished a testMethode but nothing for the whole test.

That are the messages by running in eclipse IDE:
Quote:
testStarted
log4j:WARN No appenders could be found for logger (org.eclipse.swtbot.swt.finder.matchers.AbstractMatcher).
log4j:WARN Please initialize the log4j system properly.
testFinished
testRunFinished



And that by running in headless Mode
Quote:
Testsuite: de.xcom.swtbot.headless.test.BaseTest
testStarted
testFinished
Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 2,441 sec

Testcase: openView took 1,135 sec


Thats my new Testrunner class
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;

public class JunitTestlistenerRunner extends SWTBotJunit4ClassRunner {

	private static RunListener testListener = new Junit4TestListener();

	public JunitTestlistenerRunner(Class<?> klass) throws Exception {
		super(klass);
	}
	
	public void run(RunNotifier notifier) {
		notifier.removeListener(testListener); 
		notifier.addListener(testListener);
		super.run(notifier);
	}
}


I dont unterstand whats the difference by running in the IDE.
Re: Problem with Testlistener in headless Mode [message #516802 is a reply to message #513050] Thu, 25 February 2010 04:02 Go to previous messageGo to next message
Ajay Tiwari is currently offline Ajay Tiwari
Messages: 9
Registered: February 2010
Junior Member
Hi,

I also got the same problem when i run swtbot in IDE.Actually problem is in menifiest file.You should mention Eclipse-RegisterBuddy:org.apache.log4j in menifiest file after placing the log4j.xml file under src directory

-Ajay
Re: Problem with Testlistener in headless Mode [message #516811 is a reply to message #512898] Thu, 25 February 2010 04:29 Go to previous message
speedprincess is currently offline speedprincess
Messages: 5
Registered: February 2010
Junior Member
Hello,

Thanks for the answer, but the 2 log4j warnings are not the problem.
If I add correct this, I have always the same problem. Headless the methode runFinished wont be invoked, and so my testprotocoll cant finished.

Can anybody try this code and give my an advice, what I can do?

Or is there another chance to call a special methode at the end of the tests. Can I override some methode in the runner, to do this?

What is the difference between the swtbotRunner and the eclipse junit runner, that by running in the ide the methode will be invoke and in the headless mode not.

Previous Topic:Setting a new value in a treeItem.cell
Next Topic:MenuItem disposed
Goto Forum:
  


Current Time: Thu Aug 21 22:01:20 EDT 2014

Powered by FUDForum. Page generated in 0.01566 seconds