0% found this document useful (0 votes)
20 views

week 14 slides

The document discusses the importance of unit testing in software development, highlighting its role in improving software design, reducing debugging time, and ensuring better code quality. It outlines the testing process, including when to start testing, the types of tests, and the use of frameworks like JUnit for efficient testing. Additionally, it covers test case design, the distinction between black box and white box testing, and the structure of test methods in JUnit.

Uploaded by

bc210401244ama
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

week 14 slides

The document discusses the importance of unit testing in software development, highlighting its role in improving software design, reducing debugging time, and ensuring better code quality. It outlines the testing process, including when to start testing, the types of tests, and the use of frameworks like JUnit for efficient testing. Additionally, it covers test case design, the distinction between black box and white box testing, and the structure of test methods in JUnit.

Uploaded by

bc210401244ama
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Why?

 Why testing?
• Improve software design
• Make software easier to understand
• Reduce debugging time
• Catch integration errors
 In short, to Produce Better Code
 Preconditions
• Working code
• Good set of unit tests

Unit testing with JUnit


What should be tested ?

 Test for boundary conditions


 Test for both success and failure

 Test for general functionality

 Etc..

Unit testing with JUnit


When to start testing

Software quality and testing is a


life-cycle process

Unit testing with JUnit


When to start testing...

 At the time of starting the projects


 How we start the projects ??

 Do we have any formal way ??

Unit testing with JUnit


The V-model of development

Requir ements System System Detailed


specification specification design design

System Sub-system Module and


Acceptance
integration integration unit code
test plan
test plan test plan and tess

Acceptance System Sub-system


Service
test integration test integration test

Unit testing with JUnit


Fact of testing

Testing does not guarantee


the absence of defects

Unit testing with JUnit


What is test case

 A test case is a document that


describes an input, action, or event and
an expected response, to determine if a
feature of an application is working
correctly

Unit testing with JUnit


Good test case design
 An good test case satisfies the
following criteria:
• Reasonable probability of catching an error
• Does interesting things
• Doesn’t do unnecessary things
• Neither too simple nor too complex
• Not redundant with other tests
• Makes failures obvious

Unit testing with JUnit


Test case design technique
 Test case design techniques can be
broadly split into two main categories

• Black box (functional)

• White box (structural)

Unit testing with JUnit


Black Box tests

Input Output

 Targeted at the apparent simplicity of the software


• Makes assumptions about implementation
• Good for testing component interactions
 Tests the interfaces and behavior

Unit testing with JUnit


White Box tests

Input Output

 Targeted at the underlying complexity of the


software
• Intimate knowledge of implementation
• Good for testing individual functions
 Tests the implementation and design

Unit testing with JUnit


Test case writing example

 Suppose we have two parameters we want to cover in


a set of tests. Parameters are as follows..

 Operating system  Printers


• Win98 • HP 4100
• Win2k • HP 4200
• Winxp
How We should write test case for this ??

Unit testing with JUnit


Types of Tests

 Unit
• Individual classes or
types

 Component
• Group of related classes
or types

 Integration
• Interaction between
classes

Unit testing with JUnit


What is a testing framework?
 A test framework provides reusable test
functionality which:
• Is easier to use (e.g. don’t have to write the
same code for each class)
• Is standardized and reusable
• Provides a base for regression tests

Unit testing with JUnit


Why use a testing framework?
 Each class must be tested when it is
developed
 Each class needs a regression test
 Regression tests need to have standard
interfaces
 Thus, we can build the regression test
when building the class and have a
better, more stable product for less
work

Unit testing with JUnit


Testing tools

Tools are part of the quality


equation, but not the entire
equation

Unit testing with JUnit


JUnit
 JUnit is a framework for writing unit tests
• A unit test is a test of a single class
• A test case is a single test of a single
method
• A test suite is a collection of test cases
 Unit testing is particularly important when
software requirements change frequently
• Code often has to be refactored to incorporate
the changes
• Unit testing helps ensure that the refactored
code continues to work
Unit testing with JUnit
JUnit..
 JUnit helps the programmer:
• Define and execute tests and test suites
• Formalize requirements and clarify
architecture
• Write and debug code
• Integrate code and always be ready to
release a working version

Unit testing with JUnit


What JUnit does
 JUnit runs a suite of tests and reports
results
 For each test in the test suite:

• JUnit calls setUp()


• This method should create any objects
you may need for testing

Unit testing with JUnit


What JUnit does…
• JUnit calls one test method
• The test method may comprise multiple test
cases; that is, it may make multiple calls to
the method you are testing
• In fact, since it’s your code, the test method
can do anything you want
• The setUp() method ensures you entered the
test method with a virgin set of objects; what
you do with them is up to you
• JUnit calls tearDown()
• This method should remove any objects you
created
Satish Mishra Unit testing with JUnit
Creating a test class in JUnit
 Define a subclass of TestCase
 Override the setUp() method to initialize object(s)
under test.
 Override the tearDown() method to release object(s)
under test.
 Define one or more public testXXX() methods that
exercise the object(s) under test and assert expected
results.
 Define a static suite() factory method that creates a
TestSuite containing all the testXXX() methods of the
TestCase.
 Optionally define a main() method that runs the
TestCase in batch mode.

Unit testing with JUnit


Fixtures
 A fixture is just a some code you want run
before every test
 You get a fixture by overriding the method
• protected void setUp() { …}
 The general rule for running a test is:
• protected void runTest() {
setUp(); <run the test> tearDown();
}
• so we can override setUp and/or tearDown,
and that code will be run prior to or after
every test case
Unit testing with JUnit
The structure of a test method
 A test method doesn’t return a result
 If the tests run correctly, a test method

does nothing
 If a test fails, it throws an

AssertionFailedError
 The JUnit framework catches the error

and deals with it; you don’t have to do


anything

Unit testing with JUnit


Why JUnit
 Allow you to write code faster while increasing
quality
 Elegantly simple
 Check their own results and provide immediate
feedback
 Tests is inexpensive
 Increase the stability of software
 Developer tests
 Written in Java
 Free
 Gives proper uniderstanding of unit testing

Unit testing with JUnit


Problems with unit testing
 JUnit is designed to call methods and
compare the results they return
against expected results
• This ignores:
• Programs that do work in
response to GUI commands
• Methods that are used primary to
produce output

Unit testing with JUnit


Problems with unit testing…
 I think heavy use of JUnit encourages a
“functional” style, where most methods
are called to compute a value, rather
than to have side effects
• This can actually be a good thing
• Methods that just return results, without
side effects (such as printing), are simpler,
more general, and easier to reuse

Unit testing with JUnit

You might also like