def test_scripts_below_regular(self):
        tests.write_tree_from_desc('''dir: t
file: t/__init__.py
file: t/foo.py
import unittest

class Test(unittest.TestCase):

    def test_me(self):
      self.assertTrue(True)
dir: t/scripts
file: t/scripts/__init__.py
from sst import loader

discover = loader.discoverTestScripts
file: t/scripts/script.py
raise AssertionError('Loading only, executing fails')
''')
        test_loader = loader.TestLoader()
        suite = test_loader.discoverTests('t')
        self.assertEqual(2, suite.countTestCases())
        # Check which kind of tests have been discovered or we may miss regular
        # test cases seen as scripts.
        self.assertEqual(['t.foo.Test.test_me', 't.scripts.script'],
                         [t.id() for t in testtools.iterate_tests(suite)])
    def test_regular_below_scripts(self):
        tests.write_tree_from_desc('''dir: t
file: t/__init__.py
dir: t/regular
file: t/regular/__init__.py
from sst import loader
import unittest

discover = loader.discoverRegularTests

class Test(unittest.TestCase):

    def test_in_init(self):
      self.assertTrue(True)
file: t/regular/foo.py
import unittest

class Test(unittest.TestCase):

    def test_me(self):
      self.assertTrue(True)
file: t/script.py
raise AssertionError('Loading only, executing fails')
''')
        test_loader = loader.TestLoader()
        suite = test_loader.discoverTests(
            't',
            file_loader_class=loader.ScriptLoader,
            dir_loader_class=loader.ScriptDirLoader)
        # Check which kind of tests have been discovered or we may miss regular
        # test cases seen as scripts.
        self.assertEqual([
            't.regular.Test.test_in_init', 't.regular.foo.Test.test_me',
            't.script'
        ], [t.id() for t in testtools.iterate_tests(suite)])
    def test_broken_file_in_a_dir(self):
        tests.write_tree_from_desc('''dir: t
file: t/__init__.py
file: t/foo.py
I'm not even python code
''')
        test_loader = loader.TestLoader()
        e = self.assertRaises(SyntaxError, test_loader.discoverTests, 't')
        self.assertEqual('EOL while scanning string literal', e.args[0])
    def test_load_simple_script_with_csv(self):
        self.create_script('foo.py', "pass")
        with open('foo.csv', 'w') as f:
            f.write('''\
'foo'^'bar'
1^baz
2^qux
''')
        suite = loader.TestLoader().loadTestsFromScript('.', 'foo.py')
        self.assertEqual(2, suite.countTestCases())
    def test_simple_file_in_a_dir(self):
        tests.write_tree_from_desc('''dir: t
file: t/__init__.py
file: t/foo.py
import unittest

class Test(unittest.TestCase):

    def test_me(self):
      self.assertTrue(True)
''')
        test_loader = loader.TestLoader()
        suite = test_loader.discoverTests('t')
        self.assertEqual(1, suite.countTestCases())
    def test_shared(self):
        tests.write_tree_from_desc('''dir: t
# no t/__init__.py required, we don't need to import the scripts
file: t/foo.py
from sst.actions import *

raise AssertionError('Loading only, executing fails')
dir: t/shared
file: t/shared/amodule.py
Don't look at me !
''')
        script_dir_loader = loader.ScriptDirLoader(loader.TestLoader())
        suite = script_dir_loader.discover('t', 'shared')
        self.assertIs(None, suite)
    def test_pattern(self):
        tests.write_tree_from_desc('''dir: t
file: t/__init__.py
file: t/foo_foo.py
import unittest

class Test(unittest.TestCase):

    def test_me(self):
      self.assertTrue(True)
file: t/test_foo.py
Don't look at me !
''')
        test_loader = loader.TestLoader()
        suite = test_loader.discover('t', pattern='foo*.py')
        self.assertEqual(1, suite.countTestCases())
    def test_regular(self):
        tests.write_tree_from_desc('''dir: t
# no t/__init__.py required, we don't need to import the scripts
dir: t/subdir
file: t/subdir/foo.py
raise AssertionError('Loading only, executing fails')
dir: t/shared
file: t/shared/amodule.py
Don't look at me !
''')
        test_loader = loader.TestLoader()
        suite = test_loader.discoverTests(
            '.',
            file_loader_class=loader.ScriptLoader,
            dir_loader_class=loader.ScriptDirLoader)
        self.assertEqual(1, suite.countTestCases())
    def test_regular_and_scripts_mixed(self):
        def regular(dir_name, name, suffix=None):
            if suffix is None:
                suffix = ''
            return '''
file: {dir_name}/{name}{suffix}
from sst import cases

class Test_{name}(cases.SSTTestCase):
    def test_{name}(self):
        pass
'''.format(**locals())

        tests.write_tree_from_desc('''dir: tests
file: tests/__init__.py
from sst import loader

discover = loader.discoverRegularTests
''')
        tests.write_tree_from_desc(regular('tests', 'test_real', '.py'))
        tests.write_tree_from_desc(regular('tests', 'test_real1', '.py'))
        tests.write_tree_from_desc(regular('tests', 'test_real2', '.py'))
        # Leading '_' => ignored
        tests.write_tree_from_desc(regular('tests', '_hidden', '.py'))
        # Not a python file => ignored
        tests.write_tree_from_desc(regular('tests', 'not-python'))
        # Some empty files
        tests.write_tree_from_desc('''
file: script1.py
file: script2.py
file: not_a_test
# '.p' is intentional, not a typoed '.py'
file: test_not_a_test.p
_hidden_too.py
''')
        test_loader = loader.TestLoader()
        suite = test_loader.discoverTests(
            '.',
            file_loader_class=loader.ScriptLoader,
            dir_loader_class=loader.ScriptDirLoader)
        self.assertEqual([
            'script1', 'script2',
            'tests.test_real.Test_test_real.test_test_real',
            'tests.test_real1.Test_test_real1.test_test_real1',
            'tests.test_real2.Test_test_real2.test_test_real2'
        ], [t.id() for t in testtools.iterate_tests(suite)])
 def test_simple_file_in_a_dir_no_sys_path(self):
     self._create_foo_in_tests()
     test_loader = loader.TestLoader()
     e = self.assertRaises(ImportError, test_loader.discover, 't', '*.py')
     self.assertEqual(e.message, 'No module named t')
 def test_simple_file_in_a_dir(self):
     self._create_foo_in_tests()
     test_loader = loader.TestLoader()
     suite = test_loader.discover('t', '*.py', self.test_base_dir)
     self.assertEqual(1, suite.countTestCases())
 def get_test_loader(self):
     test_loader = loader.TestLoader()
     return test_loader
 def test_load_non_existing_script(self):
     suite = loader.TestLoader().loadTestsFromScript('.', 'foo.py')
     self.assertEqual(0, suite.countTestCases())
 def test_load_simple_script(self):
     # A simple do nothing script with no imports
     self.create_script('foo.py', 'pass')
     suite = loader.TestLoader().loadTestsFromScript('.', 'foo.py')
     self.assertEqual(1, suite.countTestCases())
 def get_test_loader(self):
     return loader.TestLoader()
