-
Notifications
You must be signed in to change notification settings - Fork 263
Allow selecting a particular test or test case to run from the command line #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2016 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
// | ||
// ArgumentParser.swift | ||
// Tools for parsing test execution configuration from command line arguments | ||
// | ||
|
||
internal struct ArgumentParser { | ||
private let arguments: [String] | ||
|
||
init(arguments: [String] = Process.arguments) { | ||
self.arguments = arguments | ||
} | ||
|
||
var selectedTestName: String? { | ||
return arguments.count > 1 ? arguments[1] : nil | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2016 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
// | ||
// XCTestFiltering.swift | ||
// This provides utilities for executing only a subset of the tests provided to XCTMain | ||
// | ||
|
||
internal typealias TestFilter = (XCTestCase.Type, String) -> Bool | ||
|
||
internal struct TestFiltering { | ||
private let selectedTestName: String? | ||
|
||
init(selectedTestName: String? = ArgumentParser().selectedTestName) { | ||
self.selectedTestName = selectedTestName | ||
} | ||
|
||
var selectedTestFilter: TestFilter { | ||
if let selectedTestName = selectedTestName { | ||
if let selectedTest = SelectedTest(selectedTestName: selectedTestName) { | ||
return selectedTest.matches | ||
} else { | ||
return excludeAllFilter() | ||
} | ||
} else { | ||
return includeAllFilter() | ||
} | ||
} | ||
|
||
private func excludeAllFilter() -> TestFilter { | ||
return { _ in false } | ||
} | ||
|
||
private func includeAllFilter() -> TestFilter { | ||
return { _ in true } | ||
} | ||
|
||
static func filterTests(entries: [XCTestCaseEntry], filter: TestFilter) -> [XCTestCaseEntry] { | ||
return entries | ||
.map({ testCase, tests in | ||
return (testCase, tests.filter({ filter(testCase, $0.0) })) | ||
}) | ||
.filter({ testCase, tests in | ||
return !tests.isEmpty | ||
}) | ||
} | ||
} | ||
|
||
/// A selected test can be an entire test case, or a single test method | ||
/// within a test case. | ||
private struct SelectedTest { | ||
let testCaseName: String | ||
let testName: String? | ||
} | ||
|
||
private extension SelectedTest { | ||
init?(selectedTestName: String) { | ||
let components = selectedTestName.characters.split(separator: "/").map(String.init) | ||
switch components.count { | ||
case 1: | ||
testCaseName = components[0] | ||
testName = nil | ||
case 2: | ||
testCaseName = components[0] | ||
testName = components[1] | ||
default: return nil | ||
} | ||
} | ||
|
||
func matches(testCase testCase: XCTestCase.Type, testName: String) -> Bool { | ||
return String(reflecting: testCase) == testCaseName && (self.testName == nil || testName == self.testName) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// RUN: %{swiftc} %s -o %{built_tests_dir}/SelectedTest | ||
// RUN: %{built_tests_dir}/SelectedTest SelectedTest.ExecutedTestCase/test_foo > %T/one_test_method || true | ||
// RUN: %{built_tests_dir}/SelectedTest SelectedTest.ExecutedTestCase > %T/one_test_case || true | ||
// RUN: %{built_tests_dir}/SelectedTest > %T/all || true | ||
// RUN: %{xctest_checker} -p "// CHECK-METHOD: " %T/one_test_method %s | ||
// RUN: %{xctest_checker} -p "// CHECK-TESTCASE: " %T/one_test_case %s | ||
// RUN: %{xctest_checker} -p "// CHECK-ALL: " %T/all %s | ||
|
||
#if os(Linux) || os(FreeBSD) | ||
import XCTest | ||
#else | ||
import SwiftXCTest | ||
#endif | ||
|
||
class ExecutedTestCase: XCTestCase { | ||
static var allTests: [(String, ExecutedTestCase -> () throws -> Void)] { | ||
return [ | ||
("test_bar", test_bar), | ||
("test_foo", test_foo), | ||
] | ||
} | ||
|
||
// CHECK-METHOD: Test Case 'ExecutedTestCase.test_foo' started. | ||
// CHECK-METHOD: Test Case 'ExecutedTestCase.test_foo' passed \(\d+\.\d+ seconds\). | ||
// CHECK-TESTCASE: Test Case 'ExecutedTestCase.test_bar' started. | ||
// CHECK-TESTCASE: Test Case 'ExecutedTestCase.test_bar' passed \(\d+\.\d+ seconds\). | ||
// CHECK-ALL: Test Case 'ExecutedTestCase.test_bar' started. | ||
// CHECK-ALL: Test Case 'ExecutedTestCase.test_bar' passed \(\d+\.\d+ seconds\). | ||
func test_bar() {} | ||
|
||
// CHECK-TESTCASE: Test Case 'ExecutedTestCase.test_foo' started. | ||
// CHECK-TESTCASE: Test Case 'ExecutedTestCase.test_foo' passed \(\d+\.\d+ seconds\). | ||
// CHECK-ALL: Test Case 'ExecutedTestCase.test_foo' started. | ||
// CHECK-ALL: Test Case 'ExecutedTestCase.test_foo' passed \(\d+\.\d+ seconds\). | ||
func test_foo() {} | ||
} | ||
// CHECK-METHOD: Executed 1 test, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds | ||
// CHECK-TESTCASE: Executed 2 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds | ||
// CHECK-ALL: Executed 2 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds | ||
|
||
|
||
class SkippedTestCase: XCTestCase { | ||
static var allTests: [(String, SkippedTestCase -> () throws -> Void)] { | ||
return [("test_baz", test_baz)] | ||
} | ||
|
||
// CHECK-ALL: Test Case 'SkippedTestCase.test_baz' started. | ||
// CHECK-ALL: Test Case 'SkippedTestCase.test_baz' passed \(\d+\.\d+ seconds\). | ||
func test_baz() {} | ||
} | ||
// CHECK-ALL: Executed 1 test, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds | ||
|
||
XCTMain([ | ||
testCase(ExecutedTestCase.allTests), | ||
testCase(SkippedTestCase.allTests), | ||
]) | ||
|
||
// CHECK-METHOD: Total executed 1 test, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds | ||
// CHECK-TESTCASE: Total executed 2 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds | ||
// CHECK-ALL: Total executed 3 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loving these documentation updates! 😍