-
Notifications
You must be signed in to change notification settings - Fork 263
[SR-1541] Listing test methods #114
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4cb8f22
Add a command line flag for printing a list of the tests in the suite.
briancroom 58fab00
Add a JSON output format for listing tests
briancroom ea21826
Include the new test listing mode in the documentation.
briancroom 6be3a11
Include total count when listing tests
briancroom 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
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,107 @@ | ||
// 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 | ||
// | ||
// | ||
// TestListing.swift | ||
// Implementation of the mode for printing the list of tests. | ||
// | ||
|
||
#if os(Linux) || os(FreeBSD) | ||
import Foundation | ||
#else | ||
import SwiftFoundation | ||
#endif | ||
|
||
internal struct TestListing { | ||
private let testSuite: XCTestSuite | ||
|
||
init(testSuite: XCTestSuite) { | ||
self.testSuite = testSuite | ||
} | ||
|
||
/// Prints a flat list of the tests in the suite, in the format used to | ||
/// specify a test by name when running tests. | ||
func printTestList() { | ||
let list = testSuite.list() | ||
let tests = list.count == 1 ? "test" : "tests" | ||
let bundleName = testSuite.findBundleTestSuite()?.name ?? "<<unknown bundle>>" | ||
|
||
print("Listing \(list.count) \(tests) in \(bundleName):\n") | ||
for entry in testSuite.list() { | ||
print(entry) | ||
} | ||
} | ||
|
||
/// Prints a JSON representation of the tests in the suite, mirring the internal | ||
/// tree representation of test suites and test cases. This output is intended | ||
/// to be consumed by other tools. | ||
func printTestJSON() { | ||
let json = try! NSJSONSerialization.data(withJSONObject: testSuite.dictionaryRepresentation()) | ||
print(NSString(data: json, encoding: NSUTF8StringEncoding)!) | ||
} | ||
} | ||
|
||
protocol Listable { | ||
func list() -> [String] | ||
func dictionaryRepresentation() -> NSDictionary | ||
} | ||
|
||
private func moduleName(value: Any) -> String { | ||
let moduleAndType = String(reflecting: value.dynamicType) | ||
return String(moduleAndType.characters.split(separator: ".").first!) | ||
} | ||
|
||
extension XCTestSuite: Listable { | ||
private var listables: [Listable] { | ||
return tests | ||
.flatMap({ ($0 as? Listable) }) | ||
} | ||
|
||
private var listingName: String { | ||
if let childTestCase = tests.first as? XCTestCase where name == String(childTestCase.dynamicType) { | ||
return "\(moduleName(value: childTestCase)).\(name)" | ||
} else { | ||
return name | ||
} | ||
} | ||
|
||
func list() -> [String] { | ||
return listables.flatMap({ $0.list() }) | ||
} | ||
|
||
func dictionaryRepresentation() -> NSDictionary { | ||
let listedTests = tests.flatMap({ ($0 as? Listable)?.dictionaryRepresentation() }) | ||
return [ | ||
"name": listingName.bridge(), | ||
"tests": listedTests.bridge() | ||
].bridge() | ||
} | ||
|
||
func findBundleTestSuite() -> XCTestSuite? { | ||
if name.hasSuffix(".xctest") { | ||
return self | ||
} else { | ||
return tests.flatMap({ ($0 as? XCTestSuite)?.findBundleTestSuite() }).first | ||
} | ||
} | ||
} | ||
|
||
extension XCTestCase: Listable { | ||
func list() -> [String] { | ||
let adjustedName = name.characters | ||
.split(separator: ".") | ||
.map(String.init) | ||
.joined(separator: "/") | ||
return ["\(moduleName(value: self)).\(adjustedName)"] | ||
} | ||
|
||
func dictionaryRepresentation() -> NSDictionary { | ||
let methodName = String(name.characters.split(separator: ".").last!) | ||
return ["name": methodName].bridge() | ||
} | ||
} |
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,71 @@ | ||
// RUN: %{swiftc} %s -o %{built_tests_dir}/ListTests | ||
// RUN: %{built_tests_dir}/ListTests --list-tests > %t_list || true | ||
// RUN: %{xctest_checker} %t_list %s | ||
// RUN: %{built_tests_dir}/ListTests --dump-tests-json > %t_json || true | ||
// RUN: %{built_tests_dir}/ListTests --verify %t_json > %t_verify | ||
// RUN: %{xctest_checker} %t_verify verify_json.expected | ||
|
||
#if os(Linux) || os(FreeBSD) | ||
import XCTest | ||
import Foundation | ||
#else | ||
import SwiftXCTest | ||
import SwiftFoundation | ||
#endif | ||
|
||
// The JSON output isn't a stable enough format to use FileCheck-style line | ||
// verification directly. Instead, verify the output by deserializing the output | ||
// a stable representation of the test tree for checking. | ||
if Process.arguments.contains("--verify") { | ||
func dump(_ value: Any, prefix: String = "") { | ||
guard let object = value as? [String: Any] else { return print("<<wrong type>>") } | ||
guard let name = object["name"] as? String else { return print("<<missing name>>") } | ||
print(prefix + name) | ||
guard let children = object["tests"] as? [Any] else { return } | ||
children.forEach { | ||
dump($0, prefix: prefix + " ") | ||
} | ||
} | ||
|
||
let deserialized = try! NSJSONSerialization.jsonObject(with: NSData(contentsOfFile: Process.arguments[2])!) | ||
dump(deserialized) | ||
exit(0) | ||
} | ||
|
||
// CHECK: Listing 4 tests in .*\.xctest: | ||
// CHECK: ^$ | ||
|
||
class FirstTestCase: XCTestCase { | ||
static var allTests = { | ||
return [ | ||
("test_foo", test_foo), | ||
("test_bar", test_bar), | ||
] | ||
}() | ||
|
||
// CHECK: ListTests.FirstTestCase/test_foo | ||
func test_foo() {} | ||
|
||
// CHECK: ListTests.FirstTestCase/test_bar | ||
func test_bar() {} | ||
} | ||
|
||
class SecondTestCase: XCTestCase { | ||
static var allTests = { | ||
return [ | ||
("test_someMore", test_someMore), | ||
("test_allTheThings", test_allTheThings), | ||
] | ||
}() | ||
|
||
// CHECK: ListTests.SecondTestCase/test_someMore | ||
func test_someMore() {} | ||
|
||
// CHECK: ListTests.SecondTestCase/test_allTheThings | ||
func test_allTheThings() {} | ||
} | ||
|
||
XCTMain([ | ||
testCase(FirstTestCase.allTests), | ||
testCase(SecondTestCase.allTests), | ||
]) |
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,8 @@ | ||
// CHECK: ^All tests | ||
// CHECK: ^ .*\.xctest | ||
// CHECK: ^ ListTests.FirstTestCase | ||
// CHECK: ^ test_foo | ||
// CHECK: ^ test_bar | ||
// CHECK: ^ ListTests.SecondTestCase | ||
// CHECK: ^ test_someMore | ||
// CHECK: ^ test_allTheThings |
Oops, something went wrong.
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.
It would be really cool if, in a future pull request, we can expand the argument parsing here to include
-h
and--help
. SwiftPM also implements its own argument parsing--perhaps we can extract this out into a library of code shared between SwiftPM and corelibs-xctest?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.
I do hope to factor out the argument parser more, although for practical reasons it may end up being intertwined with our own internal infrastructure. The ideal would be for the stdlib to help... :)
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.
Indeed! Once this goes in I was intending to open a Starter Issue for getting help/usage added.