Skip to content

Commit f69bcb2

Browse files
committed
refactor: reconfigure the tests
1 parent 353709f commit f69bcb2

File tree

5 files changed

+234
-167
lines changed

5 files changed

+234
-167
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ jobs:
3232
- if: matrix.ruby != 'jruby'
3333
run: bundle exec rake test
3434
- if: matrix.ruby == 'jruby'
35-
run: bundle exec rake test TESTOPTS='--ignore-name=/multibyte/'
35+
run: bundle exec rake test TESTOPTS='--ignore-testcase=TestXmlMultibyteName'

test/check.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require "test/unit/assertions"
2+
3+
module Check
4+
def check_testsuite(testsuite, name, tests, errors, failures, skipped)
5+
assert_equal(name, testsuite.attribute("name").value)
6+
assert_equal(tests, testsuite.get_elements("testcase").size)
7+
assert_equal(tests.to_s, testsuite.attribute("tests").value)
8+
assert_equal(errors.to_s, testsuite.attribute("errors").value)
9+
assert_equal(failures.to_s, testsuite.attribute("failures").value)
10+
assert_equal(skipped.to_s, testsuite.attribute("skipped").value)
11+
assert_compare(0, "<", Float(testsuite.attribute("time").value))
12+
end
13+
14+
def check_testcase_success(testcase, class_name, assertions)
15+
assert_equal(class_name, testcase.attribute("classname").value)
16+
assert_equal(assertions.to_s, testcase.attribute("assertions").value)
17+
assert_compare(0, "<", Float(testcase.attribute("time").value))
18+
19+
assert_equal(0, testcase.get_elements("failure").size)
20+
assert_equal(0, testcase.get_elements("error").size)
21+
assert_equal(0, testcase.get_elements("skipped").size)
22+
end
23+
24+
def check_testcase_failure(testcase, class_name, assertions, message = nil)
25+
assert_equal(class_name, testcase.attribute("classname").value)
26+
assert_equal(assertions.to_s, testcase.attribute("assertions").value)
27+
assert_compare(0, "<", Float(testcase.attribute("time").value))
28+
29+
failures = testcase.get_elements("failure")
30+
assert_equal(1, failures.size)
31+
assert_true(failures.first.has_text?)
32+
if message
33+
assert_match(message, failures.first.attribute("message").value)
34+
assert_match(message, failures.first.text)
35+
end
36+
37+
assert_equal(0, testcase.get_elements("error").size)
38+
assert_equal(0, testcase.get_elements("skipped").size)
39+
end
40+
41+
def check_testcase_error(testcase, class_name, assertions, message = nil)
42+
assert_equal(class_name, testcase.attribute("classname").value)
43+
assert_equal(assertions.to_s, testcase.attribute("assertions").value)
44+
assert_compare(0, "<", Float(testcase.attribute("time").value))
45+
46+
errors = testcase.get_elements("error")
47+
assert_equal(1, errors.size)
48+
assert_not_nil(errors.first.attribute("type"))
49+
assert_true(errors.first.has_text?)
50+
if message
51+
assert_match(message, errors.first.attribute("message").value)
52+
assert_match(message, errors.first.text)
53+
end
54+
55+
assert_equal(0, testcase.get_elements("failure").size)
56+
assert_equal(0, testcase.get_elements("skipped").size)
57+
end
58+
59+
def check_testcase_skipped(testcase, class_name, assertions, message = nil)
60+
assert_equal(class_name, testcase.attribute("classname").value)
61+
assert_equal(assertions.to_s, testcase.attribute("assertions").value)
62+
assert_compare(0, "<", Float(testcase.attribute("time").value))
63+
64+
skipped = testcase.get_elements("skipped")
65+
assert_equal(1, skipped.size)
66+
assert_match(message, skipped.first.attribute("message").value) if message
67+
68+
assert_equal(0, testcase.get_elements("failure").size)
69+
assert_equal(0, testcase.get_elements("error").size)
70+
end
71+
end

test/run-test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
$LOAD_PATH.unshift(lib_dir)
1111

1212
require 'test-unit'
13+
require 'test/unit/runner/junitxml'
1314

1415
test_unit_notify_base_dir = File.join(base_dir, "..", "test-unit-notify")
1516
test_unit_notify_base_dir = File.expand_path(test_unit_notify_base_dir)

test/test_xml.rb

Lines changed: 70 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -2,186 +2,90 @@
22
require 'rexml/document'
33
require "test/unit"
44
require "test/unit/ui/junitxml/testrunner"
5+
require_relative "check"
56

6-
module Test
7-
module Unit
8-
module UI
9-
module JUnitXml
10-
class TestXml < Test::Unit::TestCase
7+
class TestXml < Test::Unit::TestCase
8+
include Check
119

