0% found this document useful (0 votes)
16 views19 pages

Automation With JUnit

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views19 pages

Automation With JUnit

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Outline JUnit JUnit through examples

Software Test Automation: JUnit as an example

Meenakshi D’Souza

International Institute of Information Technology


Bangalore.
Outline JUnit JUnit through examples

Test automation

Test automation is the second step after test case design.


Test cases designed need to be automated to meet the RIPR
model requirements.
Need to give prefix values towards reachability and postfix
values to ensure propagation.
Test automation is the process of controlling the execution of
tests, the comparison of actual outcomes to expected
outcomes, the setting up of test preconditions, and other test
control and test reporting functions.
Outline JUnit JUnit through examples

Test case: In detail

Recap: A typical test case contains test inputs and expected


outputs.
Prefix values: Inputs necessary to put the software into the
appropriate state to receive the test case values.
Postfix values: Any inputs that need to be sent to the
software after the test case values are sent.
Verification Values: Values needed to see the results of the
test case values.
Exit Values: Values or commands needed to terminate the
program or otherwise return it to a stable state.
Outline JUnit JUnit through examples

Test case: Putting it all together

A test case ready for execution includes

The test case values, prefix values, postfix values, and


expected results necessary for a complete execution and
evaluation of the software artifact under test.
Next step is to get the executable test script which is

A test case that is prepared in a form to be executed


automatically on the test software and produce a report.
Outline JUnit JUnit through examples

What is JUnit?

Open source Java testing framework used to write and run


repeatable automated tests.
An instance of xUnit architecture for unit testing frameworks.
Available from: junit.org.
A structure for writing test drivers.
JUnit can be used as stand alone Java programs (from the
command line) or within IDEs such as Eclipse, IntelliJ IDEA,
NetBeans and Visual Studio Code.
Outline JUnit JUnit through examples

JUnit features

Assertions for testing expected results.


Test features for sharing common test data.
Test suites for easily organizing and running tests.
Graphical and textual test runners.
Outline JUnit JUnit through examples

Tests in JUnit

JUnit can be used to test


An entire object,
Part of an object: a method or some interacting methods,
Interaction between several objects.
It is primarily intended for unit and integration testing, not
system testing.
Each test is embedded into one test method.
A test class contains one or more test methods. Test classes
include:
A collection of test methods.
Methods to set up the state before and update the state after
each test and before and after all tests.
Outline JUnit JUnit through examples

Writing tests for JUnit

Need to use the methods of the junit.framework.assert


class.
Each test method checks a condition (assertion) and reports
to the test runner whether the test failed or succeeded.
The test runner uses the result to report to the user.
All the methods return void.
A few representative methods of junit.framework.assert:
assertTrue(boolean)
assertTrue(string, boolean)
fail(string)
Outline JUnit JUnit through examples

Text fixtures in JUnit

A test fixture is the state of the test.


Objects and variables that are used by more than one test.
Initializations (prefix values).
Reset values (postfix values).
Different tests can use the objects without sharing the state.
Objects used in test fixtures should be declared as instance
variables.
They should be initialized in a @BeforeEach/@BeforeAll
method.
Can be deallocated or reset in an @AfterEach/@AfterAll
method.
Outline JUnit JUnit through examples

A simple example

Consider the code for addition below:

public
class Calc
{
static public int calculator.add(int a, int b);
{
return a+b;
}
}
Outline JUnit JUnit through examples

A simple example

import static org.junit.jupiter.api.Assertions.assertEquals;


import example.util.Calculator;
import org.junit.jupiter.api.Test;
class MyFirstJUnitJupiterTests {
private final Calculator calculator = new Calculator();
@Test
void addition() {
assertEquals(5, calculator.add(2, 3));
}
}

2 and 3 are test inputs, 5 is the expected output.


The assertEquals fails if the actual output is different from the
expected output.
Outline JUnit JUnit through examples

MinTest Class

The following constitute the test class for testing the Min Class:
Standard imports for all JUnit classes:
import static org.junit.Assert.*;
import org.junit.*;
import java.util.*;
Outline JUnit JUnit through examples

MinTest Class contd.

Test fixture and pre-test setup method (prefix):


private List<String> list; // Test fixture
// Set up - Called before every test method.
@BeforeEach
public void setUp()
{ list = new ArrayList<String>() };
Post test teardown method (postfix):
// Tear down - Called after every test method.
@AfterEach
public void tearDown()
{ list = null; // redundant in this example }
Outline JUnit JUnit through examples

Min Test Cases: NullPointer Exception

A test case that uses the fail assertion.

@Test public void testForNullList()


{
list = null;
try {
Min.min (list);
} catch (NullPointerException e)
{
return;
}
fail (NullPointerException expected);
}
Outline JUnit JUnit through examples

Min Test Cases: Empty element

This is for the special case of an empty element.

@Test (expected = NullPointerException.class)


public void testForSoloNullElement()
{
list.add (null);
Min.min (list);
}
Outline JUnit JUnit through examples

Other tests with JUnit

Data-driven tests: Data-driven unit tests call a constructor for


each collection of test values.
Same tests are then run on each set of data values.
Collection of data values defined by method tagged with
@Parameters annotation.
Helps to test a function with multiple test values.
Outline JUnit JUnit through examples

JUnit: Other features

We have highlighted only certain features of JUnit towards


illustrating its use in test automation.
JUnit is a full-fledged tool with several other automation
features.
Widely used in industry.
Outline JUnit JUnit through examples

What next?

Whatever test cases we use for automation, where do they


come from?
They need to be designed.
Criteria are a useful way for designing test cases.
Outline JUnit JUnit through examples

Credits

Part of the material used in these slides are derived from the
presentations of the book Introduction to Software Testing, by
Paul Ammann and Jeff Offutt.

You might also like