Post on 18-Oct-2020
transcript
GRASSFIELD Junit with Eclipse
JUNIT WITH ECLIPSEVersion 0.1
GRASSFIELD.orgMARCH 2008
PUBLIC 1
GRASSFIELD Junit with Eclipse
SOME RIGHTS RESERVEDThis work is licensed under the Creative Commons Attribution 2.5 India License. To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/in or send a letter to Creative Commons, 171
Second Street, Suite 300, San Francisco, California, 94105, USA.
PUBLIC 2
GRASSFIELD Junit with Eclipse
VERSION HISTORYDATE VERSION DESCRIPTION AUTHOR
03/30/08 0.1 Initial Draft Pandian R
PUBLIC 3
GRASSFIELD Junit with Eclipse
1 About this documentThis document can be used as a nutshell guide to write test cases in junit
The audiences are intended to be developers/senior developers and testers. Knowledge of core java, unit testing is mandatory for this document.
I have used Eclipse Platform Version: 3.3.1.1 and junit Version 4.4 to prepare this document.
I may be poor in documenting. Please keep me informed about the grammatical or typo errors.
2 About JUnitJUnit is a successful unit testing framework. Its makes the automated unit testing process easier. Also It is an important tool for test driven development. It can be downloaded from www.junit.org
PUBLIC 4
GRASSFIELD Junit with Eclipse
Table of Contents1About this document............................................................................................................................ 42About JUnit......................................................................................................................................... 41Testing sequence................................................................................................................................. 6
1.1Writing a Testcase........................................................................................................................ 61.2Creating a Test Suite.................................................................................................................. 11
PUBLIC 5
GRASSFIELD Junit with Eclipse
1 Testing sequenceWe need to follow the following procedures for the test automation.
1. Write the test case
2. write the test methods in the test case
3. Prepare the test suite
4. Run the test
1.1 Writing a TestcaseI have written a stand alone java file org.grassfield.junit.MainClass to add, subtract, multiply and divide two integers. Lets now create a test case with Eclipse.
● Create a new test case
PUBLIC 6
Illustration 1: Create a new Test Case
GRASSFIELD Junit with Eclipse
● Select the properties
PUBLIC 7
Illustration 2: Test case properties
GRASSFIELD Junit with Eclipse
● select the class under test
PUBLIC 8
Illustration 3: Selecting the class under test
GRASSFIELD Junit with Eclipse
● select the methods to be tested
● Now the test case template is ready. We need to implement suitable methods correctly for testing.
● setUp() method is used to create the environment to test the scenario. Here i am instantiating proper variables
a=10;b=5;mainClass = new MainClass();
● Implement all the test methods. For example, it checks for the equality between the output of the method and the product.public void testAdd() {
assertTrue(mainClass.add(a, b)==15);}
● Similarly testSubtrace(), testMultiply(), testDivide() methods are implemented.
PUBLIC 9
Illustration 4: selecting the methods to be tested
GRASSFIELD Junit with Eclipse
● Show Junit view. At first you need to enable testing view. Select windows>show view>other>java>junit
PUBLIC 10
Illustration 5: selecting junit view
GRASSFIELD Junit with Eclipse
● Run the test. See some test cases got failed.
PUBLIC 11
Illustration 6: junit view
GRASSFIELD Junit with Eclipse
● Incorrect methods have been rectified. Now all the cases are doing good
1.2 Creating a Test SuiteAs the test cases are being increased day by day, it will be very diffcult to group and maintain them. So, Test suites are useful to combine several tests and run them together. It is realtively simple task. The only thing we need to do is, we need to include a static method in all test classes
public static junit.framework.Test suite(){
return new JUnit4TestAdapter(TestMainClass.class);}
This method will be called by the TestRunner and the corresponding test will be added to suite.
PUBLIC 12
Illustration 7: all test cases passed
GRASSFIELD Junit with Eclipse
To create a test suite, select File>new>other
PUBLIC 13
Illustration 8: test suite wizard
GRASSFIELD Junit with Eclipse
select the test classes to be added to the suite
PUBLIC 14
Illustration 9: adding tests to suite
GRASSFIELD Junit with Eclipse
Here is the test suite. For time being we have added only one test. Any number of tests can be added with this. Similarly, A Testsuite for a group of test suites is also possible.
PUBLIC 15
Illustration 10: running test suite