Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Test passing arguments

Tags:

swift

Does anyone know how to provide command-line argument for swift test ?

I have tried : swift test "myDBName"

But I got unexpected argument error.

List of possible arguments are :

OVERVIEW: Build and run tests

USAGE: swift test [options]

OPTIONS:
  --build-path            Specify build/cache directory [default: ./.build]
  --chdir, -C             Change working directory before any other operation
  --color                 Specify color mode (auto|always|never) [default: auto]
  --configuration, -c     Build with configuration (debug|release) [default: debug]
  --enable-prefetching    Enable prefetching in resolver
  --list-tests, -l        Lists test methods in specifier format
  --parallel              Run the tests in parallel.
  --skip-build            Skip building the test target
  --specifier, -s         Run a specific test class or method, Format: <test-module>.<test-case> or <test-module>.<test-case>/<test>
  --verbose, -v           Increase verbosity of informational output
  -Xcc                    Pass flag through to all C compiler invocations
  -Xlinker                Pass flag through to all linker invocations
  -Xswiftc                Pass flag through to all Swift compiler invocations
  --help                  Display available options

Is there any other way to pass args ? ( Environment variable, etc ? )

like image 918
CZ54 Avatar asked Aug 17 '18 10:08

CZ54


2 Answers

You can, indeed, use the environment to do it.

A test with the following contents:

final class HelloTests: XCTestCase {
    func testExample() {
        XCTAssertEqual(String(cString: getenv("SOMETHING")), "else")
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}

Will succeed, using the swift command line:

SOMETHING=else swift test
like image 168
David Berry Avatar answered Oct 12 '22 21:10

David Berry


(This is not explicitly mentioned in OP's question, but swift test refers to the test tool of the Swift Package Manager; likewise does the answer below)

Swift Package Manager Test Tool does not support passing or arguments

If we compare the --help for the Test (swift test) and the Run (swift run) tools, you may note that the USAGE grammar for the former does not allow passing arguments to the binary that the Test Tool wraps, something that is a feature for the latter:

$ swift test --help

OVERVIEW: Build and run tests
USAGE: swift test [options]

...

$ swift run --help

OVERVIEW: Build and run an executable product

USAGE: swift run [options] [executable [arguments ...]]

...

We may verify this if we visit the source code, where the ArgumentParser for the SwiftRunTool binds the command line arguments, whereas the ArgumentParser for SwiftTestTool does not.

If you believe this could be a useful feature, please have a look at the Support page of the Swift Package Manager at GitHub for instructions on how to propose new features / bug fixes.

Using conditional compilation flags

For completeness (in addition to making use of the environment), conditional compilation flags can be used, and possibly suffice, depending on the use case.

This is not providing actual values when launching the tests over the command line, but you may make use of conditional compilation flags (true/false) to the Swift compiler swiftc using the -D flag.

E.g., the following test would pass:

import XCTest

#if USE_LOCALHOST
let server_ip = "127.0.0.1"
#else
let server_ip = "1.2.3.4"
#endif

final class HelloTests: XCTestCase {
    func testExample() {
        XCTAssertEqual(server_ip, "127.0.0.1")
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}

if the test suite is launched using:

$ swift test -Xswiftc -DUSE_LOCALHOST

Whereas the test would fail if launching using:

$ swift test
like image 44
dfrib Avatar answered Oct 12 '22 22:10

dfrib