SWTBot vs. Unit Testing
Asked Answered
B

1

5

We use SWTBot for writing of functional tests. To test some cases is very difficult and some programmers use classes and their methods directly from implementation (for example call methods from class AddUserDialog etc.). Is this good approach? And why?

And next qustion please. It is SWTBot enough for testing of eclipse RCP based application? Is is necessary to write unit tests please?

Note: We are scrum team.

Blockade answered 12/11, 2015 at 20:45 Comment(0)
E
9

SWTBot and JUnit serve two different purposes.

JUnit

As the name implies, JUnit is meant for unit testing. Unit tests should be small and fast to execute. They test only a single unit of code and the above mentioned attributes allow them to be executed often while developing the unit under test.

But there is more to (good) units tests. You may want to read one of the following posts for further attributes of unit tests:

I would go one step further and say that unit tests only make sense in TDD, that is you write the test before the production code. Otherwise you neglect the tests. Who want's to do the extra effort of writing tests for something that already works. And even if you have the discipline to write the tests afterwards, they merely manifest the state of your production code. Whereas, like in TDD, writing tests beforehand leads to lean production code that only does what is required by the tests. But I guess that's something not everyone will agree on.

In an RCP setting, unit tests would ideally be able to run without starting the platform (which takes rather long). I.e. they would not require to be run as PDE JUnit Tests but as plain JUnit Tests instead. Therefore the unit under test should be isolated from the RCP APIs.

On a related note, see also this question: How to efficiently JUnit test Eclipse RCP Plugins

SWTBot

While SWTBot uses the JUnit runtime to execute the tests, it is rather meant as a utility to create integration or functional tests. SWTBot, when used with RCP, starts the entire workbench and runs all tests within the same instance. Therefore great care should be taken to ensure that each test leaves the environment in the same state as it was before the test started. Specialized Rules may help here to set up and tear down a particular recurring scenario.

It is perfectly valid in order to setup an SWTBot test to call methods from your application. For example, you could programmatically open the wizard and then use SWTBot to simulate a user that enters data and presses the OK button. There is no need to use SWTBot to laboriously open the wizard itself.

In my experience, SWTBot is even too much for simple use cases. Consider a test that should enter some data into a dialog and then press OK. If you already have the dialog opened programmatically you can as well continue without SWTBot:

dialog.textField.setText( "data" );
dialog.okButton.notifyListeners( SWT.Selection, null );

assertThat( dialog.getEnteredData() ).isEqualTo( "data" );

Use Both

The best bet is to have both, unit tests that ensure the behavior of the respective units and functional tests that make sure that the particular units play together as desired.

Not sure if that answers the question, if you have further concerns please leave a comment.

Eversion answered 14/11, 2015 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.