Skip to content

Unit Test & UI Test

Guillermo Moral edited this page May 2, 2023 · 10 revisions

What is Unit Tests?

  • It tests a single function, method, or class

  • Its goal is to verify the correctness of a unit of logic under a variety of conditions

  • External dependencies of the unit under test are generally mocked out (using swift mock generator)

  • They generally don’t read from or write to disk, render to screen, or receive user actions from outside the process running the test

Why do we need to write Unit Testing?

A safety-net

  • Unit Testing is mandatory when working with CI/CD. Without such a mechanism like automated testing as a safety-net, who can guarantee we won't deploy a defective service?

Documentation

  • This is considered better than internal documentation.

Efficiency

  • We can save a lot of time because the coder only needs to focus on a single unit without worrying it will break another part of the project

Code Quality

  • The existence of Unit Testing is a sign that it's a good project

How does it work?

  • Add the test dependency

  • Create a test file

  • Create a class to test

  • Write a test for our class

  • Combine multiple tests in a group

  • Run the tests and give feedback

Given-When-Then, Template

  • The given part describes the state of the world before you begin the behavior, you're specifying in this scenario. You can think of it as the pre-conditions to the test

  • The when section is that behavior that you're specifying

  • Finally the then section describes the changes you expect due to the specified behavior

Demonstrations

XcodeMockGenerator

Frameworks

  • Quick and Nimble are frameworks that are used in Unit tests in iOS. Quick is a testing framework and Nimble is a matching framework. They help us to write tests more easily and readably in addition to verifying how your programs behave. According to its documentation https://github.com/Quick/Nimble

  • EarlGrey 2.0 is a native iOS UI automation test framework that combines EarlGrey with XCUITest, Apple's official UI Testing Framework. https://github.com/google/EarlGrey/tree/earlgrey2

  • Appium designed to facilitate UI automation of many app platforms, including mobile (iOS, Android, Tizen), browser (Chrome, Firefox, Safari), desktop (macOS, Windows), TV (Roku, tvOS, Android TV, Samsung), and more. (http://appium.io/docs/en/2.0/)

Plugin

Creating Mock APIs

https://app.mocklab.io/

References

https://martinfowler.com/bliki/GivenWhenThen.html

https://medium.com/mobil-dev/quick-and-nimble-5c90aa6b3d48

https://medium.com/assertqualityassurance/earlgrey2-setting-up-white-box-testing-capability-e90af19a3fee

https://medium.com/@ar.sarris/swift-dependency-interaction-testing-ec10a75d52c

Clone this wiki locally