Skip to content

Commit

Permalink
Version 1.1.2 (#6)
Browse files Browse the repository at this point in the history
* Feature: Add GFM (GitHub Flavored Markdown) format support. (#5)

* Add the `gfm` (Github Flavored Markdown) output format and improve the handling logic of the OutputFormat cases.

* Implement an encoder for the gfm format.

* Update README.md

---------

Co-authored-by: Kanstantsin Bucha <[email protected]>
  • Loading branch information
GErP83 and kanstantsin-bucha authored May 18, 2023
1 parent 25aa799 commit 2887241
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Testify

Testify converts XCTest output into a proper structure (JSON, JUNIT, MD), or it'll miserably fail. 😉
Testify converts XCTest output into a proper structure (JSON, JUNIT, MD, GFM), or it'll miserably fail. 😉


## Install command line utility

You can use the command line utility to convert test results into JSON, JUNIT and MD on the fly.
You can use the command line utility to convert test results into JSON, JUNIT, MD and GFM on the fly.

```
git clone https://github.com/BinaryBirds/Testify.git && cd Testify
Expand All @@ -17,14 +17,15 @@ which testify

In your project folder run:

* for JSON format: `swift test | testify json`
* for JSON format: `swift test | testify json`
* for JUNIT format: `swift test | testify junit`
* for MD format: `swift test | testify md`
* for GFM format: `swift test | testify gfm`

You can just use the [Swift Package Manager](https://theswiftdev.com/2017/11/09/swift-package-manager-tutorial/) as usual:

```swift
.package(url: "https://github.com/binarybirds/testify", from: "1.1.1"),
.package(url: "https://github.com/binarybirds/testify", from: "1.1.2"),
```

⚠️ Don't forget to add "Testify" to your target as a dependency!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Foundation

public struct TestResultGitHubFlavoredMarkdownEncoder: TestResultEncoder {

public init() {

}

public func encode(_ input: TestSuite) throws -> String {
var totalTestsCount = 0
var totalTime: Double = 0
var totalSucceedCount = 0
var totalFailedCount = 0
var result = ""

let suites: [TestSuite] = input.children.reduce([]) { $0 + $1.children }
for suite in suites {
result += "\n"
let name = suite.name
let count = suite.cases.count
let time = suite.cases.reduce(0) { $0 + $1.duration }
let successCount = suite.cases.reduce(0) { $0 + ($1.outcome == .success ? 1 : 0) }
let failureCount = suite.cases.reduce(0) { $0 + ($1.outcome == .failure ? 1 : 0) }

totalTestsCount += count
totalTime += time
totalSucceedCount += successCount
totalFailedCount += totalFailedCount

result += "<details>\n"
let suiteResult = count == successCount ? "" : ""
result += "<summary> \(suiteResult) \(name): \(count) tests were completed in \(timeString(time)) with \(successCount) passed, \(failureCount) failed.</summary>\n"

for testCase in suite.cases {
let name = testCase.testName
let testResult = testCase.outcome == .success ? "" : ""
let time = timeString(testCase.duration)
result += "| \(testResult) \(time) | \(name) <br>\n"
}

result += "<br>\n"
result += "</details>\n"
}

let testsRunResult = totalTestsCount == totalSucceedCount ? "" : ""
var testsResult = "# \(testsRunResult) \(totalTestsCount) tests were completed in \(timeString(totalTime))"
testsResult += "\n \n"
testsResult += "\(totalSucceedCount) tests passed, \(totalFailedCount) test failed.\n"
testsResult += "\n----\n"
testsResult += result

return testsResult
}

private func timeString(_ sec: Double) -> String {
"\(String(format: "%.2f", sec))s"
}
}
3 changes: 2 additions & 1 deletion Sources/TestifySDK/Models/OutputFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import Foundation

public enum OutputFormat : String {
public enum OutputFormat : String, CaseIterable {
case json
case junit
case md
case gfm
}
25 changes: 15 additions & 10 deletions Sources/testify/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import Foundation
import TestifySDK

let args = CommandLine.arguments
var format: String = OutputFormat.json.rawValue
var outputFormat: OutputFormat = .json
if (args.count >= 2) {
if let enumCase = OutputFormat(rawValue: args[1]) {
format = enumCase.rawValue
if let format = OutputFormat(rawValue: args[1]) {
outputFormat = format
} else {
fatalError("Error: Unknown output format. Available formats: 'json', 'junit', 'md'")
let formats = OutputFormat.allCases
.map { "'\($0)'"}
.joined(separator: ", ")
fatalError("Error: Unknown output format. Available formats: \(formats)")
}
}

Expand All @@ -28,23 +31,25 @@ repeat {
let decoder = RawTestResultDecoder()
let suite = try decoder.decode(input)

switch format {
case OutputFormat.json.rawValue:
switch outputFormat {
case .json:
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let jsonData = try! encoder.encode(suite)
print("\n", String(data: jsonData, encoding: .utf8)!, "\n")

case OutputFormat.junit.rawValue:
case .junit:
let encoder = TestResultJunitEncoder()
let junitData = try! encoder.encode(suite)
print(junitData)

case OutputFormat.md.rawValue:
case .md:
let encoder = TestResultMarkdownEncoder()
let mdData = try! encoder.encode(suite)
print(mdData)

default:
fatalError("Error: Unknown output format")
case .gfm:
let encoder = TestResultGitHubFlavoredMarkdownEncoder()
let mdData = try! encoder.encode(suite)
print(mdData)
}

0 comments on commit 2887241

Please sign in to comment.