[pitivi] tests: Allow the tests to be run by frameworks like nosetests



commit 2b02155c300d520df10ecaef311549cb32522b3e
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Tue Jan 28 22:19:55 2014 +0100

    tests: Allow the tests to be run by frameworks like nosetests

 tests/__init__.py     |    4 ++
 tests/runtests.py     |  111 ++++++++++++++++++++++++++++++-------------------
 tests/test_project.py |    2 +-
 3 files changed, 73 insertions(+), 44 deletions(-)
---
diff --git a/tests/__init__.py b/tests/__init__.py
index e69de29..7500891 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -0,0 +1,4 @@
+#!/usr/bin/env python2
+
+import runtests
+runtests.setup()
diff --git a/tests/runtests.py b/tests/runtests.py
index bef0815..e65f0b6 100644
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -7,46 +7,71 @@ import sys
 import unittest
 
 
-parent = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
-sys.path.append(os.path.join(parent, "pitivi/coptimizations/.libs"))
-
-from pitivi.check import initialize_modules
-initialize_modules()
-
-
-def gettestnames(file_names):
-    test_names = [file_name[:-3] for file_name in file_names]
-    return test_names
-
-loader = unittest.TestLoader()
-
-# Set verbosity.
-descriptions = 1
-verbosity = 1
-if 'VERBOSE' in os.environ:
-    descriptions = 2
-    verbosity = 2
-from pitivi.utils import loggable as log
-log.init('PITIVI_DEBUG', 1)
-
-# Make available to configure.py the top level dir.
-dir = os.path.dirname(os.path.abspath(__file__))
-top_srcdir = os.path.split(dir)[0]
-os.environ.setdefault('PITIVI_TOP_LEVEL_DIR', top_srcdir)
-
-# Pick which tests to run.
-TEST_CASE = os.getenv("TESTCASE")
-if TEST_CASE:
-    test_names = [TEST_CASE]
-else:
-    test_names = gettestnames(sys.argv[1:])
-suite = loader.loadTestsFromNames(test_names)
-if not list(suite):
-    raise Exception("No tests found")
-
-# Run the tests.
-testRunner = unittest.TextTestRunner(descriptions=descriptions,
-    verbosity=verbosity)
-result = testRunner.run(suite)
-if result.failures or result.errors:
-    sys.exit(1)
+def _testcases(filenames):
+    """Yield testcases out of filenames."""
+    for filename in filenames:
+        if filename.endswith(".py"):
+            yield filename[:-3]
+
+
+def _tests_suite():
+    """Pick which tests to run."""
+    testcase = os.getenv("TESTCASE")
+    if testcase:
+        testcases = [testcase]
+    else:
+        testcases = _testcases(sys.argv[1:])
+    loader = unittest.TestLoader()
+    return loader.loadTestsFromNames(testcases)
+
+
+def get_pitivi_dir():
+    from pitivi.configure import _in_devel
+    if _in_devel():
+        # We know exactly where the top dir.
+        tests_dir = os.path.dirname(os.path.abspath(__file__))
+        pitivi_dir = os.path.join(tests_dir, os.path.pardir)
+    else:
+        # Probably running make distcheck. The path to the test files
+        # is different than the build path, so we must use the current
+        # dir which is build_path/tests.
+        pitivi_dir = os.path.join(os.path.abspath(os.path.curdir), os.path.pardir)
+    return os.path.abspath(pitivi_dir)
+
+
+def setup():
+    # Make available to configure.py the top level dir.
+    pitivi_dir = get_pitivi_dir()
+    os.environ.setdefault('PITIVI_TOP_LEVEL_DIR', pitivi_dir)
+
+    # Make available the compiled C code.
+    libs_dir = os.path.join(pitivi_dir, "pitivi/coptimizations/.libs")
+    sys.path.append(libs_dir)
+
+    # Make sure the modules are initialized correctly.
+    from pitivi.check import initialize_modules
+    initialize_modules()
+
+
+if __name__ == "__main__":
+    setup()
+
+    # Set verbosity.
+    descriptions = 1
+    verbosity = 1
+    if 'VERBOSE' in os.environ:
+        descriptions = 2
+        verbosity = 2
+    from pitivi.utils import loggable as log
+    log.init('PITIVI_DEBUG', 1)
+
+    suite = _tests_suite()
+    if not list(suite):
+        raise Exception("No tests found")
+
+    # Run the tests.
+    testRunner = unittest.TextTestRunner(descriptions=descriptions,
+        verbosity=verbosity)
+    result = testRunner.run(suite)
+    if result.failures or result.errors:
+        sys.exit(1)
diff --git a/tests/test_project.py b/tests/test_project.py
index 7b1488e..1f3f43c 100644
--- a/tests/test_project.py
+++ b/tests/test_project.py
@@ -93,7 +93,7 @@ class TestProjectLoading(TestCase):
         # Create a blank project and save it.
         project = Project("noname")
         result = [False, False, False]
-        uris = ["file://%s/samples/tears of steel.webm" % os.path.abspath(".")]
+        uris = ["file://%s/samples/tears of steel.webm" % os.path.dirname(os.path.abspath(__file__))]
         project.connect("loaded", loaded, self.mainloop, result, uris)
         project.connect("done-importing", added, self.mainloop, result, uris)
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]