Skip to content

Commit

Permalink
add md tests, add cli arguments, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
GErP83 committed Feb 18, 2023
1 parent 412dd27 commit e259a0f
Show file tree
Hide file tree
Showing 23 changed files with 1,441 additions and 290 deletions.
45 changes: 2 additions & 43 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ let package = Package(
],
products: [
.executable(name: "testify", targets: ["testify"]),

.library(name: "TestifySDK", targets: ["TestifySDK"]),
.library(name: "TestifyJUnit", targets: ["TestifyJUnit"]),
.library(name: "TestifyMarkdown", targets: ["TestifyMarkdown"]),
.library(name: "TestifySDK", targets: ["TestifySDK"])
],
targets: [
.executableTarget(
name: "testify",
dependencies: [
.target(name: "TestifySDK"),
.target(name: "TestifyJUnit"),
.target(name: "TestifyMarkdown"),
.target(name: "TestifySDK")
]
),

Expand All @@ -29,24 +24,6 @@ let package = Package(
name: "TestifySDK",
dependencies: []
),
.target(
name: "TestifyJSON",
dependencies: [
.target(name: "TestifySDK"),
]
),
.target(
name: "TestifyJUnit",
dependencies: [
.target(name: "TestifySDK"),
]
),
.target(
name: "TestifyMarkdown",
dependencies: [
.target(name: "TestifySDK"),
]
),

// MARK: - test targets

Expand All @@ -56,23 +33,5 @@ let package = Package(
.target(name: "TestifySDK"),
]
),
.testTarget(
name: "TestifyJSONTests",
dependencies: [
.target(name: "TestifyJSON"),
]
),
.testTarget(
name: "TestifyJUnitTests",
dependencies: [
.target(name: "TestifyJUnit"),
]
),
.testTarget(
name: "TestifyMarkdownTests",
dependencies: [
.target(name: "TestifyMarkdown"),
]
),
]
)
17 changes: 11 additions & 6 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), or it'll miserably fail. 😉
Testify converts XCTest output into a proper structure (JSON, JUNIT, MD), or it'll miserably fail. 😉


## Install command line utility

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

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

## Usage

In your projetct folder run `swift test 2>&1 | testify` or
In your project folder run:

Just use the [Swift Package Manager](https://theswiftdev.com/2017/11/09/swift-package-manager-tutorial/) as usual:
* for JSON format: `swift test | testify json`
* for JUNIT format: `swift test | testify junit`
* for MD format: `swift test | testify md`

If no argument or wrong argument added then output format fallbacks to JSON!

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.0.0"),
.package(url: "https://github.com/binarybirds/testify", from: "1.1.0"),
```

⚠️ Don't forget to add "Testify" to your target as a dependency!
Expand All @@ -30,4 +36,3 @@ Just use the [Swift Package Manager](https://theswiftdev.com/2017/11/09/swift-pa
import Testify

let suite = TestSuite.parse("test-output-string")
```
10 changes: 0 additions & 10 deletions Sources/TestifyJSON/TestifyJSON.swift

This file was deleted.

64 changes: 0 additions & 64 deletions Sources/TestifyJUnit/TestifyJunit.swift

This file was deleted.

22 changes: 0 additions & 22 deletions Sources/TestifyMarkdown/TestifyMarkdown.swift

This file was deleted.

10 changes: 6 additions & 4 deletions Sources/TestifySDK/Codable/TestResultJunitEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct TestResultJunitEncoder: TestResultEncoder {
allTests += tests
allTimes += time
allFails += failureCount
restOfResult += "<testsuite name=\"\(name)\" tests=\"\(tests)\" skipped=\"0\" failures=\"\(failureCount)\" errors=\"0\" timestamp=\"\(start)\" hostname=\"JunitEncoder\" time=\"\(time)\">\n"
restOfResult += "<testsuite id=\"\(name)\" name=\"\(name)\" tests=\"\(tests)\" skipped=\"0\" failures=\"\(failureCount)\" errors=\"0\" timestamp=\"\(start)\" hostname=\"JunitEncoder\" time=\"\(time)\">\n"

for testCase in suite.cases {
let name = testCase.testName
Expand All @@ -56,10 +56,12 @@ public struct TestResultJunitEncoder: TestResultEncoder {
}
restOfResult += "</testsuite>\n"
}
restOfResult += "</testsuites>\n\n\n"
restOfResult += "</testsuites>\n"

var startResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
startResult += "<testsuites id=\"\(input.name)\" name=\"\(input.name)\" tests=\"\(allTests)\" failures=\"\(allFails)\" time=\"\(allTimes)\">\n"
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd_HHmmss"
var startResult = "\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
startResult += "<testsuites id=\"\(formatter.string(from: input.startDate))\" name=\"\(input.name)\" tests=\"\(allTests)\" failures=\"\(allFails)\" time=\"\(allTimes)\">\n"

return startResult + restOfResult
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TestifySDK/Codable/TestResultMarkdownEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct TestResultMarkdownEncoder: TestResultEncoder {

let suites: [TestSuite] = input.children.reduce([]) { $0 + $1.children }
for suite in suites {
result += "\n\n"
result += "\n"
let name = suite.name
let count = suite.cases.count
let time = suite.cases.reduce(0) { $0 + $1.duration }
Expand Down
14 changes: 14 additions & 0 deletions Sources/TestifySDK/Models/OutputFormat.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// File.swift
//
//
// Created by Lengyel Gábor on 2023. 02. 17..
//

import Foundation

public enum OutputFormat : String {
case json
case junit
case md
}
10 changes: 10 additions & 0 deletions Sources/TestifySDK/Models/TestSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

import Foundation

public extension TestSuite {

static func parse(_ input: String) -> TestSuite? {
let decoder = RawTestResultDecoder()
let suite = try! decoder.decode(input)
return suite
}

}

public struct TestSuite: Codable {
public let name: String
public let startDate: Date
Expand Down
63 changes: 46 additions & 17 deletions Sources/testify/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,49 @@
//

import Foundation
//import Testify
//
//var data: Data
//var input: String = ""
//repeat {
// data = FileHandle.standardInput.availableData
// input += String(data: data, encoding: .utf8)!
//} while (data.count > 0)
//
//
//
////let input = String(bytes: FileHandle.standardInput.availableData, encoding: .utf8)!
//let suite = TestSuite.parse(input)
//let encoder = JSONEncoder()
//encoder.outputFormatting = .prettyPrinted
//let jsonData = try! encoder.encode(suite)
//print(String(data: jsonData, encoding: .utf8)!)
import TestifySDK

let args = CommandLine.arguments
var format: String = OutputFormat.json.rawValue
let msg = "Testify output format is"
if (args.count >= 2) {
if let enumCase = OutputFormat(rawValue: args[1]) {
format = enumCase.rawValue
print(msg, "'\(format)'")
} else {
print("Unknown Testify output format, the format is 'json' for now. Available formats: 'json', 'junit', 'md'")
}
} else {
print(msg, "'json'")
}

var data: Data
var input: String = ""
repeat {
data = FileHandle.standardInput.availableData
input += String(data: data, encoding: .utf8)!
} while (data.count > 0)

let decoder = RawTestResultDecoder()
let suite = try decoder.decode(input)

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

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

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

default:
print("Unknown output format")
}
Loading

0 comments on commit e259a0f

Please sign in to comment.