[SWIFT] What you need to know before writing a test in an iOS app

Introduction

Hello, this is dayossi of iOS engineers.

I want to provide a service that makes my family happy We have released a family diary app called HaloHalo.

This time, about the unit test I would like to sort out the prerequisites that I did not understand well.

What is a unit test in the first place?

Even when the app is running A technique for continuously checking if your code works.

Debugging with print () allows you to check single processing results and variable values, If there is a similar process in other situations, You need to write print () again.

Also, if the same process appears many times, It is better to manage it in a unified format so that it can be used repeatedly. It's more readable and it's clear what you're testing.

Unit tests are done under a unified format The result of repeated processing can be automatically and repeatedly obtained. It seems that it is a tool that can be made.

(Reference books: iOS test complete book (2019) from Chapter 1)

What you need to know before introducing unit tests

This is the main subject.

I understand the benefits of unit testing, On the contrary, "If you can't write in a design that can call the process repeatedly, you can't test." It seems that it can be considered.

I didn't really understand this point, so Below, we will organize them in order.

Is it an app design that allows unit testing?

For example, a process in a particular class If you instantiate the class every time and call the process.

This is a class-dependent case, The process and class are not separated.

As shown below, sortAscendData () described in the view controller If you want to use it with OtherViewCotroller

At this rate, you can instantiate the view controller and call it. It takes time to write the same code in OtherViewCotroller.

If many similar efforts occur, the amount of code to write will increase wastefully, When checking the behavior of the entire app, it becomes difficult to understand where the bug occurred.

//Referenced view controller
import UIKit

class ViewController: UIViewController {

    
    var userDataCollection:[String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
      
        //View drawing
        setupView()
    }

    func setupView(){
        //Draw View
        self.view.addSubview(<#T##view: UIView##UIView#>)
    }
    
    func getUserData(){
        //User data acquisition process
    }
    
    func reloadView(){
        //View redraw
    }
    
    func didTacAction(){
        //Detection of tap motion
    }
    
    func sortAscendData(_ userDataCollection:Array<String>){
        //Data sorting process
    }
}
//ViewController you want to reference

class OtherViewController: UIViewController {
    
    //Display data is stored and I want to sort...
    var otherUserData: [String] = []

    let vc = ViewController()
    vc.sortAscendData(self.otherUserData)

    // MARK:-All you really need is this...    
    func sortAscendData(_ userDataCollection:Array<String>){
        //Data sorting process
    }
}

The common process used in various situations is If you abstract it with protocol,

Without depending on a specific class Also, it becomes clear that there is a common process in the protocol. It will be easier to check the behavior of the entire app.

Therefore, if the responsibilities cannot be shared for each file Since it becomes difficult to extract the process you want to test,

First of all, design that can clarify "where and what kind of responsibility do you have?" I think it's important to think about it.

Summary

Being aware of design means It can be said that it is clear which role is managed by which file.

I think that the number of files to manage will inevitably increase, It makes it easier to do both automatic verification by the test unit and visual check. I think it will be effective even in situations where you are aware of development speed.

I will write more and more unit tests!

(I would appreciate it if you could give me a warm tsukkomi such as "Isn't it okay to think this way here?")

Reference books / articles

Kazuaki Matsuo, Yusuke Hosonuma, Kenji Tanaka et al. IOS Test Complete Book (2019) Introduction to Swift Unit Testing IOS application development policy for designing at the same time while writing code I tried to make an iOS application with MVC now (Swift) / Kai

Recommended Posts

What you need to know before writing a test in an iOS app
[IOS] What you need to know before creating a widget
What you need to do to open a file from the menu in the document-Based App macOS app
What to do if you get a java.io.IOException in GlassFish
[iOS] [Objective-C] How to update a widget from an Objective-C app
What to do if you get a "302" error in your controller unit test code in Rails
What to do if you get an error in Basic authentication during Rails test code
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
What to do if you get a gcc error in Docker
What to do if you get a DISPLAY error in gym.render ()
What to do if you get a groovy warning in Thymeleaf Layout
What to do if you get an Argument Error: wrong number of arguments (given 2, expected 0) in your RSpec test
What to do if you get a wrong number of arguments error in binding.pry
What to do if you can't get the text of an element in Selenium
Create an app that uses the weather API to determine if you need an umbrella.
What to do if you accidentally create a model
What impressed me as a beginner in writing Ruby
What to do if you don't see the test code error message in the terminal console
What to do when you launch an application with rails
How to change a string in an array to a number in Ruby
What happened in "Java 8 to Java 11" and how to build an environment
[Swift] When you want to know if the number of characters in a String matches a certain number ...
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
What to do when you want to know the source position where the method is defined in binding.pry
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do if you get an [An HTTP request took too long to complete.] Error in Docker.