def runtests(test_regexps,
             test_dir='.',
             collect_only=False,
             browser_factory=None,
             report_format='console',
             shared_directory=None,
             screenshots_on=False,
             failfast=False,
             debug=False,
             extended=False,
             includes=None,
             excludes=None):

    config.results_directory = os.path.abspath('results')
    actions._make_results_dir()

    if test_dir == 'selftests':
        # XXXX horrible hardcoding
        # selftests should be a command instead
        package_dir = os.path.dirname(__file__)
        os.chdir(os.path.dirname(package_dir))
        test_dir = os.path.join('.', 'sst', 'selftests')
    else:
        if not os.path.isdir(test_dir):
            msg = 'Specified directory %r does not exist' % test_dir
            print msg
            sys.exit(1)
    shared_directory = find_shared_directory(test_dir, shared_directory)
    config.shared_directory = shared_directory
    sys.path.append(shared_directory)

    if browser_factory is None:
        # TODO: We could raise an error instead as providing a default value
        # makes little sense here -- vila 2013-04-11
        browser_factory = browsers.FirefoxFactory()

    test_loader = loader.TestLoader(browser_factory, screenshots_on, debug,
                                    extended)
    alltests = test_loader.suiteClass()
    alltests.addTests(
        test_loader.discoverTests(test_dir,
                                  file_loader_class=loader.ScriptLoader,
                                  dir_loader_class=loader.ScriptDirLoader))

    alltests = filters.filter_by_regexps(test_regexps, alltests)
    alltests = filters.exclude_regexps(excludes, alltests)

    print ''
    print '  %s test cases loaded\n' % alltests.countTestCases()
    print '--------------------------------------------------------------'

    if not alltests.countTestCases():
        print 'Error: Did not find any tests'
        sys.exit(1)

    if collect_only:
        print 'Collect-Only Enabled, Not Running Tests...\n'
        print 'Tests Collected:'
        print '-' * 16
        for t in testtools.testsuite.iterate_tests(alltests):
            print t.id()
        return

    text_result = result.TextTestResult(sys.stdout,
                                        failfast=failfast,
                                        verbosity=2)
    if report_format == 'xml':
        results_file = os.path.join(config.results_directory, 'results.xml')
        xml_stream = file(results_file, 'wb')
        res = testtools.testresult.MultiTestResult(
            text_result,
            junitxml.JUnitXmlResult(xml_stream),
        )
        res.failfast = failfast
    else:
        res = text_result

    res.startTestRun()
    try:
        alltests.run(res)
    except KeyboardInterrupt:
        print >> sys.stderr, 'Test run interrupted'
    finally:
        # XXX should warn on cases that were specified but not found
        pass
    res.stopTestRun()

    return len(res.failures) + len(res.errors)
 def get_test_loader(self):
     test_loader = loader.TestLoader()
     # We don't use the default PackageLoader for unit testing DirLoader
     # behavior. But we still leave ModuleLoader for the file loader.
     test_loader.dirLoaderClass = loader.DirLoader
     return test_loader