12-
def test_1
13-
test_case = Class.new(Test::Unit::TestCase) do
14-
test "success" do
15-
assert_equal(1, 1)
16-
end
17-
18-
def test_failure
19-
assert_equal(1, 2)
20-
end
21-
22-
def test_error
23-
raise "hello"
24-
end
25-
26-
def test_omission
27-
omit
28-
end
29-
30-
def test_pending
31-
pend
32-
end
33-
end
34-
35-
output = StringIO.new
36-
runner = TestRunner.new(test_case.suite, :output => output)
37-
runner.start
38-
39-
output.rewind
40-
doc = REXML::Document.new(output)
41-
42-
testsuites = doc.get_elements("testsuites")
43-
assert_equal(1, testsuites.size)
44-
testsuite_array = testsuites.first.get_elements("testsuite")
45-
assert_equal(1, testsuite_array.size)
46-
testsuite = testsuite_array.first
47-
check_testsuite(testsuite, "", 5, 1, 1, 2)
48-
49-
check_testcase_success(
50-
testsuite.elements["testcase[@name='success']"],
51-
"", 1)
52-
check_testcase_failure(
53-
testsuite.elements["testcase[@name='test_failure()']"],
54-
"", 1)
55-
check_testcase_error(
56-
testsuite.elements["testcase[@name='test_error()']"],
57-
"", 0)
58-
check_testcase_skipped(
59-
testsuite.elements["testcase[@name='test_omission()']"],
60-
"", 0)
61-
check_testcase_skipped(
62-
testsuite.elements["testcase[@name='test_pending()']"],
63-
"", 0)
64-
end
65-
66-
def test_multibyte_text
67-
test_case = Class.new(Test::Unit::TestCase) do
68-
test "成功" do
69-
assert_equal(1, 1)
70-
end
71-
72-
def test_failure
73-
assert_equal(1, 2, "失敗")
74-
end
75-
76-
def test_error
77-
raise "エラー"
78-
end
79-
80-
def test_omission
81-
omit("除外")
82-
end
83-
84-
def test_pending
85-
pend("保留")
86-
end
87-
end
88-
89-
output = StringIO.new
90-
runner = TestRunner.new(test_case.suite, :output => output)
91-
runner.start
92-
93-
output.rewind
94-
doc = REXML::Document.new(output)
95-
96-
testsuites = doc.get_elements("testsuites")
97-
assert_equal(1, testsuites.size)
98-
testsuite_array = testsuites.first.get_elements("testsuite")
99-
assert_equal(1, testsuite_array.size)
100-
testsuite = testsuite_array.first
101-
check_testsuite(testsuite, "", 5, 1, 1, 2)
102-
103-
check_testcase_success(
104-
testsuite.elements["testcase[@name='成功']"],
105-
"", 1)
106-
check_testcase_failure(
107-
testsuite.elements["testcase[@name='test_failure()']"],
108-
"", 1, "失敗")
109-
check_testcase_error(
110-
testsuite.elements["testcase[@name='test_error()']"],
111-
"", 0, "エラー")
112-
check_testcase_skipped(
113-
testsuite.elements["testcase[@name='test_omission()']"],
114-
"", 0, "除外")
115-
check_testcase_skipped(
116-
testsuite.elements["testcase[@name='test_pending()']"],
117-
"", 0, "保留")
118-
end
10+
setup do
11+
test_case = Class.new(Test::Unit::TestCase) do
12+
test "success" do
13+
assert_equal(1, 1)
14+
end
11915

120-
private
16+
def test_failure
17+
assert_equal(1, 1)
18+
assert_equal(1, 2)
19+
end
12120

122-
def check_testsuite(testsuite, name, tests, errors, failures, skipped)
123-
assert_equal(name, testsuite.attribute("name").value)
124-
assert_equal(tests, testsuite.get_elements("testcase").size)
125-
assert_equal(tests.to_s, testsuite.attribute("tests").value)
126-
assert_equal(errors.to_s, testsuite.attribute("errors").value)
127-
assert_equal(failures.to_s, testsuite.attribute("failures").value)
128-
assert_equal(skipped.to_s, testsuite.attribute("skipped").value)
129-
assert_compare(0, "<", Float(testsuite.attribute("time").value))
130-
end
21+
def test_error
22+
assert_equal(1, 1)
23+
assert_equal(1, 1)
24+
assert_equal(1, 1)
25+
raise "hello"
26+
end
13127

