Unit Testing Text Fields in Swift

In this tutorial, I am going to share with you how to Unit Test some of the UITextField attributes in Swift. Learning how to Unit Test text field attributes covered in this blog post, should give you a good understanding of how to Uni Test other UITextField attributes. This blog post also contains video lessons.

If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App

Text Fields To Test

For this tutorial, I have created a single view project with an example of the Signup Form. I will be writing unit tests for the text fields that are on the image below.

Signup Form Example in Swift

Loading a UIVieController in a Unit Test

The Signup View Controller which is on the picture above uses a Storyboard. So to be able to unit test its text fields, I will first need to load this ViewController in my unit test. Below is a unit test method that loads a UIViewController that is on the storyboard.

class TestingUITextFieldPropertiesTests: XCTestCase {

    var sut: SignupViewController!
    
    override func setUpWithError() throws {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        
        sut = storyboard.instantiateViewController(withIdentifier: "SignupViewController") as? SignupViewController
        
        sut.loadViewIfNeeded()
    }
    
    override func tearDownWithError() throws {
        sut = nil
    }

}

Test that UITextField is Connected

If you are testing text fields in your View Controller you will most likely want to check first that the UITextFields are connected to their IBOutlet(s). You can do that by simply trying to unwrap the field with the XCTUnwrap test assertion.

If any of UITextFields is not connected, the XCTUnwrap will fail the unit test.

func testSignupForm_WhenLoaded_TextFieldAreConnected() throws {
 _ = try XCTUnwrap(sut.firstNameTextField, "The First Name UITextField is not connected")
 _ = try XCTUnwrap(sut.lastNameTextField, "The Last Name UITextField is not connected")
 _ = try XCTUnwrap(sut.emailTextField, "Email address UITextField is not connected")
 _ = try XCTUnwrap(sut.mobilePhoneTextField, "Mobile phone UITextField is not connected")
 _ = try XCTUnwrap(sut.passwordTextField, "The Password UITextField is not connected")
}

Test UITextField has Correct Input Text Content Type

To test that UITextField has a correct Input Text Content Type we can use XCTAssertEqual test assertion and the UITextContentType structFor example, lets test that the email address text field has a UITextContentType.emailAddress assigned.

func testEmailTextField_WhenCreated_HasEmailAddressContentTypeSet() throws {
    let emailTextField = try XCTUnwrap(sut.emailTextField, "Email address UITextField is not connected")
    
    XCTAssertEqual(emailTextField.textContentType, UITextContentType.emailAddress, "Email address UITextField does not have an Email Address Content Type set")
}

To learn what is UITextContentType have a look at the following video tutorial.

To see how to unit test UITextField for a correct UITextContentType on video, have a look at the following video tutorial.

Test that UITextField Has Correct Keyboard Type

We can also create a unit test to test that the UITextField has a correct Keyboard Type configured. For example, it is good for the text field that accepts user phone number, to present the user with a Phone Pad. If a user needs to enter an email address, then it is good to present a different keyboard type, like for example, UIKeyboardType.emailAddress.

The unit test below tests that the UITextField that collects the user’s email address has an Email Address keyboard type assigned to it.

func testEmailTextField_WhenCreated_HasEmailKeyboardTypeSet() throws {
   let emailTextField = try XCTUnwrap(sut.emailTextField, "Email address UITextField is not connected")
    
    XCTAssertEqual(emailTextField.keyboardType, UIKeyboardType.emailAddress, "Email Address UITextField does not have Email Keyboard type set")
}

Below is a video tutorial that demonstrates how to do it.

Test that Password Text Field is a Secure Text Entry

It is also very important to make sure that text fields that collect the user’s password and pin codes are configured to a Secure Text Entry. When a UITextField is set to be a Secure Text Entry then the password characters that user types in, appear masked.

The below unit test asserts that the Password text field is secure text entry.

func testPasswordTextField_WhenCreated_IsSecureTextEntryField() throws {
    let passwordTextField = try XCTUnwrap(sut.passwordTextField, "The Password UITextField is not connected")
    
    XCTAssertTrue(passwordTextField.isSecureTextEntry, "Password UITextField is not a Secure Text Entry Fieldg")
}

I hope this tutorial, was of some help to you. If you are interested to learn more about unit testing Swift code, have a look at my other unit testing and UI testing tutorials on this web site. For video lessons have a look at my video course “Unit Testing and UI Testing Swift Mobile App“.

Happy unit testing! 🙋🏻‍♂️

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *