a7f8fca675fbf5c3734e5100a1413943a0bd52af
[rusty/samba.git] / lib / subunit / python / subunit / tests / test_subunit_stats.py
1 #
2 #  subunit: extensions to python unittest to get test results from subprocesses.
3 #  Copyright (C) 2005  Robert Collins <robertc@robertcollins.net>
4 #
5 #  Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
6 #  license at the users choice. A copy of both licenses are available in the
7 #  project source as Apache-2.0 and BSD. You may not use this file except in
8 #  compliance with one of these two licences.
9 #  
10 #  Unless required by applicable law or agreed to in writing, software
11 #  distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
12 #  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13 #  license you chose for the specific language governing permissions and
14 #  limitations under that license.
15 #
16
17 """Tests for subunit.TestResultStats."""
18
19 import unittest
20 from StringIO import StringIO
21
22 import subunit
23
24
25 class TestTestResultStats(unittest.TestCase):
26     """Test for TestResultStats, a TestResult object that generates stats."""
27
28     def setUp(self):
29         self.output = StringIO()
30         self.result = subunit.TestResultStats(self.output)
31         self.input_stream = StringIO()
32         self.test = subunit.ProtocolTestCase(self.input_stream)
33
34     def test_stats_empty(self):
35         self.test.run(self.result)
36         self.assertEqual(0, self.result.total_tests)
37         self.assertEqual(0, self.result.passed_tests)
38         self.assertEqual(0, self.result.failed_tests)
39         self.assertEqual(set(), self.result.seen_tags)
40
41     def setUpUsedStream(self):
42         self.input_stream.write("""tags: global
43 test passed
44 success passed
45 test failed
46 tags: local
47 failure failed
48 test error
49 error error
50 test skipped
51 skip skipped
52 test todo
53 xfail todo
54 """)
55         self.input_stream.seek(0)
56         self.test.run(self.result)
57     
58     def test_stats_smoke_everything(self):
59         # Statistics are calculated usefully.
60         self.setUpUsedStream()
61         self.assertEqual(5, self.result.total_tests)
62         self.assertEqual(2, self.result.passed_tests)
63         self.assertEqual(2, self.result.failed_tests)
64         self.assertEqual(1, self.result.skipped_tests)
65         self.assertEqual(set(["global", "local"]), self.result.seen_tags)
66
67     def test_stat_formatting(self):
68         expected = ("""
69 Total tests:       5
70 Passed tests:      2
71 Failed tests:      2
72 Skipped tests:     1
73 Seen tags: global, local
74 """)[1:]
75         self.setUpUsedStream()
76         self.result.formatStats()
77         self.assertEqual(expected, self.output.getvalue())
78
79
80 def test_suite():
81     loader = subunit.tests.TestUtil.TestLoader()
82     result = loader.loadTestsFromName(__name__)
83     return result