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 ? )
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
(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)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With