Swift : TDD & Unit Testing for Synchronous Code, Xcode project - Part 1
Topics included in part 1
- Why TDD (Test Driven Development)
- XCTest framework/ XCTestCase subclass
- Test Cases and Test Methods
- Test Assertions
- Write first unit test
Lets start a journey towards bug free and more robust app development...
If we write unit tests then, Do we follow TDD? Answer is NO :(
TDD is a development technique where we must first write a test, then write code to get it working. In general writing automated tests seems boring as compared to build new features. But actually TDD turns testing into a design activity. We use the tests to clarify our ideas about what we want the code to do.
TDD allows us to maintain a constant cost of change.
XCTest is the base framework for UI testing to create unit tests. To add tests to a project we need to import XCTest framework in our test class. If you didn’t opt for including Unit test during project creation then you can add it any time as shown below.
After that we can add unit test class, make sure to group related test methods into a single test case class, which is a subclass of XCTestCase
. As in below image we have created MathsOperationsTests class. Below points describe things :
- XCTest is required test framework to import.
- This gives the unit tests access to the internal types and functions in UnitTestDemo project.
- Create object of class, whose functionality we need to test as we may need this for required function calling.
- This is our unit test case class MathsOperationsTest subclass form XCTestCase, we would test all related test of MathsOperations class here.
- We can add setup code here, as this will be called before invocation of each test method.
- It’s a default teardown method.
- This is a demo test function we add for testing, here we are calling add function, that we defined in our MathsOperations class. Just testing sample values 1 and 2, whose result must be 3. Checking result using XCTAssert, if result is incorrect then the test would get failed with failure message.
Tip
Give each test case a name that summarizes the tests within it, such as MathsOperationsTests, or JSONParsingTests.
To help identify failing tests, give each test method a name that makes it clear what is tested by that method, such as testAddion().
According to Apple : A test method is a small, self-contained method that tests a specific part of your code. A test case is a group of related test methods.
If you want to run all tests of a class, just click diamond adjacent to XCTestCase class or you can also run single test by just clicking diamond adjacent to function name.
We used XCTAssert in above example to compare results. There are various like XCTAssertTrue, XCTAssertFalse, XCTAssertNil, XCTAssertNotNil and more. https://developer.apple.com/documentation/xctest
In Part 2, we will jump to Asynchronous Tests and Expectations and more.
Please clap if you find it helpful and share your feedback in comment section. Stay healthy, Stay happy 😀😀