Professional Documents
Culture Documents
Agenda
Intro to Software Testing Test-Driven Development Writing a Test Plan Test Frameworks
JUnit Visual Studio
Software Testing
Software testing is the process of verifying that a software program works as expected.
Without testing, there s no proof that the software does what it is intended to do
Testing == Quality
Testing should be incorporated into the development process from the beginning
The need to test your software will impact how you develop it The earlier you find a problem, the easier it is to fix
Cost of fixing a defect depending on the stage it was found
Time Introduced Requirements Design Implementation Time Detected Requirements Design Implementation QA Post-Release
1x -
3x 1x -
5-10x 10x 1x
Program Manager
Writes functional spec that enables tester to write a test plan Reviews the test plan
Test Engineer
Writes test plan Writes and executes test cases, files bugs Signs off on final product
Business Stakeholder
Participates in internal alpha testing before the software is released
Customer
The person who is ultimately affected if the code was not well-tested May participate in beta testing or perform useracceptance testing
Testing Methods
The box analogy describes the point of view the engineer takes when testing
Tester has access to internal data structures and algorithms
White Box
API testing Code coverage Fault injection
Black Box
no knowledge of
Grey Box
Tester has knowledge of internal data structures and algorithms, but tests at the user/black-box level
Integration testing (between two modules of code at the interface level) Reverse engineering
System Integration Testing Testing that a system is integrated to an external or 3rd party system Each system has already passed system testing
Test-Driven Development
A development technique that relies on the concept of writing the test cases before the product code
Validates that spec and requirements are well-understood Test cases will initially fail Developer writes code to make them pass
TDD Workflow
1. (Re)write a test Test succeeds
Repeat
2. Run all tests & see if the new one fails 3. Write some code Test(s) fail
Test fails
Clean minimal writing code Write up the code as necessary Always start byproduct a testto If all test fail pass, alland Write some code to make run to achieve production tested make the testbecause quality Test mustcasescompile the the requirements test duplication, test pass that are met Focus on Validates removingharness works feature isn t implemented (if it succeeds,test itself: for the If they fail,the need between May the duplicationsuboptimal; including keep iterating Testsbe inelegant ormake sure it feature improve it later code Assuming the code we willt is obviated) new test & pass without doesn producttests are comprehensive,cases to to To write a test passthe onfully Make the test, for must no the Re-run the fail move expected Test shouldtest you only,ensure final refactoring functionality understand the featureis testing extra ensure that it s that stage reasonuntested isn t breaking specification and requirements any functionality the right thing
5. Refactor code
Test Strategy
Describe the testing methodology you plan to use
E.g. white box, black box, grey box, or a combination
Test Strategy We plan to employ mostly white-box unit tests in order to test the application, using Visual Studio s built-in unit-testing framework. Each of the following major classes will have at least one unit test: Game GameBoard Player GameStatistics
2.2
Move 2 fails
2.3
Move 3 fails
Testing Frameworks
JUnit is a unit-testing framework for Java
Developed by the same people who pioneered TDD Uses source code annotations to decorate special methods to be run by the test harness Integrated with Java IDEs like Eclipse and JCreator
Test Methods
JUnit
Annotate test case methods with @Test Use methods from org.junit.Assert to check your test conditions or fail the test case
import org.junit.Test;
public class AdditionTest { private int x = 1; private int y = 1; @Test public void testAdd() { int z = this.x + this.y; assertEquals(2, z); } }
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Visual Studio
Annotate test case methods with <TestMethod()> Use methods from the Assert class to check your test conditions or fail the test case
<TestClass()> Public Class AdditionTest Private x As Integer = 1 Private y As Integer = 1 <TestMethod()> Public Sub TestAdd () Dim z As Integer = Me.x + Me.y Assert.AreEqual(2, z) End Sub End Class
Further Reading
TDD
Test-Driven Development: By Example (Google Books) testdriven.com
JUnit
JUnit Cookbook JUnit Javadoc An early look at JUnit 4
APPENDIX
Testing Exceptions
It is easy to test for expected exceptions
Annotate your test with the expected exception If the exception isn t thrown (or a different one is), the test will fail
@Test(expected=ArithmeticException.class) public void divideByZero() { int n = 2 / 0; }
Limitation: if you need to test the exception s message or other properties, use a different structure:
@Test public void divideByZero() { try { int n = 2 / 0; fail("Divided by zero"); } catch (ArithmeticException(success) { assertNotNull(success.GetMessage()); } }
Timed Tests
Simple performance bench-marking:
@Test(timeout=500) public void retrieveAllElementsInDocument() { doc.query("//*"); }
Network testing:
@Test(timeout=2000) public void remoteBaseRelativeResolutionWithDirectory() throws IOException, ParsingException { builder.build("http://www.ibiblio.org/xml"); }