240 likes | 255 Views
Learn about unit testing in software engineering, including terminology and an introduction to JUnit. Explore the objectives of testing, testing personnel, testing activities, and the cycle of testing activities. Discover the XP approach to testing and the steps of test-driven development.
E N D
CS352Software Engineering IILecture 16: Unit Testing Presented by: Mohammad El-Ramly, PhD http://www.acadox.com/join/24VZEM http://www.acadox.com/class/3962
Outline • Test Organization • Unit Testing Terminology • Intro to JUnit
SE Topics to Cover Yourself • Professional Ethics • SW Project Management • Estimation • Contracts • Licenses • Economics • Metrics • Human Computer Interaction / UI Design • SW Maintenance
Testing: Objectives • Uncover the errors (defects) in the software, including errors in: • requirements from requirement analysis. • design documented in design specifications. • coding (implementation). • system resources and system environment. • hardware problems and their interfaces to software.
Testing: Personnel • Test Manager: • Manage, supervise, and control a software test project. • Define and specify a test plan. • Software Test Engineers and Testers: • Define test cases, write test specifications, run tests. • Development Engineers: • Only perform unit tests and integration tests. • Quality Assurance Group and Engineers: • Perform system testing. • Define software testing standards and quality control process. • Independent Test Group.
Testing: Activities • Test Planning: • a test schedule for a test process and its activities, as well as assignments. • test requirements and items. • test strategy and supporting tools. • Test Design and Specification: • conduct software design based on well-defined test generation methods. • specify test cases to achieve a targeted test coverage.
Testing: Activities • Test Set up: • Testing Tools and Environment Set-up. • Test Suite Set-up. • Test Operation and Execution: • Run test cases manually or automatically. • Test Result Analysis and Reporting: • Report software testing results and conduct test result analysis. • Problem Reporting: • Report program errors using a systematic solution.
Testing: Activities • Test Management and Measurement: • Manage software testing activities, control testing schedule, measure testing complexity and cost. • Test Automation: • Define and develop software test tools. • Adopt and use software test tools. • Write software test scripts. • Test Configuration Management: • Manage and maintain different versions of software test suites, test environment and tools, and documents for various product versions.
TestCases TestResults Specification Design Error Repair PerformTest LocateError RepairError retest Testing: Cycle of Activities • Testing usually involves repetition of testing activities. • Example:
Unit Testing Terminology 15 • A test fixture sets up the data (both objects and primitives) that are needed to run tests • Example: If you are testing code that updates an employee record, you need an employee record to test it on • A unit test is a test of a single class • A test case tests the response of a single method to a particular set of inputs • A test suite is a collection of test cases • A test runner is software that runs tests and reports results • An integration test is a test of how well classes work together • JUnit provides some limited support for integration tests 4 1 14 16 6
test runner another unit test test case (for one method) another test case another unit test another test case another test case another test case Once more, in pictures test suite • A unit test tests the methods in a single class • A test case tests (insofar as possible) a single method • You can have multiple test cases for a single method • A test suite combines unit tests • The test fixture provides software support for all this • The test runner runs unit tests or an entire test suite • Integration testing (testing that it all works together) is not well supported by JUnit unit test (for one class) test case (for one method) another test case test fixture
XP approach to testing • In the Extreme Programming approach, • Tests are written before the code itself • If code has no automated test case, it is assumed not to work • A test framework is used so that automated testing can be done after every small change to the code • This may be as often as every 5 or 10 minutes • If a bug is found after development, a test is created to keep the bug from coming back • Consequences • Fewer bugs • More maintainable code • Continuous integration—During development, the program always works—it may not do everything required, but what it does, it does right
Test Driven Development Steps • Write a test. • See it fail. • Make it work. • Refactor. • Update. • Commit. • Pair programming: Switch roles often!
JUnit is the Most Used Java Library • A research survey performed in 2013 across 10,000 GitHub projects found that JUnit, along with slf4j-api, are the most popular libraries. Each library was used by 30.7% of projects. • Guava Google library is very popular: • Basics, Collections, Concurrency, Strings, Caches, Hashing, Math, Reflection, etc.
Other Testing Tools • TestingNG Unit & Integration Testing • dJUnit Coverage Testing • Twsit User / Acceptance Testing • Jbehave User / Acceptance Testing • ……..Many Others
TestingNG http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/
Java Annotations • Are instructions to the compiler to act in a certain way. • This example @Override annotation Checks that the method is an override. Causes a compile error if the method is not found in one of the parent classes or implemented interfaces. • Good article on writing your annotations • http://www.mkyong.com/java/java-custom-annotations-example/
Java Annotations • Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. • Annotations are meta-tags that you can add to your code and apply to package declarations, type declarations, constructors, methods, fields, parameters and variables.
Java Annotations public class Animal { public void speak() { } public String getType() { return "Generic animal"; } } public class Cat extends Animal { @Override public void speak() { // good override. System.out.println("Meow."); } @Override public String gettype() { // Compile-time error return "Cat"; } }
JUnit Annotations @Before @BeforeClass @After @AfterClass @Test @Ignore @Test(timeout=500) @Test(expected = IllegalArgumentException.class) • http://javarevisited.blogspot.com/2012/06/junit4-annotations-test-examples-and.html
JUnit Assertions • void assertEquals (boolean expected, boolean actual) Check that two primitives/Objects are equal • void assertTrue (boolean expected, boolean actual)Check that a condition is true • void assertFalse (boolean condition) Check that a condition is false • void assertNotNull (Object object) Check that an object isn't null. • void assertNull (Object object)Check that an object is null
JUnit Assertions • assertSame (boolean condition) • The assertSame() methods tests if two object references point to the same object • void assertNotSame (boolean condition) • The assertNotSame() methods tests if two object references not point to the same object • void assertArrayEquals (expectedArray, resultArray); • The assertArrayEquals() method will test whether two arrays are equal to each other.