You are on page 1of 51

Software Testing and Quality Assurance

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.

AHMED A. QAZZAZ NOV 2021













Unit Testing
• Unit testing is conducted in two complementary phases:
• Static unit testing (code review)
• Dynamic unit testing

AHMED A. QAZZAZ NOV 2021


















Life Cycle

AHMED A. QAZZAZ NOV 2021

























Definition
• Test execution: is the process of executing the code and comparing the expected and actual
results.
• The typical objectives of test execution are to: Stimulate the items under test in a controlled
manner so as to cause failures that can be used to identify defects.
• The aim is to spend as much time executing as possible
• The test execution can be either manual or Automatic.

AHMED A. QAZZAZ NOV 2021








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

AHMED A. QAZZAZ NOV 2021















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

• Before/After testings, are functions that executed before Before Test_2


and after each test function.
Test_2

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.

AHMED A. QAZZAZ NOV 2021

























JUnit Test
• to identify a class as testing class, you should write it in the testing package, and it should
contains testing annotations by necessity.
• Testing annotations:
• @Before • @DisplayName(“”) This list of annotations available in junit 5.*

• @Test • @RepeatedTest(10)
• @After • @ParametrizedTest
• @BeforeClass • @ValueSource(ints={2,3,4})
• @AfterClass

AHMED A. QAZZAZ NOV 2021















JUnit Testing Example
• First things first
• import the annotations that you are going to use.

Then write you class name, and de ne all the


variables that might be used in the test.

AHMED A. QAZZAZ NOV 2021









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

This is the testing code

AHMED A. QAZZAZ NOV 2021

























JUnit Testing Example

- First we initialized an object from the class we want to test


- then we execute the target function using test cases value
- nally we compare the expected result as mentioned in the test case,
with the actual results

AHMED A. QAZZAZ NOV 2021


fi
















s








Triangle Example

AHMED A. QAZZAZ NOV 2021

























When Success

AHMED A. QAZZAZ NOV 2021

























When Failed

AHMED A. QAZZAZ NOV 2021

























Force Failed

AHMED A. QAZZAZ NOV 2021

























Asset functions
• There are a list of assertion functions that can be used.
• assertEquals: used to compare expected and actual results, and there are many overloads to
cover all data types

AHMED A. QAZZAZ NOV 2021

























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

AHMED A. QAZZAZ NOV 2021

























Another Assertions
• Assert.assertNotEqual • Assert.assertThrow
• Assert.assertNull • Assert.assertSame
• Assert.assertNotNull • Assert.assertNotSame

• Assert.assertArrayEquals
• Assert.assertFalse
• Assert.assertTrue

AHMED A. QAZZAZ NOV 2021























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.

AHMED A. QAZZAZ NOV 2021




















Testing with callbacks
• Creating a countdown object called latch to set the number of threads need to wait in a single
test

AHMED A. QAZZAZ NOV 2021


























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

AHMED A. QAZZAZ NOV 2021

















XCTAssert

AHMED A. QAZZAZ NOV 2021

























XCT with callback

AHMED A. QAZZAZ NOV 2021

























XCT Functions
• -setUp : before a test start
• tearDown : after the test finish
• test function should start with the word
“test”

AHMED A. QAZZAZ NOV 2021
























XCT Performance Testing

AHMED A. QAZZAZ NOV 2021

























Test Driven Development
• TDD is the practice of writing unit tests and development code concurrently and at a very fine
level of granularity.
• programmers first write a small portion of a unit test, and then they write just enough
development code to make that unit test compile and execute

AHMED A. QAZZAZ NOV 2021

























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

AHMED A. QAZZAZ NOV 2021
















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

AHMED A. QAZZAZ NOV 2021




























TDD Example with XCT
• Movies filter
• there is a struct called movie which represent a movie info.
• we want to write a method that filters an array of movies based on release year

AHMED A. QAZZAZ NOV 2021












Media struct (The parent of movies and shows)

• We want to filter the list based on release date

AHMED A. QAZZAZ NOV 2021

























TDD example
• Write the function we want to test

Write the testing code

AHMED A. QAZZAZ NOV 2021

























TDD Example
• Write the function code

Test Again

AHMED A. QAZZAZ NOV 2021

























Hint
• When writing a function such as filter in swift check for Functional Programming

its 10% better in our small example

AHMED A. QAZZAZ NOV 2021

























Example #1
Factorial Function
• Req 1: 0! = 1 package MyClass
• Req 2: 1! = 1
import static org.junit.jupiter.api.Assertions.*
• Req 3: n! = n * (n-1)!
import org.junit.jupiter.api.Test

class factorialTest
  @Tes
  public void shouldReturnOneWhenZeroIn()
  assertEquals(1, fact.factorial(0))
 
}

AHMED A. QAZZAZ NOV 2021


}














Req 1 - Code
• As we write this test we will get an compilation error complaining about the missing factorial
method and fact class.

public    class fact


  static  int factorial(int i)
         if (i==0
        return 1
else return -
 

AHMED A. QAZZAZ NOV 2021


}




















Req 2 - Test Code

AHMED A. QAZZAZ NOV 2021

























Req 2 - Code
public class fact
  static  int factorial(int i)
         if (i==0 || i==1
        return 1
else return -
 

AHMED A. QAZZAZ NOV 2021


}




















Req 3 - Test

AHMED A. QAZZAZ NOV 2021

























Req 3 - Code
public    class fact
  static  int factorial(int i)
        if (i==0 ||i==1
  return 1
         else return factorial(i-1)*i

AHMED A. QAZZAZ NOV 2021


}


















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.

AHMED A. QAZZAZ NOV 2021
















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.

AHMED A. QAZZAZ NOV 2021























Test Code

AHMED A. QAZZAZ NOV 2021

























Test Results

This is the current code >>

AHMED A. QAZZAZ NOV 2021

























The Code

AHMED A. QAZZAZ NOV 2021

























Mobile Students only

Learn more about Espresso Testing for


Android in the Lab course
https://developer.android.com/training/testing/ui-testing/espresso-testing
Software Development Students only

Learn more about Selenium Testing tool


for web pages to discuss it in the lecture

You might also like