forked from bats-core/bats-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
junit-formatter.bats
140 lines (119 loc) · 6.09 KB
/
junit-formatter.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bats
load test_helper
fixtures junit-formatter
FLOAT_REGEX='[0-9]+(\.[0-9]+)?'
TIMESTAMP_REGEX='[0-9]+-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]'
TESTSUITES_REGEX="<testsuites time=\"$FLOAT_REGEX\">"
@test "junit formatter with skipped test does not fail" {
run bats --formatter junit "$FIXTURE_ROOT/skipped.bats"
echo "$output"
[[ $status -eq 0 ]]
[[ "${lines[0]}" == '<?xml version="1.0" encoding="UTF-8"?>' ]]
[[ "${lines[1]}" =~ $TESTSUITES_REGEX ]]
TESTSUITE_REGEX="<testsuite name=\"skipped.bats\" tests=\"2\" failures=\"0\" errors=\"0\" skipped=\"2\" time=\"$FLOAT_REGEX\" timestamp=\"$TIMESTAMP_REGEX\" hostname=\".*\">"
echo "TESTSUITE_REGEX='$TESTSUITE_REGEX'"
[[ "${lines[2]}" =~ $TESTSUITE_REGEX ]]
TESTCASE_REGEX="<testcase classname=\"skipped.bats\" name=\"a skipped test\" time=\"$FLOAT_REGEX\">"
[[ "${lines[3]}" =~ $TESTCASE_REGEX ]]
[[ "${lines[4]}" == *"<skipped></skipped>"* ]]
[[ "${lines[5]}" == *"</testcase>"* ]]
TESTCASE_REGEX="<testcase classname=\"skipped.bats\" name=\"a skipped test with a reason\" time=\"$FLOAT_REGEX\">"
[[ "${lines[6]}" =~ $TESTCASE_REGEX ]]
[[ "${lines[7]}" == *"<skipped>a reason</skipped>"* ]]
[[ "${lines[8]}" == *"</testcase>"* ]]
[[ "${lines[9]}" == *"</testsuite>"* ]]
[[ "${lines[10]}" == *"</testsuites>"* ]]
}
@test "junit formatter: escapes xml special chars" {
case $OSTYPE in
linux*|darwin)
# their CI can handle special chars on filename
TEST_FILE_NAME="xml-escape-\"<>'&.bats"
ESCAPED_TEST_FILE_NAME="xml-escape-"<>'&.bats"
TEST_FILE_PATH="$BATS_TEST_TMPDIR/$TEST_FILE_NAME"
cp "$FIXTURE_ROOT/xml-escape.bats" "$TEST_FILE_PATH"
;;
*)
# use the filename without special chars
TEST_FILE_NAME="xml-escape.bats"
ESCAPED_TEST_FILE_NAME="$TEST_FILE_NAME"
TEST_FILE_PATH="$FIXTURE_ROOT/$TEST_FILE_NAME"
;;
esac
run bats --formatter junit "$TEST_FILE_PATH"
echo "$output"
[[ "${lines[2]}" == "<testsuite name=\"$ESCAPED_TEST_FILE_NAME\" tests=\"3\" failures=\"1\" errors=\"0\" skipped=\"1\" time=\""*"\" timestamp=\""*"\" hostname=\""*"\">" ]]
[[ "${lines[3]}" == " <testcase classname=\"$ESCAPED_TEST_FILE_NAME\" name=\"Successful test with escape characters: "'<>&[0m (0x1b)\" time=\""*"\" />" ]]
[[ "${lines[4]}" == " <testcase classname=\"$ESCAPED_TEST_FILE_NAME\" name=\"Failed test with escape characters: "'<>&[0m (0x1b)\" "* ]]
[[ "${lines[5]}" == ' <failure type="failure">(in test file '*"$ESCAPED_TEST_FILE_NAME, line 6)" ]]
[[ "${lines[6]}" == ' 'echo "<>'&[0m" && false' failed'* ]]
[[ "${lines[9]}" == " <testcase classname=\"$ESCAPED_TEST_FILE_NAME\" name=\"Skipped test with escape characters: "'<>&[0m (0x1b)\" time=\""*"\">" ]]
[[ "${lines[10]}" == " <skipped>"'<>&[0m</skipped>" ]]
}
@test "junit formatter: test suites" {
run bats --formatter junit "$FIXTURE_ROOT/suite/"
echo "$output"
[[ "${lines[0]}" == '<?xml version="1.0" encoding="UTF-8"?>' ]]
[[ "${lines[1]}" == *"<testsuites "* ]]
[[ "${lines[2]}" == *"<testsuite name=\"file1.bats\""* ]]
[[ "${lines[3]}" == *"<testcase "* ]]
[[ "${lines[4]}" == *"</testsuite>"* ]]
[[ "${lines[5]}" == *"<testsuite name=\"file2.bats\""* ]]
[[ "${lines[6]}" == *"<testcase"* ]]
[[ "${lines[7]}" == *"</testsuite>"* ]]
[[ "${lines[8]}" == *"</testsuites>"* ]]
}
@test "junit formatter: test suites relative path" {
cd "$FIXTURE_ROOT"
run bats --formatter junit "suite/"
echo "$output"
[[ "${lines[0]}" == '<?xml version="1.0" encoding="UTF-8"?>' ]]
[[ "${lines[1]}" == *"<testsuites "* ]]
[[ "${lines[2]}" == *"<testsuite name=\"file1.bats\""* ]]
[[ "${lines[3]}" == *"<testcase "* ]]
[[ "${lines[4]}" == *"</testsuite>"* ]]
[[ "${lines[5]}" == *"<testsuite name=\"file2.bats\""* ]]
[[ "${lines[6]}" == *"<testcase"* ]]
[[ "${lines[7]}" == *"</testsuite>"* ]]
[[ "${lines[8]}" == *"</testsuites>"* ]]
}
@test "junit formatter: files with the same name are distinguishable" {
run bats --formatter junit -r "$FIXTURE_ROOT/duplicate/"
echo "$output"
[[ "${lines[2]}" == *"<testsuite name=\"first/file1.bats\""* ]]
[[ "${lines[5]}" == *"<testsuite name=\"second/file1.bats\""* ]]
}
@test "junit formatter as report formatter creates report.xml" {
cd "$BATS_TEST_TMPDIR" # don't litter sources with output files
run bats --report-formatter junit "$FIXTURE_ROOT/suite/"
echo "$output"
[[ -e "report.xml" ]]
run cat "report.xml"
echo "$output"
[[ "${lines[2]}" == *"<testsuite name=\"file1.bats\" tests=\"1\" failures=\"0\" errors=\"0\" skipped=\"0\""* ]]
[[ "${lines[5]}" == *"<testsuite name=\"file2.bats\" tests=\"1\" failures=\"0\" errors=\"0\" skipped=\"0\""* ]]
}
@test "junit does not mark tests with FD 3 output as failed (issue #360)" {
run bats --formatter junit "$FIXTURE_ROOT/issue_360.bats"
echo "$output"
[[ "${lines[2]}" == '<testsuite name="issue_360.bats" '*'>' ]]
[[ "${lines[3]}" == ' <testcase classname="issue_360.bats" '*'>' ]]
# only the outputs on FD3 should be visible on a successful test
[[ "${lines[4]}" == ' <system-out>setup FD3' ]]
[[ "${lines[5]}" == 'hello Bilbo' ]]
[[ "${lines[6]}" == 'teardown FD3</system-out>' ]]
[[ "${lines[7]}" == ' </testcase>' ]]
[[ "${lines[8]}" == ' <testcase classname="issue_360.bats" name="fail to say hello to Biblo" time="'*'">' ]]
# a failed test should show FD3 output first ...
[[ "${lines[9]}" == ' <system-out>setup FD3' ]]
[[ "${lines[10]}" == 'hello Bilbo' ]]
[[ "${lines[11]}" == 'teardown FD3</system-out>' ]]
[[ "${lines[12]}" == ' <failure type="failure">(in test file '*'test/fixtures/junit-formatter/issue_360.bats, line 21)' ]]
[[ "${lines[13]}" == ' 'false' failed' ]]
# ... and then the stdout output
[[ "${lines[14]}" == '# setup stdout' ]]
[[ "${lines[15]}" == '# hello stdout' ]]
[[ "${lines[16]}" == '# teardown stdout</failure>' ]]
[[ "${lines[17]}" == ' </testcase>' ]]
[[ "${lines[18]}" == '</testsuite>' ]]
}