Skip to content

Show the number of test cases that ran at the end of a test #972

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

Conversation

hrayatnia
Copy link
Contributor

@hrayatnia hrayatnia commented Feb 23, 2025

Motivation:

To have the number of test cases at the end of the test in for parameterized tests, so the logs don’t easily get buried.

Modifications:

Log the number of test cases for parameterized tests at the end of a test.

Result:

If the user runs the parameterized tests, an extra piece of information (with * test cases) would get bound to the end log.

Resolves #943.
Resolves rdar://144250498.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

@hrayatnia hrayatnia changed the title [943] minor change: - show number of args passed to at the end of a test #943 minor change: - show number of args passed to at the end of a test Feb 23, 2025
@hrayatnia hrayatnia changed the title #943 minor change: - show number of args passed to at the end of a test [943] minor change: - show number of args passed to at the end of a test Feb 23, 2025
Copy link
Contributor

@grynspan grynspan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this PR! It would be a good idea to familiarize yourself with the existing code in Event.HumanReadableOutputRecorder.swift and get a feel for how it works and the existing mechanisms we use for gathering data during test runs.

return " with \(numberOfArguments) " + (numberOfArguments > 1 ? "arguments" : "argument")
private func _includeNumberOfTestCases(for test: Test, verbose: Int) -> String {
if verbose == 2 && !test.isSuite { // very verbose
let testCasesCount = test.testCases?.count(where: { _ in true }) ?? 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling testCases here will produce a second sequence of test cases, which makes getting the number of test cases an O(n) operation instead of O(1). What we would normally do is keep a running count of test cases in the TestData structure, incrementing it for each .testCaseStarted event.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, it helps me understand the way you guys think 🙏.

Honestly, it was my first idea; however, I thought it is the same due to the fact that O(n) gets distributed among each test run and also adds a negligible amount of miscalculation to the compute time (because it happens after the start instant as well as it adds 8 bytes of additional data to the test data). For those reasons, I went with the second solution.

I fixed the issue.

let numberOfArguments = _numberOfArguments(for: test.testCases)
return " with \(numberOfArguments) " + (numberOfArguments > 1 ? "arguments" : "argument")
private func _includeNumberOfTestCases(for test: Test, verbose: Int) -> String {
if verbose == 2 && !test.isSuite { // very verbose
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably mean >= 2, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if you want to count test cases, you should use test.isParameterized, not !test.isSuite, since it doesn't make sense to count test cases for a non-parameterized test function either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

private func _includeNumberOfTestCases(for test: Test, verbose: Int) -> String {
if verbose == 2 && !test.isSuite { // very verbose
let testCasesCount = test.testCases?.count(where: { _ in true }) ?? 0
return " with \(testCasesCount) " + (testCasesCount > 1 ? "test cases" : "test case")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a helper function for pluralizing English nouns (used elsewhere in this file.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, applied. 🙏

/// - verbose: If the level is very verbose, a detailed description is returned.
///
/// - Returns: A string describing the number of arguments in the test cases, or an empty string if it's not very verbose level.
/// - Returns: A string describing the number of argument(s) in the test cases, or an empty string if it's not very verbose level.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation comments (but not code) should wrap at 80 columns. This helps give readers a sense of how long the documentation will be when rendered with DocC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied, sorry for not double-checking it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For more details see the Style Guide.

…eedback

- use counting instead of ternary operator
- fixing char limit per row
- fixing verbosity condition check
- check if test is parameterized

case .testCaseStarted:
guard verbosity >= 2 else { break }
let id: [String] = if let test {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't check verbosity in this first switch intentionally so that the data we collect is always comprehensive. The second switch below checks verbosity before producing any messages.

test will never be nil for this event, and .testStarted will always be recorded before .testCaseStarted, so you can just write:

case .testCaseStarted:
  let test = test!
  context.testData[test.id.keyPathRepresentation]?.testCasesCount += 1

/// - Returns: A string describing the number of test cases in the test,
/// or an empty string if it's not very verbose level.
///
private func _includeNumberOfTestCasesIfNeeded(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably don't need this helper function and can just construct the string in place in the .testEnded handler below.

@@ -366,18 +404,21 @@ extension Event.HumanReadableOutputRecorder {
let testData = testDataGraph?.value ?? .init(startInstant: instant)
let issues = _issueCounts(in: testDataGraph)
let duration = testData.startInstant.descriptionOfDuration(to: instant)
let testCasesCountMessage = _includeNumberOfTestCasesIfNeeded(test.isParameterized,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you can probably just write:

let testCasesCount = if verbosity >= 2 && test.isParameterized {
  " with \(testData.testCasesCount.counting("test case"))"
} else {
  ""
}

("Message" has a domain-specific meaning in this file, which is why I renamed this variable in my example above.)

@grynspan grynspan changed the title [943] minor change: - show number of args passed to at the end of a test Show the number of test cases that ran at the end of a test Feb 24, 2025
@grynspan grynspan added enhancement New feature or request tools integration 🛠️ Integration of swift-testing into tools/IDEs command-line experience ⌨️ enhancements to the command line interface labels Feb 24, 2025
@grynspan grynspan added this to the Swift 6.x milestone Feb 24, 2025
@hrayatnia
Copy link
Contributor Author

@grynspan Thank you for being patience with me. I've applied all the requested changes—please review and resolve if everything looks good. Also, I'll make an effort to participate more in code reviews and go through other pull requests to minimize trivial questions in the future.

@stmontgomery Thank you for your support as well. 🙏

@hrayatnia hrayatnia marked this pull request as ready for review February 24, 2025 20:53
@hrayatnia hrayatnia requested a review from grynspan February 24, 2025 20:59
@@ -146,6 +149,7 @@ extension Event.HumanReadableOutputRecorder {

return (errorIssueCount, warningIssueCount, knownIssueCount, totalIssueCount, description)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: stray newline? :)

@@ -566,3 +610,4 @@ struct EventRecorderTests {
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: stray newline?

@grynspan
Copy link
Contributor

@swift-ci test

@grynspan
Copy link
Contributor

@swift-ci test

@@ -366,18 +373,23 @@ extension Event.HumanReadableOutputRecorder {
let testData = testDataGraph?.value ?? .init(startInstant: instant)
let issues = _issueCounts(in: testDataGraph)
let duration = testData.startInstant.descriptionOfDuration(to: instant)
let testCasesCount = if verbosity >= 2 && test.isParameterized {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stmontgomery What do you think about the required verbosity here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should always include this, and not conditionalize it on verbosity. It's only a few extra words, not adding any additional lines, and I think it's a generally useful enhancement. The current summary text always includes a count of the issues recorded regardless of verbosity, and this isn't all that different.

(We should continue to check test.isParameterized, though.)

Comment on lines 186 to 190
("f()", #".* Test f\(\) failed after .*"# , 0),
("f()", #".* Test f\(\) failed after .*"# , 2),
("d(_:)", #".* Test d\(_:\) with .+ test cases passed after.*"# , 2),
("PredictablyFailingTests", #".* Suite PredictablyFailingTests failed after .*"# , 1),
("PredictablyFailingTests", #".* Suite PredictablyFailingTests failed after .*"# , 2),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: whitespace

Suggested change
("f()", #".* Test f\(\) failed after .*"# , 0),
("f()", #".* Test f\(\) failed after .*"# , 2),
("d(_:)", #".* Test d\(_:\) with .+ test cases passed after.*"# , 2),
("PredictablyFailingTests", #".* Suite PredictablyFailingTests failed after .*"# , 1),
("PredictablyFailingTests", #".* Suite PredictablyFailingTests failed after .*"# , 2),
("f()", #".* Test f\(\) failed after .*"#, 0),
("f()", #".* Test f\(\) failed after .*"#, 2),
("d(_:)", #".* Test d\(_:\) with .+ test cases passed after.*"#, 2),
("PredictablyFailingTests", #".* Suite PredictablyFailingTests failed after .*"#, 1),
("PredictablyFailingTests", #".* Suite PredictablyFailingTests failed after .*"#, 2),

@@ -62,6 +62,9 @@ extension Event {

/// The number of known issues recorded for the test.
var knownIssueCount = 0

/// The number of test cases for the test.
var testCasesCount: Int = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can omit the explicit type since this isn't a public property, to match prevailing style

Suggested change
var testCasesCount: Int = 0
var testCasesCount = 0

@stmontgomery
Copy link
Contributor

@swift-ci please test


@available(_regexAPI, *)
@Test(
"number of arguments based on verbosity level at the end of test run",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The display name of this test can be rephrased now to not mention verbosity. I'd also prefer if we could capitalize the first word to match prevailing style.

Copy link
Contributor

@stmontgomery stmontgomery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple more style comments but besides that I think this is ready to land. Thanks @hrayatnia!

@stmontgomery
Copy link
Contributor

@swift-ci please test

@stmontgomery stmontgomery merged commit f9e1701 into swiftlang:main Feb 25, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
command-line experience ⌨️ enhancements to the command line interface enhancement New feature or request tools integration 🛠️ Integration of swift-testing into tools/IDEs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Log the number of test cases run when a test function finishes
3 participants