JUnit Automated Software Testing Framework Advanced Material Paul

  • Slides: 8
Download presentation
JUnit Automated Software Testing Framework Advanced Material Paul Ammann & Jeff Offutt http: //www.

JUnit Automated Software Testing Framework Advanced Material Paul Ammann & Jeff Offutt http: //www. cs. gmu. edu/~offutt/softwaretest/

Advanced Topics in JUnit • Assertion Patterns – How To Decide If Your Test

Advanced Topics in JUnit • Assertion Patterns – How To Decide If Your Test Passes – State Testing vs. Interaction Testing Patterns • Parameterized JUnit Tests – How to Describe and Run Very Similar Tests • JUnit Theories – Applying the Contract Model to Testing – AAA Model: Assume, Act, Assert – Very Powerful Approach • But Also Still A Work in Progress Introduction to Software Testing (Ch 1) © Ammann & Offutt 2

Assertion Patterns for Unit Tests • State Testing Patterns – Final State Assertion •

Assertion Patterns for Unit Tests • State Testing Patterns – Final State Assertion • Most Common Pattern: Arrange. Act. Assert. – Guard Assertion • Assert Both Before and After The Action (Precondition Testing) – Delta Assertion • Verify a Relative Change to the State – Custom Assertion • Encodes Complex Verification Rules • Interaction Assertions – Verify Expected Interactions – Heavily used in Mocking tools – Very Different Analysis Compared to State Testing • Resource: http: //martinfowler. com/articles/mocks. Arent. Stubs. html Introduction to Software Testing (Ch 1) © Ammann & Offutt 3

Parameterized JUnit Tests • Problem: Testing A Function With Similar Values – How To

Parameterized JUnit Tests • Problem: Testing A Function With Similar Values – How To Avoid Test Code Bloat? • Simple Example: Adding Two Numbers – Adding a Given Pair of Numbers Is Just Like Adding Any Other Pair – You Really Only Want to Write One Test • Parameterized Unit Tests Call Consructor For Each Logical Set of Data Values – Same Tests Are Then Run On Each Set of Data Values – List of Data Values Identified with @Parameters Annotation Introduction to Software Testing (Ch 1) © Ammann & Offutt 4

Parameterized Unit Tests import org. junit. *; import org. junit. runner. Run. With; import

Parameterized Unit Tests import org. junit. *; import org. junit. runner. Run. With; import org. junit. runners. Parameterized. Parameters; import static org. junit. Assert. *; import java. util. *; @Run. With(Parameterized. class) public class Param. Test { public int sum, a, b; public Param. Test (int sum, int a, int b) { this. sum = sum; this. a = a; this. b = b; } @Parameters public static Collection<Object[]> parameters() { return Arrays. as. List (new Object [][] {{0, 0, 0}, {2, 1, 1}}); } @Test public void addition. Test() { assert. Equals(sum, a+b); } } Introduction to Software Testing (Ch 1) © Ammann & Offutt 5

JUnit Theories • These Are Unit Tests With Actual Parameters – So Far, We’ve

JUnit Theories • These Are Unit Tests With Actual Parameters – So Far, We’ve Only Seen Parameterless Test Methods • Contract Model: Assume, Act, Assert – Assumptions (Preconditions) Limit Values Appropriately – Action Performs Activity Under Scrutiny – Assertions (Postconditions) Check Result @Theory public void remove. Then. Add. Does. Not. Change. Set( Set<String> set, String string) { // Parameters! assume. True(set. contains(string)) ; // Assume Set<String> copy = new Hash. Set<String>(set); // Act copy. remove(string); copy. add(string); assert. True (set. equals(copy)); // Assert // // System. out. println(“Instantiated test: “ + set + “, “ + string); } Introduction to Software Testing (Ch 1) © Ammann & Offutt 6

Question: Where Does The Data Come From? • Answer: – All Combinations of Values

Question: Where Does The Data Come From? • Answer: – All Combinations of Values from @Data. Point Annotations Where Assume Clause is True – Four (of Nine) Combinations in This Particular Case – Note: @Data. Point Format is an Array. @Data. Points public static String[] string = {"ant", "bat", "cat"}; @Data. Points public static Set[] sets = { new Hash. Set(Arrays. as. List("ant", "bat")), new Hash. Set(Arrays. as. List(“bat", “cat", “dog“, “elk”)), new Hash. Set(Arrays. as. List(“Snap”, “Crackle”, “Pop")) }; Introduction to Software Testing (Ch 1) © Ammann & Offutt 7

JUnit Theories Need Boiler. Plate import org. junit. *; org. junit. runner. Run. With;

JUnit Theories Need Boiler. Plate import org. junit. *; org. junit. runner. Run. With; static org. junit. Assert. *; static org. junit. Assume. *; import org. junit. experimental. theories. Data. Point; org. junit. experimental. theories. Data. Points; org. junit. experimental. theories. Theories; org. junit. experimental. theories. Theory; import java. util. *; @Run. With(Theories. class) public class Set. Theory. Test { … // See Earlier Slides } Introduction to Software Testing (Ch 1) © Ammann & Offutt 8