Xcode testing Using XCTest.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
4 Oracle Data Integrator First Project – Simple Transformations: One source, one target 3-1.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
WASTE MANAGEMENT ©2010 SciQuest USA Confidential 1 Powered by RFx User Guide.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Key Applications Module Lesson 12 — Word Essentials
Storyboards Managing multiple views. Overview Create a single view application Give the project a name and click “Use Storyboards” and “Use Automatic.
1Allocating Expenses. Create Report Header Page To access this screen, click on ‘New Expense Report’ from your Expense Module Tab This page will come.
Automating Tasks with Macros. Macro Essentials  A macro is a list of actions that happen when you run the macro.  Creating a Macro: − Choose Create.
Ogden Air Logistics Center. Purpose of Excel2FV Many agencies produce point lists of different data (target lists, force locations, etc.) in either Excel.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Sprite Animation CSE 391 Fall 2012 Tony Scarlatos.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Microsoft ® Office Outlook ® 2007 Training See and Use Multiple Calendars ICT Staff Development presents:
Xcode Presentation Tom Pletzke. Creating App from template Launch Xcode Select Tabbed Application.
Introduction to the WebBoard Terry Dennis. The WebBoard - Our Connection The WebBoard URL is
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
iOS components in Swift
InDesign CS3 Lesson 4 ( Only pages ) Importing and Editing Text.
Using Visual Basic for Applications (VBA) – Project 8.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
Tabbed Views UITabBarController. Controller Architecture UITabBarController Controls the first view that the user sees The view controller class (and.
IOS with Swift Hello world app.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Scratch Programming Lesson 4 Question asking and answering.
View Controllers Content taken from book: “iPhone SDK Development” by Bill Dudney and Chris Adamson.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Gestures UIGestureRecognizer.
GISMO/GEBndPlan Overview Geographic Information System Mapping Object.
SilkTest 2008 R2 SP1: Silk4J Introduction. ConfidentialCopyright © 2008 Borland Software Corporation. 2 What is Silk4J? Silk4J enables you to create functional.
Nav Controllers UINavigationController. Overview Nav Controller basics Like a tabview controller, a navViewController manages views A navigationViewController.
Orientation Configuration adapting to orientation changes.
The Homepage My Campaign is where you can track your contacts information.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Steps for posting a training (single event or series) to the regional calendar R&C Regional Calendar.
Page Designer Storyboard J. A. Fitzpatrick December 2004.
JDS5 Training Guide. On Start Up you will see this screen click the OK button Click OK.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
IT533 Lectures ASP.NET AJAX.
1 Reverse a String iPhone/iPad, iOS Development Tutorial.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
Basic ActionScript and PHP Cis 126. Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your.
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
Folio3 IPhone Training Session 2 Testing App on device Presenter: Imam Raza.
Tutorial 11 1 JavaScript Operators and Expressions.
Perfecto Mobile Automation
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Editing and Debugging Mumps with VistA and the Eclipse IDE Joel L. Ivey, Ph.D. Dept. of Veteran Affairs OI&T, Veterans Health IT Infrastructure & Security.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
WebViews UIWebView. Controller Architecture UITabBarController Controls the first view that the user sees The view controller class (and xib) that manages.
Logical Operators.  Quizzes!  Let's look at the schedule  Logical Operators 2.
The Smarter Balanced Assessment Consortium
The Smarter Balanced Assessment Consortium
Conditions and Ifs BIS1523 – Lecture 8.
The Smarter Balanced Assessment Consortium
The Smarter Balanced Assessment Consortium
Apple Xcode with Swift Demo
CSC 581: Mobile App Development
CSC 581: Mobile App Development
Executive Reports, Instructions and Documentation
Presentation transcript:

Xcode testing Using XCTest

Testing in XCode Done using XCTest class Test classes are created separately from code Test classes can access and call classes in your app Test classes have assertion functions that you can call to test results You can define how fine-grain your tests are.

Test Navigator Create a new single-view project. Click on the test navigator button in the left panel You’ll see two templates for testing

Test Navigator The Run button ( ) appears to the right of the item name when you hold the pointer over any item in the list. This is a quick way to run all the tests in a bundle, all the tests in the class, or any individual test. Tests return pass or fail results to Xcode. As tests are being executed, these indicators update to show you the results, a green checkmark for pass or a red x for fail. In the test navigator shown here, two of the tests have asserted a failure.

Test Navigator Clicking any test class or test method in the list opens the test class in the source editor. Test failures display the result string at the associated assertions in the source editor.

Testing Continued on next slide… In the test navigator in the single-view application that you created earlier, click on the class testExample() In the editor, you’ll see the code: import UIKit import XCTest class testingInSwiftTests: XCTestCase { override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. } override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() Continued on next slide…

Testing func testExample() { // This is an example of a functional test case. XCTAssert(true, "Pass") } func testPerformanceExample() { // This is an example of a performance test case. self.measureBlock() { // Put the code you want to measure the time of here. This is an assertion that always passes Click on the run button that appears next to testExample() when you put your mouse over it. The green check means test was passed

Test Organization The setUp method gets called before each test runs. If you have initialization code you call before running each test, place the code in the setUp method so you don’t have to repeat the code in each test. The tearDown method gets called after each test runs. If you run any cleanup code after running each test, place that code in the tearDown method so you don’t have to repeat the code in each test. The testExample method provides an example of a test method. The testPerformanceExample method provides an example of a performance test case, which measures how long a task takes to complete.

Test Methods A Swift test method has the following conditions: It must begin with the word test. It takes no arguments. It returns no value. You can create as many test methods in a class as you like. Every test method must have an assertion which determines whether or not the test is passed.

Assertions XCTest assertions start with XCTAssert. If you start typing XCTAssert, Xcode’s code completion should show you a list of all the available assertions The following are the most widely-used assertions: XCTAssertNotNil asserts a variable is not nil. XCTAssertTrue asserts a condition is true. XCTAssertFalse asserts a condition is false. XCTAssertEqual asserts two values are equal. XCTAssertEqualObjects asserts two objects are equal. XCTAssertEqualWithAccuracy asserts two floating-point values are equal.

Assertions XCTest assertions take two or more arguments. The first arguments depend on the assertion. XCTAssertTrue and XCTAssertFalse take a condition as the first argument. The XCTAssertEqual assertions take two arguments at the start: the two items you’re checking are equal. After the assertion testing arguments comes the error message, which is a string. Xcode displays the error message if the assertion fails. If you want to show the value of a variable in the error message, place the variable name in parentheses in the error message, prefixed by a backslash character. \(variableName) The following code shows an example of an assertion involving a variable named length: XCTAssertTrue(length == 12, “The length should have been 12. Actual length: \(length)")

Testing Add the following method to the testExample() class. Run the testExample() func testMultiplyingTwoNegativeNumbersYieldsAPositiveValue() { let x = –6 let y = –4 let product = x * y XCTAssertTrue(product > 0, "Multiplying two negative numbers should yield a positive number. Product: \(product)") } This test does not test the code in the app but only serves to demonstrate how assertions work

Testing II Create the following view in your app Add and connect the following IBOutlets: @IBOutlet weak var addButton: UIButton! @IBOutlet weak var resultText: UITextField! @IBOutlet weak var secondText: UITextField! @IBOutlet weak var firstText: UITextField!

Testing II Add the following code to the viewController @IBAction func addButtonPressed(sender: UIButton) { // toInt returns optional that's why we used a:Int? let a:Int? = firstText.text.toInt() // firstText is UITextField let b:Int? = secondText.text.toInt() // secondText is UITextField // check a and b before unwrapping using ! if a != nil && b != nil { var ans = a! + b! resultText.text = "\(ans)“ } else { resultText.text = "Input values are not numeric" } connect the button in the storyboard to this IBAction method

Testing II Go to the Project Navigator and click on the file ViewController.swift Then click on the File Inspector on the rightmost panel and add the ViewController to the test target Do the same with the AppDelegate.swift file This enables methods in the test file to see variables in the viewController

Testing II Go to your test class At the top add the line import testingInSwift Where testingInSwift is the name of your project.

Testing II First determine whether the root view loads: func testViewDidLoad() { // we only have access to this if we import our project above // Viewcontroller is the name of the root view controller class in your app let v = ViewController() // assert that the ViewController.view is not nil XCTAssertNotNil(v.view, "View Did Not load") }

Testing III It’s easy to test your models. It’s tricky to test your storyboard. First steps Go to the project navigator on the left (where the classes in your project are listed). Click on your project name Go to the Build Settings tab Go to the Packaging section Change Defines Module to “YES” Note the Product Module Name several lines below this; this name is what you’ll need to import into your test class

Testing III Go to your test class and import the product module name: import testingInSwift class testingInSwiftTests: XCTestCase { Add this line but substitute your product module name

Testing III Manual Lifecycle events when your app runs, lifecycle events are generated automatically (viewWillAppear, viewDidLoad, etc.) in tests, you will need to generate these in code

Testing III First, need to give your storyboard a name. In the project navigator, go to the Main.storyboard (this app only has one storyboard; some apps have more) In the storyboard navigator, click on the view controller Then open the identity inspector in the right side panel Give the storyboard an identifier

Testing III Now we can write a test method to test our interface. go to your test class and add the following method (can be anywhere in the class): func testAddMethod() { let SB = UIStoryboard(name: "Main", bundle: nil) myVC = SB.instantiateViewControllerWithIdentifier("testStoryboard") as ViewController let _ = myVC.view // more code goes here } We’ll examine each of these lines on the next slide

Testing III func testAddMethod() { let SB = UIStoryboard(name: "Main", bundle: nil) myVC = SB.instantiateViewControllerWithIdentifier("testStoryboard") as ViewController let _ = myVC.view // more code goes here } First we create an instance of the storyboard Then we instantiate the viewcontroller for this storyboard (note the “as ViewController). The variable myVC now points to the viewcontroller. The IBOutlets are only created when the view loads, so we create a blank variable that contains the view, thus forcing the view to load.

Testing III We can now populate the fields and click the button. Add this code after the “more code goes here “ comment on the previous slide: let sumText = “–10“ // this is the expected answer myVC.firstText!.text = “–6" // Now set values in the textfields myVC.secondText!.text = “–4"

Testing III Now you can run the test! Now click the button myVC.addButton!.sendActionsForControlEvents(UIControlEvents.TouchUpInside) and then test the answer XCTAssertEqual(sumText, myVC.resultText!.text, "Adding two numbers should yield \(sum)") } This is the IBOutlet to the button We’re getting the answer out of the result textfield This is the string containing the expected answer Now you can run the test!

Implementing the test plan Every test in the plan should have a method that implements that test