Performance Testing in Swift Using Tdd

Performance testing in Swift using TDD

Can you check whether BasicFunctionTest file Target membership includes your test target. You should be able to check this by selecting the file and navigating to File Inspector (target membership).

Unit test in Swift for beginner in TDD

Start with XCTest, a lot of teams us it (mine included). There are other testing frameworks that help with asynchronous tests, have different syntax such as BDD style syntax, but you will be fine with XCTest initially.

Don't worry about code coverage in the beginning. It takes time to get into the habit of writing tests, and some tests for UI code are really hard to write, so I would just focus on improving coverage week by week.

How is measure() testing normally use for in performance unit testing?


  1. Sometimes we need to test the performance of specific functions. e.g. Encrypt and compress a big file; load big data; process image and video; internet operation etc. Performance test will be very useful to check the time consuming for a task.

  2. First of all, you need to set baseline for comparing each operating time. The default tolerance is 10% of baseline which means if the operating time is more than 10% of baseline, then the test would not pass. You can change the tolerance to any percentage depends on your case.

  3. You can also check all the performance reports from here: Navigator->Click last tab called show the report navigator->Select one test->You can see all the test report including performance and coverage of unit test.

Sample Image

Exercises to enforce good practices such as TDD and Mocking

It's a difficult thing to encourage because it can be perceived (quite fairly) as a sea-change; not so much a progression to a goal but an entirely different approach to things.

The short-list of advice is:

  • You need to be the leader, you need to become proficient before you can convince others to, you need to be able to show others the path and settle their uncertainties.

  • First become proficient in writing unit tests yourself

    • Practice writing tests for existing methods. You'll probably beat your head on the desk trying to test lots of your code--it's not because testing is hard or you can't understand testing; it's more likely because your existing code and coding style isn't very testable.

    • If you have a hard time getting started then find the simplest methods you can and use them as a starting point.

  • Then focus on improving the testability of the code you produce

    • The single biggest tip: make things smaller and more to the point. This one is the big change--this is the hardest part to get yourself to do, and even harder to convince others of.

Personally I had my "moment of clarity" while reading Bob Martin's "Clean Code" book; an early chapter talks about what a clean method will look like and as an example he takes a ~40 line method that visually resembled something I'd produce and refactors it out into a class which is barely larger line-count wise but consists of nothing but bite-sized methods that are perhaps 3-7 lines each.

Looking at these itty-bitty methods it suddenly clicked that the unit-testing cornerstone "each test only tests one thing" is easiest to achieve when your methods only do one thing (and do that one thing without having 30 internal mechanisms at play).

The good thing is that you can begin to apply your findings immediately; practice writing small methods and small classes and testing along the way. You'll probably start out slow, and hit a few snags fairly quickly, but the first couple months will help get you pointed in the right direction.

Unit testing for UNUserNotificationCenter .requestAuthorization in Swift 5

So here's where I'm at so far. With thanks to this previous answer:

Unit testing iOS 10 notifications

My testclass:

import XCTest
import UserNotifications

@testable import NotificationsExperiments

class TestClass: XCTestCase {

override func setUp() {
super.setUp()
}

func testDoSomething() {
//Given
// Class being tested
let exampleClass = ExampleClass()
// Create your mock class.
let mockNotificationCenter = MockNotificationCenter()
exampleClass.notificationCenter = mockNotificationCenter
//When
exampleClass.doSomething()
//Then
XCTAssertTrue(mockNotificationCenter.didRequestAuthorization)
}
}


extension TestClass {

class MockNotificationCenter: MockUserNotificationCenterProtocol {

var didRequestAuthorization = false

func requestAuthorization(options: UNAuthorizationOptions, completionHandler: ((Bool, Error?) -> Void)) {
didRequestAuthorization = true
}
}
}

My Example class:

import Foundation
import UserNotifications

class ExampleClass {

#if DEBUG
var notificationCenter: MockUserNotificationCenterProtocol = UNUserNotificationCenter.current()
#else
var notificationCenter = UNUserNotificationCenter.current()
#endif

func doSomething() {
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
notificationCenter.requestAuthorization(options) {
(didAllow, error) in
if !didAllow {
print("User has declined notifications")
}
}
}
}

#if DEBUG
protocol MockUserNotificationCenterProtocol: class {
func requestAuthorization(options: UNAuthorizationOptions, completionHandler: ((Bool, Error?) -> Void))

}

extension UNUserNotificationCenter: MockUserNotificationCenterProtocol {
func requestAuthorization(options: UNAuthorizationOptions, completionHandler: ((Bool, Error?) -> Void)) {
print("Requested Authorization")
}
}
#endif

This does work, but it's more than a bit hacky. In DEBUG mode it doesn't actually send a request for authorization, but it does when it's released.

Any further contributions would be gladly accepted.



Related Topics



Leave a reply



Submit