Godfrey Nolan
RIIS LLC
import XCTest
@testable import HelloWorld
class HelloWorldTests: XCTestCase {
func testExample() {
XCTAssertEqual(HelloWorld().text, "Hello, World!")
}
}
struct HelloWorld {
var text = "Hello, World!"
}
import PackageDescription
let package = Package(
name: "HelloWorld"
)
import PackageDescription
let package = Package(
name: "DeckOfPlayingCards",
dependencies: [
.Package(url: "https://github.com/apple/example-package-fisheryates.git", majorVersion: 2),
.Package(url: "https://github.com/apple/example-package-playingcard.git", majorVersion: 3),
]
)
import XCTest
@testable import HelloWorldTests
XCTMain([
testCase(HelloWorldTests.allTests),
])
import XCTest
@testable import HelloWorld
class HelloWorldTests: XCTestCase {
static var allTests : [(String, (HelloWorldTests) -> () throws -> Void)] {
return [
("testExample", testExample),
]
}
func testExample() {
XCTAssertEqual(HelloWorld().text, "Hello, World!")
}
}
struct HelloWorld {
var text = "Hello, World!"
}
import XCTest
import Nimble
@testable import Calculator
class CalculatorTests: XCTestCase {
let resCalc = CalculatorModel()
func testAdd() {
expect(self.resCalc.add(1,1)) == 2
}
func testAddRange() {
expect(self.resCalc.mul(4, 3)).to(satisfyAnyOf(beGreaterThan(10),beLessThan(20)))
}
}
$ sudo gem install cocoapods
$ pod init
$ vi podfile
platform :ios, '9.0'
source 'https://github.com/CocoaPods/Specs.git'
target 'CalculatorTests' do
use_frameworks!
pod 'Quick'
pod 'Nimble', '~> 5.0.0'
end
$ pod install
1. Create your project with model class and test class.
2. Run pod init
3. Edit the generated podfile and add pod “Cuckoo” as a test target.
4. Run pod install.
5. Close the project and reopen the workspace.
6. Click on the project folder then choose Test Target ➤ Build Phases.
7. Click + and choose New Run Script Phase.
8. Add Listing to the Run Script section, making sure to modify the input files that you want to mock.
9. Build the project.
10. Run the tests.
11. Drag and drop GeneratedMocks.swift into the test section.
12. Run the mocked tests.
platform :ios, '9.0'
use_frameworks!
target 'DateWithCuckooTests' do
pod 'Cuckoo',
:git => 'https://github.com/SwiftKit/Cuckoo.git',
:branch => 'swift-3.0'
end
# Define output file; change "${PROJECT_NAME}Tests" to your test's
root source folder, if it's not the default name
OUTPUT_FILE="./${PROJECT_NAME}Tests/GeneratedMocks.swift"
echo "Generated Mocks File = ${OUTPUT_FILE}"
# Define input directory; change "${PROJECT_NAME}" to your project's root
source folder, if it's not the default name
INPUT_DIR="./${PROJECT_NAME}"
echo "Mocks Input Directory = ${INPUT_DIR}"
# Generate mock files; include as many input files as you'd like to create mocks for
${PODS_ROOT}/Cuckoo/run generate --testable "${PROJECT_NAME}" \
--output "${OUTPUT_FILE}" \
"${INPUT_DIR}/FileName1.swift" \
"${INPUT_DIR}/FileName2.swift" \
"${INPUT_DIR}/FileName3.swift"
# ... and so forth
# Mocking takes the form when(methodIsCalled).thenReturn(aValue)
stub(mock) {
(mock) in when(mock.getDate.get).thenReturn(dateAndTime.from(year: 2014, month: 05, day: 20) as Date)
}
XCTAssertEqual(mock.getDate, dateAndTime.from(year: 2014, month: 05, day: 20) as Date)
XCTAssertNotNil(verify(mock).getDate)
$ gem install slather
$ slather coverage --html --scheme XcodeSchemeName path/to/project.xcodeproj
$ slather coverage --html --scheme Calculator Calculator/Calculator.xcodeproj
# SwiftLint
$ brew install swiftlint
$ cd /path/to/XCode directory
$ swiftlint autocorrect
# SwiftFormat
$ brew install swiftformat
$ swiftformat --indent 4
func testExample() {
let app = XCUIApplication()
app.buttons["3"].tap()
app.buttons["*"].tap()
app.buttons["4"].tap()
app.buttons["="].tap()
let resultTextField = app.textFields["resultsFld"]
XCTAssert(resultTextField.value! as! String == "12")
}
class CalculatorUITests: XCTestCase {
override func setUp() {
super.setUp()
// stop if any test fails
continueAfterFailure = false
}
func testExample() {
// Arrange
let app = XCUIApplication()
// Act
app.buttons["3"].tap()
app.buttons["*"].tap()
app.buttons["4"].tap()
app.buttons["="].tap()
// Assert
XCTAssert(app.textFields["resultsFld"].value! as! String == "12")
}
}
@riisllc, @godfreynolan
godfrey@riis.com
http://riis.com/blog