Testare Software
Testare Software
Testare Software
ALIN ZAMFIROIU
ALIN.ZAMFIROIU@CSIE.ASE.RO
Contents
Unit Testing
Junit – Assertions
FIRST
TestSuite
Double Test
White box testing & Black box testing
WhiteBox Testing
Since tests can be very complex, highly skilled resources are required, with
thorough knowledge of programming and implementation.
Test script maintenance can be a burden if the implementation changes
too frequently.
Since this method of testing it closely tied with the application being
testing, tools to cater to every kind of implementation/platform may not
be readily available.
BlackBox Testing
The tester know only the input and the output of the application.
BlackBox Testing Advantages
Tests are done independent from the developers and have a objective
perspective.
Test cases are designed after the specifications are complete, because
are based on these specifications.
BlackBox Testing Disadvantages
Test cases will be difficult to be designed because the tester don’t have
the specification of the application;
A way of testing the code, by programmers still from the software product
development stage.
UNIT TEST – code sequence used to test a well-defined unit of the software application
code. Usually the unit is a method.
Easy to write
Although they are simple, they can define collections of tests – Test Suites
They can be played automatically every time that need it (write once, use many
times)
Reasons to use Unit Tests
There are many frameworks and tools that simplify the process of writing
and running
Reduce the lost time for debugging and for finding bugs
Increases the rate of bugs identified during the code writing phase
Reasons to not use Unit Tests
I do not have time for tests. I have to implement functionalities, not tests.
It is a framework that allows you to create and run tests for different
methods from the developed projects.
Kent Beck has developed in the 90's the first automated test tool, xUnit, for Smalltalk
Beck and Gamma (Gang of Four) developed JUnit during a flight from Zurich to
Washington DC.
Junit has become the standard tool for TDD-type development processes in Java
Junit is the standard component in multiple Java IDEs (Eclipse, BlueJ, Jbuilder, DrJava,
InteliJ)
Xunit tools have been developed for other languages as well (Perl, C++, Python, Visual
Basic, C#, …)
JUnit
JUnit NUnit
assertEquals Assert.AreEqual
assertEquals Assert.AreNotEqual
assertSame Assert.AreSame
assertNotSame Assert.AreNotSame
assertNull Assert.IsNull
assertNotNull Assert.IsNotNull
assertTrue Assert.IsTrue
assertFalse Assert.IsFalse
JUnit3 și JUnit4 – differences
JUnit3 needs a version of JDK newer than JDK1.2 while JUnit4 needs a version newer than
JDK5;
in JUnit3 the test classes must be derived from the TestCase class, and in JUnit 4 it is not
necessary to inherit the TestCase class;
in JUnit3 the name of test methods is built on the testAAA format; so all test methods
contain in their name the test word; in JUnit4 methods name is not important, but the
methods that are run as tests have the @Test annotation;
in JUnit4 it is possible to use the adnotation @Ignore, if a test should be avoided; in JUnit3 that
test should be deleted or commented.
Test Case
Unit testing - Junit skeleton
Unit testing - Junit skeleton
Unit testing - Junit skeleton
@BeforeClass @BeforeAll
@AfterClass @AfterAll
@Before @BeforeEach
@After @AfterEach
Unit testing - Assertions
Create unit tests for the methods of this class, using JUnit4.
Unit testing
Unit testing - Assertions
assertEquals(expected, actual)
assertEquals(expected, actual, delta)
assertSame(expected, actual)
assertNotSame(expected, actual)
Test the “sum” method. assertNull(object)
assertNotNull(object)
assertTrue(condition)
assertFalse(condition)
fail(message)
Unit testing - Assertions - assertEquals
Unit testing - Assertions
assertEquals(expected, actual)
assertEquals(expected, actual, delta)
assertSame(expected, actual)
assertNotSame(expected, actual)
Test the “division” method.
assertNull(object)
assertNotNull(object)
assertTrue(condition)
assertFalse(condition)
fail(message)
Unit testing - Assertions - assertEquals
assertEquals(expected, actual)
assertEquals(expected, actual, delta)
assertSame(expected, actual)
assertNotSame(expected, actual)
Test the “isEven” method.
assertNull(object)
assertNotNull(object)
assertTrue(condition)
assertFalse(conditin)
fail(message)
Assertions – assertTrue / assertFalse
Unit testing - Assertions
assertEquals(expected, actual)
assertEquals(expected, actual, delta)
assertSame(expected, actual)
Test the “nEvenNumbers” method. assertNotSame(expected, actual)
assertNull(object)
assertNotNull(object)
assertTrue(condition)
assertFalse(condition)
fail(message)
Assertions – assertNull / assertNotNull
Assertions – assertNull / assertNotNull
This method also should be modified, because for n=0, we will have an
empty list, not a null value.
Assertions
These assertions are only a small part of the all assertions that exists, but are
the most used.
Right-BICEP principle
Right-BICEP
Now that we know how to do tests, all we have to do is to know what tests we have
to do.
People with a lot of experience know directly (from their own experience) what they
have to test.
People with less experience need to follow certain directions or certain principles.
Every time when we test a method, the first thing that should be veryfied is
if that method delivers the right results.
That is why the first direction is to check the correctness of the results.
This verification is made in accordance with the specifications of the
developed project.
For the Utils.java class it is necessary to check :
if the sum is correct calculated.
if the division is correct determinate.
Unit testing - Right
Unit testing - Boundary
Once these limits have been determined, exact tests are performed for
those values.
Boundary tests do not require testing outside these values but checking
the correctness for these values - limit values.
There are usually lower limits and higher limits. Tests are done for both
situations.
Unit testing - Boundary
In order to identify the extreme limits more easily, we must use the
CORRECT principle :
C – Conformance;
O – Ordering;
R – Range;
R – References;
E – Existence;
C – Cardinality;
T – Time.
Unit testing - Inverse relationship
Certain methods can be tested by applying the inverse rule: starting from
the result it must reach the same input from which it initially started..
Also for the databases it can be checked whether an insert was made by
the reverse operation: select.
Usually there are several ways to solve a problem. Thus, another method
for solving the same problems for testing the newly implemented method
can be used
Unit testing - Cross-Check
Testing the new method is done through the old method, even if it
consumes more resources.
Unit testing - Cross-Check
Testing for forcing errors is done on all methods. All methods have at least
one situation where they will provide error. Testing is done for these
situations and it is verified that the method treats that case and throws or
delivers an exception.
Unit testing - Error conditions
For JUnit4 to test the time it is used the next adnotation: @Test(timeout=100)
C – Conformance;
O – Ordering;
R – Range;
R – Reference;
E – Existence;
C – Cardinality;
T – Time.
CORRECT
Each subprinciple has a question that should be in the mind of the tester.
This principle is used also to helps the testers for the boundery conditions
Conformance
It is also known as
Type testing
Compliance testing
Conformity assessment
Conformance
Usually for any input and for any output it must be verified that it conforms
to a format or to a standard.
Conformance
Tests can be done to check what happens if the input data does not
conform to the format or to see if the result obtained conforms to the
project specification format.
Conformance
For the Person class we have to test if the CNP atribute has 13 characters.
Conformance
Ordering
In the case of lists, check if the order of the items is the one that you want.
You can also test the behavior of the method if it receives some
parameters in another order or a list of items in a different order than
expected.
Ordering
For both input and output values, certain intervals are set. These intervals
need to be checked.
Several intervals are set for certain methods. This will test for all these
intervals.
Range
All functions that have an index must be tested for range because that
index has a well-established range.
Usually it is necessary to check:
The start and end value for the index are the same;
The first element is larger or smaller than the last element;
What happens if the index is negative;
What happens if the index is higher than the upper limit;
The number of items is not the same as the one you want;
Etc.
Range
For example:
A web application requires the user to be logged in;
A pop stack works if there are elements in the stack;
Etc.
Reference
These tests are performed using test doubles (stub, fake, dummy, mock).
Existence
Also for software systems that work with files or with internet connection, it
is necessary to check the existence of these files or the availability of
internet connection. Otherwise, the application does not need to crack
but must behave normally.
Existence
0-1-n Rule
For example, to call the logout method(), you must first call the login
method() and other examples.
F.I.R.S.T.
F.I.R.S.T
“ For unit tests to be useful and effective for your programming team you
need to remember to make them FIRST.”
F.I.R.S.T
Fast
Isolated/Independent
Repeatable
Self-Validating
Timely
Fast
The developed test should be fast because if we have too many tests we
don’t have to wait to much time when we run them.
Isolated
When a test fail, the developer should not debug to identify what is wrong
and where.
The test should be isolated and to say exactly where is the problem and
what problem.
Repeatable
The obtained results should be the same indifferent of the number of test runs.
If the tests pass, the developer should have high confidence that the code
is correct without errors.
If a test fail the developer must have high confidence that the code
should be improved.
Timely
In this moment we have a lot of testCases and we have to run only few
test from these testCases.
With suites we can group some tests from different testCases and to run
only these tests.
Unit testing – TestSuite
Custom TestSuite – JUnit 4
For each test that you want to be part of that suite, add the annotation :
@Category(CustomSuite.class)
Custom TestSuite – JUnit 4
@RunWith(Categories.class)
@IncludeCategory(CustomSuite.class)
@SuiteClasses({ UtilsTest.class, PersonTest.class })
public class NewSuiteTests {
}
Custom TestSuite – JUnit 4
In this way we can create custom suites, based only on these categories.
Double test
Double test
Every time when we test a method, we try to test the correctness of this
method.
There is a problem for situations where our method uses external objects,
external links, or external method calls.
These objects, links, or methods can influence the results obtained using
the method that we want to test.
Double test
This is the way to test the method, but with a control over the external
references.
REFERENCE (CORRECT)
A double test is simply another object that fits the necessary collaborator
interface and can be replaced in its place. There are several types of test
doubles.
Dummy object
Dummy object – an object that respects the interface but the methods do nothing or
return 0 or null.
When we have to use the real object, actually we use a dummy object.
These doubles are used when we don’t have to call the methods from that object.
Because they are doing nothing.
Dummy object
Stub
https://blog.pragmatists.com/test-doubles-fakes-mocks-and-stubs-1a7491dfa3da
Stub
Fake
Fake – is an object that behaves like a real one but has a simplified version.
Usually for a fake we can set what the value should it return. It will not be a
hardcoded value.
Fake
https://blog.pragmatists.com/test-doubles-fakes-mocks-and-stubs-1a7491dfa3da
Spy
Spy– is a Stub or Fake that manages and counts the number of calls.
Mock
https://blog.pragmatists.com/test-doubles-fakes-mocks-and-stubs-1a7491dfa3da
Mock testing
Mock testing – It is used when we want the tested method not to be influenced by
external references.
Mock object – an object that simulates the behavior of a real object, but in a controlled
manner.
Methods:
doReturn()
doAnswer()
when()
thenReturn()
thenAnswer()
thenThrow()
doThrow()
doCallRealMethod()
Mockito
Test data files
There will be a single TestCase with the assert method called in a loop.
For every set of test data in the file, call the assert method.
https://github.com/ghsukumar/SFDC_Best_Prac
tices/wiki/F.I.R.S.T-Principles-of-Unit-Testing
https://pragprog.com/magazines/2012-01/unit-
tests-are-first
http://agileinaflash.blogspot.com/2009/02/first.
html