Testing
Testing
Testing
Your Expectations ?
Types of Testing
Activities Based:
Unit Testing
Functional Testing
Regression Testing
Functional Regression
Unit Regression
Break Testing / Monkey Testing
Usability Testing
UI Testing
OS/Browser Testing
Performance Testing
Project Phase Based:
Build Testing
Integration Testing
Stabilization Testing
User Acceptance Testing
Pilot / Beta
Types of Testing … (continued)
Project
Design
Live !!
Integration /
Development
Stabilization
UAT Pilot / Beta
Build Testing
Warranty Support
Unit Testing
Functional Testing
Performance Testing
Unit Testing
Functional Testing
Treats the system as a black box and ensure that the software works as a whole
Test the system by providing know inputs and ensure that it returns
expected outputs
Tests are done at a scenario / flow level
Done post implementation by a QA team
Can be manual or automated
The ‘scenarios’ can be at different levels (depending on the phase of the project)
Interaction of all layers in a given module
Interaction between two / three related modules
Business flows / scenarios involving multiple modules in the system
Regression Testing
Bugs have a way of creeping into the systems and there are many ways and many
reasons why they occur
When a particular bug gets fixed….there is always a chance that this particular fix
could potentially break some other code (either in the same module or other
related module)
This could happen because the developer who fixes a bug might not
understand all the intricacies of the system
Or developer could be careless while fixing
The testing done by a developer after fixing a bug, to ensure that he/she has not
‘introduced’ a bug else where… is called Regression Testing
Note, Regression testing can be done a ‘Unit’ level and/or at a ‘Functional’ level
It is usually a combination of these two types of tests
Build Testing
Testing that is done right after a regular build is completed and released to a ‘test’
environment is called Built Testing
Build testing may include unit testing / functional testing / regression testing
Testing done by the Infrastructure / CM team, before, releasing the new build to
other is called ‘Sanity Test’
The purpose of this test is to ensure that the build is done correctly and
there are no configuration issues
Infra team does some sanity tests like logging into the site, app can access
the DB, browsing to see if main pages are accessible, test if emails are
being sent etc
Break Testing
UI Testing:
User Interface Testing tests the system to ensure that it meets all the UI
specifications/standards
Just like code has coding standards, applications do have UI standards for
the look and feel of the site
CSS, font size, colours, lengths, button style, language etc
OS / Browser Testing:
Testing the application on different OS / Browser combinations to ensure that the
app works from a functionality as well as a look and feel perspective
Performance Testing
Project
Design
Live !!
Integration /
Development
Stabilization
UAT Pilot / Beta
Build Testing
Warranty Support
Unit Testing
Functional Testing
Performance Testing
Business Design
Functional Functional
Specification Testing
Tech Design
Technical Integration
Specification Testing
Detailed Tech
Program
Design
Unit Testing
Specification
Development
CODE
Manual Testing
Test Scripts
The general term used is ‘Test Scripts’ and any given test script might have
multiple ‘Test Cases’
EXERCISE 1
Writing a java class to manually test the following classes that have been
provided:
IAccount.java
Account.java
AccountInsufficientFundsException.java
How was the unit test ?
What are its drawback ?
How do we improve on it ….. use JUnit Framework.
What is JUnit ?
Open source regression testing framework for Java, written by Erich Gamma and Kent
Beck
Part of XUnit family (HTTPUnit, CppUnit, Cactus etc)
Consists of
Base classes for definition of tests and test suites
Tools for running tests (graphical and text-based)
Used to write and run repeatable unit tests for Java objects ( Not EJB, JSP,
Servlets)
Why JUnit ?
JUnit tests allow you to write code faster while increasing quality
JUnit is elegantly simple
JUnit tests check their own results and provide immediate feedback (Green / Red
Status Bar)
JUnit tests can be composed into a hierarchy of test suites
Writing JUnit tests is inexpensive
JUnit tests increase the stability of software
JUnit tests are developer tests
JUnit tests are written in Java
JUnit is free !!
Philosophy :
Let developers write unit tests
Make it easy and painless
So that they test early and test often !!!
assertion in JPL
An assertion is a statement that enables you to test your assumptions about your
program
Each assertion contains a boolean expression that you believe will be true when
the assertion executes.
If it is not true, the system will throw an error
By verifying that the boolean expression is indeed true, the assertion confirms
your assumptions about the behavior of your program, increasing your confidence
that the program is free of errors.
Experience has shown that writing assertions while programming is one of the
quickest and most effective ways to detect and correct bugs.
As an added benefit, assertions serve to document the inner workings of your
program, enhancing maintainability.
Syntax:
assert <boolean expression>
OR
assert <boolean expression> : <string expression>
In the first statement, you only get a AssertError, where as in the second one, the
AssertError that is generated will contain the <string_expression>
You will have to enable assertion via a jvm argument:
“-enableassertions” or “-ea”
Demo AssertionDemo.java
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html
TestCase
assertXYZ()
assertTrue(boolean condition)
assertTrue(java.lang.String message, boolean condition)
assertNotNull(java.lang.Object object)
assertNotNull(java.lang.String message, java.lang.Object object)
assertNull(java.lang.Object object)
assertNull(java.lang.String message, java.lang.Object object)
fail()
fail(java.lang.String message)
EXERCISE 2
Assertions are used to check for the possibility of success and failures, therefore
success and failures within actual code should be anticipated and tested
appropriately
Positive vs. Negative testing
Failed assertions will result in JUnit failures
Uncaught errors/exceptions result in JUnit errors
Errors are unanticipated problems (within actual code or in a test case) resulting
in uncaught exceptions being propagated to a JUnit test method
Both failures and errors will cause the test to fail, the behaviour of a failed test
within a test suite is up to the developer
Ideally there should be zero errors and failures in the entire test suite
Fixtures
Using a test fixture avoids duplicating the test code that is necessary to initialize
and cleanup objects that are common for each test within a given TestCase
A test fixture is useful if you have two or more tests that use a common set of
objects
Used to insure there are no side effects between tests
Enforce the test independence rule, test execution order is not guaranteed
Override setUp() method to initialize the variables
Override tearDown() to release any permanent resources allocated in setUp()
File Pointers, DB connections, sockets etc
EXERCISE 3
EXERCISE 4
Handling Exceptions that are thrown in code by writing positive and a negative
test cases
Test Suites
EXRCISE 5
One Test Case for each Java Class and each public method in the class should
have 1…n textXYZ methods
TestClass naming: Test<className>.java
Test method naming convention: textXYZ()
Test each class in isolation. Each test should test only one class, not indirectly its
collaborators.
Do not use the constructor of the Test Case to set up data for the tests…use the
setUp() method
Each test method should be independent of each other. One should not assume a
certain ‘state’ that will be build by the other test method
Each test method should test only one specific flow through a method… if the test
method tests multiple flows, then it is really not unit testing
Tests should be self verifying …. each and every test should run automatically
and should assert ….no manual intervention
Be careful while writing test cases that changes the state of data, which might
affect other test cases
Changing System data by a test case
Do not load data from hard-coded locations on a filesystem
FileInputStream inp ("C:\\TestData\\dataSet1.dat"); in the setUp() method
As far as possible, use the assertXYZ() methods with the message option, so that
you get a good user friendly message in case of an error
Running the entire suite of test cases for any given system should not take
hours….keep them simple and make them fast
Good news is that, all the other frameworks mentioned above are build on top of
JUnit….so if you know JUnits then you already have a head start in understanding
other frameworks
Test Coverage
Test coverage provides a measure of how well your test suite actually test the
product i.e if each and every line of the actual code that has been written is
executed, by running the entire test suite, then you have 100% coverage
Higher the % of test coverage, better the ‘quantity’ of testing and hopefully
quality also increases
Test coverage tools give you these metrics…using which you can decide if you
need to write more test cases….and for which part/modules do you need to write
them or which parts of the code are being tested multiple times
Resources
JUnit
http://www.junit.org/index.htm
Cactus
http://jakarta.apache.org/cactus
Installation
unzip the junit.zip file
add junit.jar to the CLASSPATH. For example: set classpath=%classpath
%;INSTALL_DIR\junit3\junit.jar
Testing
Test the installation by using either the batch or the graphical TestRunner tool to run the
tests that come with this release. All the tests should pass OK.
for the batch TestRunner type:
java junit.textui.TestRunner junit.samples.AllTests
for the graphical TestRunner type:
java junit.awtui.TestRunner junit.samples.AllTests
for the Swing based graphical TestRunner type:
java junit.swingui.TestRunner junit.samples.AllTests
Notice:
The tests are not contained in the junit.jar but in the installation directory
directly. Therefore make sure that the installation directory is on the class
path
Important:
Don't install the junit.jar into the extension directory of your JDK
installation.
If you do so the test class on the files system will not be found.