Professional Documents
Culture Documents
Unit Testing
AHMED A. QAZZAZ NOV 2021
Definition
• Unit Test:
• is a software test method in which a programmer tests if individual units of source code are
fit for use.
• A unit is the smallest testable part of an application.
• In procedural programming a unit may be an individual function or procedure while in
object-oriented programming, the smallest unit is usually a class.
Unit Testing
• Unit testing is conducted in two complementary phases:
• Static unit testing (code review)
• Dynamic unit testing
Life Cycle
Definition
• Types of test executions:
• 1) Manual via a set of function calls
• Hard to check when the number of test cases grows
• 2) Fully automatic without programmers assistance
• Not possible so far
• 3) Automatic with programmers assistance
• • Unit testing SDKs, and libraries such as JUnit test, and XCT
Unit Testing Structure
Before Class
• Usually unit testing has major parts
• class testing function, which executed once at the beginning Before Test_1
and ending of a collection of tests. Test_1
• Testing function, which aim to test a special code (function,
class, or process). After Test_1
After Test_2
After Class
AHMED A. QAZZAZ NOV 2021
JUnit Testing
• To write Testing Code for Android apps you can use JUnit test, which is related to Java, or use
android testing for Kotlen.
• @Test • @RepeatedTest(10)
• @After • @ParametrizedTest
• @BeforeClass • @ValueSource(ints={2,3,4})
• @AfterClass
JUnit Testing Example
• First things first
• import the annotations that you are going to use.
fi
JUnit Testing Example
Before Class
Before Test_1
Test_1
After Test_1
Before Test_2
Test_2
After Test_2
After Class
AHMED A. QAZZAZ NOV 2021
JUnit Testing Example
• Will test this function now
Triangle Example
AssetEqual
• When dealing with floats or doubles there is an extra parameter you can add, that refers to the
tolerance between the actual and the expected results
• e.g. if the difference between the two values is less than 0.1 consider it pass
Another Assertions
• Assert.assertNotEqual • Assert.assertThrow
• Assert.assertNull • Assert.assertSame
• Assert.assertNotNull • Assert.assertNotSame
• Assert.assertArrayEquals
• Assert.assertFalse
• Assert.assertTrue
Testing With Call backs
• Mobile apps usually based on callback function, and functional programming, and each process
might be based on request.
• All the previous unit tests are direct and will not wait for a request to complete or for a thread
to finish.
• There are special annotations and techniques used to force the unit test to wait for the results
from a request, or a thread.
Testing with callbacks
• Creating a countdown object called latch to set the number of threads need to wait in a single
test
Testing With XCT for iOS
• XCT is a shortcut for Xcode Testing
• XCT implements the same structure as all test class structure except for before and after
class
• Also XCT uses the same assert collection of comparing functions
XCTAssert
XCT Performance Testing
TDD Design
Design Test
Implement Implement
Test Test
normal testing TDD
AHMED A. QAZZAZ NOV 2021
Design - Test - Implement - Test
• Figure out what we want to do
• Write a test to express the design << should fail
• Write the code
• Test again << should success
TDD Stages
Write a test
Refactor code
Compile
(and test)
Run test,
Fix compile errors
watch it pass
Run test,
Write code
watch it fail
Media struct (The parent of movies and shows)
Test Again
class factorialTest
@Tes
public void shouldReturnOneWhenZeroIn()
assertEquals(1, fact.factorial(0))
}
Req 1 - Code
• As we write this test we will get an compilation error complaining about the missing factorial
method and fact class.
Req 2 - Test Code
Req 3 - Test
Example #2
Password validation
• Requirements
• length larger than or equal to 6 characters.
• length smaller than or equal to 10 characters.
• Contains at least one digit
• Contains at least one upper case character.
Test Cases
• Ab123 • Abc123456789 • Abcdefg • 1bcdefg • 1Abcdef
• >= 6 char. • >= 6 char. • >= 6 char. • >= 6 char. • >= 6 char.
• <= 10 char. • <= 10 char. • <= 10 char. • <= 10 char. • <= 10 char.
• one digit • one digit • one digit • one digit • one digit
• upper case. • upper case. • upper case. • upper case. • upper case.
Test Code