1# -*- coding: utf-8 -*-
2import codecs
3import os
4import sys
5import unittest
6from nose.plugins.capture import Capture
7from nose.plugins.xunit import Xunit
8from nose.plugins.skip import Skip
9from nose.plugins import PluginTester
10
11support = os.path.join(os.path.dirname(__file__), 'support')
12xml_results_filename = os.path.join(support, "xunit.xml")
13
14# the plugin is tested better in unit tests.
15# this is just here for a sanity check
16
17class TestXUnitPlugin(PluginTester, unittest.TestCase):
18    activate = '--with-xunit'
19    args = ['-v','--xunit-file=%s' % xml_results_filename]
20    plugins = [Xunit(), Skip()]
21    suitepath = os.path.join(support, 'xunit')
22
23    def runTest(self):
24        print str(self.output)
25
26        assert "ERROR: test_error" in self.output
27        assert "FAIL: test_fail" in self.output
28        assert "test_skip (test_xunit_as_suite.TestForXunit) ... SKIP: skipit" in self.output
29        assert "XML: %s" % xml_results_filename in self.output
30
31        f = codecs.open(xml_results_filename,'r', encoding='utf8')
32        result = f.read()
33        f.close()
34        print result.encode('utf8', 'replace')
35
36        assert '<?xml version="1.0" encoding="UTF-8"?>' in result
37        assert '<testsuite name="nosetests" tests="6" errors="2" failures="1" skip="1">' in result
38        assert '<testcase classname="test_xunit_as_suite.TestForXunit" name="test_error" time="' in result
39        # TODO(Kumar) think of better x-platform code here that
40        # does not confuse 2to3
41        if sys.version_info[0:2] >= (3,0):
42            assert ('<error type="%s.Exception" message="日本">' % (Exception.__module__,)) in result
43        else:
44            assert ('<error type="%s.Exception" message="日本">' % (Exception.__module__,)).decode('utf8') in result
45        assert '</testcase>' in result
46        assert '</testsuite>' in result
47
48
49class TestIssue134(PluginTester, unittest.TestCase):
50    activate = '--with-xunit'
51    args = ['-v','--xunit-file=%s' % xml_results_filename]
52    plugins = [Capture(), Xunit()]
53    suitepath = os.path.join(support, 'issue134')
54
55    def runTest(self):
56        print str(self.output)
57        f = open(xml_results_filename,'r')
58        result = f.read()
59        f.close()
60        print result
61        assert 'raise IOError(42, "test")' in result
62        assert 'tests="1" errors="1" failures="0" skip="0"' in result
63
64
65class TestIssue279(PluginTester, unittest.TestCase):
66    activate = '--with-xunit'
67    args = ['-v','--xunit-file=%s' % xml_results_filename]
68    plugins = [Xunit(), Skip()]
69    suitepath = os.path.join(support, 'issue279')
70
71    def runTest(self):
72        print str(self.output)
73        f = open(xml_results_filename,'r')
74        result = f.read()
75        f.close()
76        print result
77        assert 'tests="1" errors="1" failures="0" skip="0"' in result
78        assert "Exception: I would prefer not to" in result
79
80
81class TestIssue680(PluginTester, unittest.TestCase):
82    activate = '--with-xunit'
83    args = ['-v','--xunit-file=%s' % xml_results_filename]
84    plugins = [Xunit(), Skip()]
85    suitepath = os.path.join(support, 'issue680')
86
87    def runTest(self):
88        print str(self.output)
89        f = open(xml_results_filename,'rb')
90        result = f.read().decode('utf-8')
91        f.close()
92        print result
93        assert 'tests="1" errors="0" failures="0" skip="0"' in result
94
95
96class TestIssue700(PluginTester, unittest.TestCase):
97    activate = '--with-xunit'
98    args = ['-v','--xunit-file=%s' % xml_results_filename]
99    plugins = [Xunit(), Skip()]
100    suitepath = os.path.join(support, 'issue700')
101
102    def runTest(self):
103        print str(self.output)
104        f = open(xml_results_filename,'r')
105        result = f.read()
106        f.close()
107        print result
108        assert 'tests="1" errors="0" failures="0" skip="0"' in result
109        assert 'line1\n' in result
110        assert 'line2\n' in result
111
112
113class TestIssue859(PluginTester, unittest.TestCase):
114    activate = '--with-xunit'
115    testsuite_name = "TestIssue859"
116    args = ['-v', '--xunit-file=%s' % xml_results_filename, '--xunit-testsuite-name=%s' % testsuite_name]
117    plugins = [Xunit(), Skip()]
118    suitepath = os.path.join(support, 'issue859')
119
120    def runTest(self):
121        print str(self.output)
122        f = open(xml_results_filename, 'r')
123        result = f.read()
124        f.close()
125        print result
126        assert 'tests="1" errors="0" failures="0" skip="0"' in result
127        assert 'testsuite name="TestIssue859"' in result
128