132-
def check_testcase_success(testcase, class_name, assertions)
133-
assert_equal(class_name, testcase.attribute("classname").value)
134-
assert_equal(assertions.to_s, testcase.attribute("assertions").value)
135-
assert_compare(0, "<", Float(testcase.attribute("time").value))
28+
def test_omission
29+
omit
30+
end
13631

137-
assert_equal(0, testcase.get_elements("failure").size)
138-
assert_equal(0, testcase.get_elements("error").size)
139-
assert_equal(0, testcase.get_elements("skipped").size)
140-
end
32+
def test_pending
33+
pend
34+
end
35+
end
14136

142-
def check_testcase_failure(testcase, class_name, assertions, message = nil)
143-
assert_equal(class_name, testcase.attribute("classname").value)
144-
assert_equal(assertions.to_s, testcase.attribute("assertions").value)
145-
assert_compare(0, "<", Float(testcase.attribute("time").value))
37+
output = StringIO.new
38+
runner = Test::Unit::UI::JUnitXml::TestRunner.new(
39+
test_case.suite, :output => output)
40+
runner.start
14641

147-
failures = testcase.get_elements("failure")
148-
assert_equal(1, failures.size)
149-
assert_match(message, failures.first.attribute("message").to_s) if message
150-
assert_true(failures.first.has_text?)
42+
output.rewind
43+
@doc = REXML::Document.new(output)
44+
end
15145

152-
assert_equal(0, testcase.get_elements("error").size)
153-
assert_equal(0, testcase.get_elements("skipped").size)
154-
end
46+
test "testsuites" do
47+
testsuites_array = @doc.get_elements("/testsuites")
48+
assert_equal(1, testsuites_array.size)
49+
end
15550

156-
def check_testcase_error(testcase, class_name, assertions, message = nil)
157-
assert_equal(class_name, testcase.attribute("classname").value)
158-
assert_equal(assertions.to_s, testcase.attribute("assertions").value)
159-
assert_compare(0, "<", Float(testcase.attribute("time").value))
51+
test "testsuite" do
52+
testsuite_array = @doc.get_elements("/testsuites/testsuite")
53+
assert_equal(1, testsuite_array.size)
54+
check_testsuite(testsuite_array.first, "", 5, 1, 1, 2)
55+
end
16056

161-
errors = testcase.get_elements("error")
162-
assert_equal(1, errors.size)
163-
assert_match(message, errors.first.attribute("message").to_s) if message
164-
assert_not_nil(errors.first.attribute("type"))
165-
assert_true(errors.first.has_text?)
57+
test "testcase success" do
58+
testcase_array = @doc.get_elements(
59+
"/testsuites/testsuite/testcase[@name='success']")
60+
assert_equal(1, testcase_array.size)
61+
check_testcase_success(testcase_array.first, "", 1)
62+
end
16663

167-
assert_equal(0, testcase.get_elements("failure").size)
168-
assert_equal(0, testcase.get_elements("skipped").size)
169-
end
64+
test "testcase failure" do
65+
testcase_array = @doc.get_elements(
66+
"/testsuites/testsuite/testcase[@name='test_failure()']")
67+
assert_equal(1, testcase_array.size)
68+
check_testcase_failure(testcase_array.first, "", 2)
69+
end
17070

171-
def check_testcase_skipped(testcase, class_name, assertions, message = nil)
172-
assert_equal(class_name, testcase.attribute("classname").value)
173-
assert_equal(assertions.to_s, testcase.attribute("assertions").value)
174-
assert_compare(0, "<", Float(testcase.attribute("time").value))
71+
test "testcase error" do
72+
testcase_array = @doc.get_elements(
73+
"/testsuites/testsuite/testcase[@name='test_error()']")
74+
assert_equal(1, testcase_array.size)
75+
check_testcase_error(testcase_array.first, "", 3, "hello")
76+
end
17577

176-
failures = testcase.get_elements("skipped")
177-
assert_equal(1, failures.size)
178-
assert_match(message, failures.first.attribute("message").to_s) if message
78+
test "testcase omission" do
79+
testcase_array = @doc.get_elements(
80+
"/testsuites/testsuite/testcase[@name='test_omission()']")
81+
assert_equal(1, testcase_array.size)
82+
check_testcase_skipped(testcase_array.first, "", 0)
83+
end
17984

180-
assert_equal(0, testcase.get_elements("failure").size)
181-
assert_equal(0, testcase.get_elements("error").size)
182-
end
183-
end
184-
end
185-
end
85+
test "testcase pending" do
86+
testcase_array = @doc.get_elements(
87+
"/testsuites/testsuite/testcase[@name='test_pending()']")
88+
assert_equal(1, testcase_array.size)
89+
check_testcase_skipped(testcase_array.first, "", 0)
18690
end
18791
end

0 commit comments

Comments
 (0)