Day-8 Flashcards
Unit Testing
A test class inherits from XCTestCase
- In computer programming, unit testing is a software testing method by which individual units of source code are tested to determine whether they are fit to use
- Objective-C and Swift are both strongly, statically typed (for the most part), which helps avoid a lot of bugs
Testing for UI
Large part of an iOS app is the user interface, which is more difficult to test and usually is not worth writing unit tests for. For this, you would look into implementing the UI Testing framework.
Benefits of Unit Testing
- Unit testing can help you design your app during the early stages
- You can test out the functionality of your app’s logic without having to rig up any dummy UI or navigating through your app’s screens
- Unit testing gives you confidence when adding new features. Unit tests can tell you which old features were broken by new code
XCTest
A testing framework built right into Xcode (since Xcode 5)
Test Methods must always return void, and start with the prefix “test” and take in no parameters
The structure of a test
Arrange - arrange all necessary preconditions and inputs
Act - act on the object or method under test
Assert - Assert that the expected results have occurred
What should I test?
- Test your model
- You need to test all the different paths of your application
- different paths are like hitting an if / switch / while loops
You can have multiple tests for each public method.
TDD
- Test First - developers are encouraged to write tests before writing code that will be tested
- Red, Green, Refactor - writing a failing test that encapsulates the desired behavior of the code you have not yet written
- App Design - getting an idea of what the features of the app are how they all fit together, also known as System Metaphor
- Independent Tests - each class should have an associated unit test class that tests methods of that class. Being unit tests, we want to minimize the dependencies of the class we are testing. To help with this, we can use mock objects
Writing tests in swift
At the top of your test file, import your Swift application’s module. This will look like: import name_of_your_project
No quotes or .swift necessary. It probably wont autofill. If you have spaces in your project name, use underscores