swift-junit icon indicating copy to clipboard operation
swift-junit copied to clipboard

Test xml.file is not generated

Open derLalla opened this issue 4 years ago • 2 comments

Hi, I am working on a Mac and I am building a plain Swift project (no Xcode project). I've done all steps in the Readme (without the steps for Xcode, as it is no Xcode project) but no test.xml file is getting generated. What am I doing wrong or where can I find the generated junit xml-file?

I am doing the following:

swift build
Fetching https://github.com/alexaubry/HTMLString.git
Fetching https://github.com/allegro/swift-junit.git
Cloning https://github.com/alexaubry/HTMLString.git
Resolving https://github.com/alexaubry/HTMLString.git at 5.0.0
Cloning https://github.com/allegro/swift-junit.git
Resolving https://github.com/allegro/swift-junit.git at 2.0.0

swift test
...
Test Suite 'All tests' passed at 2020-11-02 14:52:15.834.
         Executed 2 tests, with 0 failures (0 unexpected) in 0.086 (0.086) seconds

This is my Package.swift:

// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "swiftExercise",
    dependencies: [
        .package(name: "SwiftTestReporter", url: "https://github.com/allegro/swift-junit.git", from: "2.0.0"),
    ],
    targets: [
        .target(name: "swiftExerciseLib"),
        .target(name: "swiftExerciseApp", dependencies: ["swiftExerciseLib"]),
        .testTarget(name: "swiftExerciseTests", dependencies: ["swiftExerciseLib", "SwiftTestReporter"]),
    ]
)

My Swift version: Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)

derLalla avatar Nov 02 '20 13:11 derLalla

Please try to add following code:

class TestObserverTestCase: XCTestCase {
  private static var initialized = false

  open override func setUp() {
    if !TestObserverTestCase.initialized {
      TestObserverTestCase.initialized = true
      _ = TestObserver()
    }
    super.setUp()
  }
}

Then modify your tests, so as to inherit from this particular class, it should works.

ar4s avatar Nov 02 '20 19:11 ar4s

Thanks, but I had to modify it a bit for it to work. This needs to be called before the very first test case is executed otherwise the first testcase is missing out in the resulting tests.xml. So I've created a

/** 
  * This init class needs to be the first executed test case. 
  * It is needed to set the TestObserver once to create the tests.xml file on macOS
  * Class name starts with 'AA' to be the first executed test.
  */
class AA_InitTest: XCTestCase {
    override class func setUp() {
        _ = TestObserver()
        super.setUp()
    }

    // dummy test method to be interpreted as a test case
    func testInit() {}
}

This has to be the first executed test case and leads to all subsequent test cases being included in the tests.xml.

I also think that this approach wouldn't break the LinuxMain.swift, as the testInit() function is not included in the overall allTests variable. So I hope this won't get executed. Because setting _ = TestObserver() twice will lead to an double output of The file tests.xml was created in the console.

derLalla avatar Nov 11 '20 07:11 derLalla