Skip to content

Commit 338c2e0

Browse files
authored
Add swift-testing support to the xctest runner (#2554)
This adds support for swift-testing tests by checking for both the swift testing output as well as the normal xctest output.
1 parent dff720d commit 338c2e0

File tree

3 files changed

+194
-3
lines changed

3 files changed

+194
-3
lines changed

apple/testing/default_runner/ios_xctestrun_runner.template.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,17 +572,28 @@ if [[ "${ERROR_ON_NO_TESTS_RAN:-1}" == "1" ]]; then
572572
# Fail when bundle executes nothing
573573
no_tests_ran=false
574574
if [[ $parallel_testing_enabled == true ]]; then
575+
echo "Parallel testing is enabled" >&2
575576
# When executing tests in parallel, test start markers are absent when no
576577
# tests are run.
577-
test_execution_count=$(grep -c -e "Test suite '.*' started*" "$testlog")
578+
test_execution_count=$(grep -c -e "Test suite '.*' started.*" "$testlog")
578579
if [[ "$test_execution_count" == "0" ]]; then
579580
no_tests_ran=true
580581
fi
581582
else
583+
echo "Testing is serialized" >&2
582584
# Assume the final 'Executed N tests' or 'Executed 1 test' is the
583585
# total execution count for the test bundle.
584-
test_target_execution_count=$(grep -e "Executed [[:digit:]]\{1,\} tests*," "$testlog" | tail -n1)
585-
if echo "$test_target_execution_count" | grep -q -e "Executed 0 tests, with 0 failures"; then
586+
xctest_target_execution_count=$(grep -e "Executed [[:digit:]]\{1,\} test.*," "$testlog" | tail -n1)
587+
swift_testing_target_execution_count=$(grep -e "Test run with [[:digit:]]\{1,\} test.*" "$testlog" | tail -n1 || true)
588+
if echo "$xctest_target_execution_count" | grep -q -e "Executed 0 tests, with 0 failures" && \
589+
[ -z "$swift_testing_target_execution_count" ] ; then
590+
echo "No tests ran -> no count lines found" >&2
591+
no_tests_ran=true
592+
fi
593+
594+
if echo "$xctest_target_execution_count" | grep -q -e "Executed 0 tests, with 0 failures" && \
595+
echo "$swift_testing_target_execution_count" | grep -q -e "Test run with 0 tests" ; then
596+
echo "No tests ran -> count line was 0" >&2
586597
no_tests_ran=true
587598
fi
588599
fi

test/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ apple_shell_test(
248248
tags = common.skip_ci_tags,
249249
)
250250

251+
apple_shell_test(
252+
name = "ios_xctest_swift_testing_runner_unit_test_17.x",
253+
size = "large",
254+
src = "ios_xctest_swift_testing_runner_unit_test_17.x.sh",
255+
args = [
256+
"--ios_multi_cpus=sim_arm64,x86_64",
257+
],
258+
tags = common.skip_ci_tags,
259+
)
260+
251261
apple_shell_test(
252262
name = "ios_test_runner_ui_test",
253263
size = "large",
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#!/bin/bash
2+
3+
# Copyright 2018 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Integration tests for iOS test runner.
18+
19+
MIN_OS_IOS="17.0.0"
20+
21+
function set_up() {
22+
mkdir -p ios
23+
}
24+
25+
function tear_down() {
26+
rm -rf ios
27+
}
28+
29+
function create_sim_runners() {
30+
cat > ios/BUILD <<EOF
31+
load(
32+
"@build_bazel_rules_apple//apple:ios.bzl",
33+
"ios_application",
34+
"ios_unit_test"
35+
)
36+
load("@build_bazel_rules_swift//swift:swift.bzl",
37+
"swift_library"
38+
)
39+
load(
40+
"@build_bazel_rules_apple//apple/testing/default_runner:ios_xctestrun_runner.bzl",
41+
"ios_xctestrun_runner"
42+
)
43+
44+
ios_xctestrun_runner(
45+
name = "ios_x86_64_sim_runner_17",
46+
device_type = "iPhone 11",
47+
os_version = "17.2",
48+
)
49+
EOF
50+
}
51+
52+
function create_swift_testing_tests() {
53+
cat > ios/passing_swift_testing_test.swift <<EOF
54+
import Testing
55+
import XCTest
56+
57+
@Suite("Simple passing swift test suite")
58+
class SimpleSwiftTestingTest {
59+
@Test
60+
func testCaseOne() {
61+
let one = 1
62+
#expect(one == 1, "this test passes")
63+
}
64+
65+
@Test
66+
func testCaseTwo() {
67+
let two = 2
68+
#expect(two == 2, "this test passes")
69+
}
70+
}
71+
EOF
72+
73+
cat > ios/failing_swift_testing_test.swift <<EOF
74+
import Testing
75+
import XCTest
76+
77+
@Suite("Simple failing swift test suite")
78+
class SimpleSwiftTestingTest {
79+
@Test
80+
func testCaseOne() {
81+
82+
let one = 1
83+
#expect(one == 2, "this test fails")
84+
}
85+
}
86+
EOF
87+
88+
89+
cat > ios/empty_swift_testing_test.swift <<EOF
90+
import Testing
91+
import XCTest
92+
93+
@Suite("Simple empty swift test suite")
94+
class SimpleSwiftTestingTest {
95+
96+
}
97+
EOF
98+
99+
cat >> ios/BUILD <<EOF
100+
swift_library(
101+
name = "passing_swift_testing_test_lib",
102+
testonly = True,
103+
srcs = ["passing_swift_testing_test.swift"],
104+
)
105+
106+
swift_library(
107+
name = "failing_swift_testing_test_lib",
108+
testonly = True,
109+
srcs = ["failing_swift_testing_test.swift"],
110+
)
111+
112+
swift_library(
113+
name = "empty_swift_testing_test_lib",
114+
testonly = True,
115+
srcs = ["empty_swift_testing_test.swift"],
116+
)
117+
118+
ios_unit_test(
119+
name = "PassingSwiftTestingTest",
120+
deps = [":passing_swift_testing_test_lib"],
121+
minimum_os_version = "${MIN_OS_IOS}",
122+
runner = ":ios_x86_64_sim_runner_17",
123+
)
124+
125+
ios_unit_test(
126+
name = "FailingSwiftTestingTest",
127+
deps = [":failing_swift_testing_test_lib"],
128+
minimum_os_version = "${MIN_OS_IOS}",
129+
runner = ":ios_x86_64_sim_runner_17",
130+
)
131+
132+
ios_unit_test(
133+
name = "EmptySwiftTestingTest",
134+
deps = [":empty_swift_testing_test_lib"],
135+
minimum_os_version = "${MIN_OS_IOS}",
136+
runner = ":ios_x86_64_sim_runner_17",
137+
)
138+
EOF
139+
}
140+
141+
function do_ios_test() {
142+
do_test ios "--test_output=all" "--spawn_strategy=local" "$@"
143+
}
144+
145+
function test_ios_swift_testing_pass() {
146+
create_sim_runners
147+
create_swift_testing_tests
148+
do_ios_test //ios:PassingSwiftTestingTest || fail "should pass"
149+
expect_log "Suite \"Simple passing swift test suite\" passed"
150+
expect_log "Test run with 2 tests passed after"
151+
}
152+
153+
function test_ios_swift_testing_fail() {
154+
create_sim_runners
155+
create_swift_testing_tests
156+
(! do_ios_test //ios:FailingSwiftTestingTest) || fail "should not pass"
157+
expect_log "Suite \"Simple failing swift test suite\" failed after"
158+
expect_log "Test run with 1 test failed after"
159+
}
160+
161+
function test_ios_swift_testing_empty() {
162+
create_sim_runners
163+
create_swift_testing_tests
164+
(! do_ios_test //ios:EmptySwiftTestingTest) || fail "should not pass"
165+
expect_log "Suite \"Simple empty swift test suite\" passed"
166+
expect_log "Test run with 0 tests passed after"
167+
expect_log "error: no tests were executed, is the test bundle empty?"
168+
}
169+
170+
run_suite "ios_unit_test with iOS 17.x test runner bundling tests"

0 commit comments

Comments
 (0)