Advertisement
marksweb

PhantomTestCase

Sep 12th, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. __author__ = 'markw'
  2.  
  3. from multiprocessing import Process
  4. import os.path
  5. from subprocess import Popen, PIPE
  6. import sys
  7.  
  8. from django.test import LiveServerTestCase
  9.  
  10. import buildconstants
  11. from django_offline.startuphelpers import django_server_helper
  12.  
  13. __all__ = ['PhantomTestCase']
  14.  
  15.  
  16. class PhantomTestCase(LiveServerTestCase):
  17.     """LiveServerTestCase subclass that can invoke PhantomJS tests."""
  18.  
  19.     use_phantom_disk_cache = False
  20.  
  21.     def __init__(self, *args, **kwargs):
  22.         self.interactive = False
  23.         super(PhantomTestCase, self).__init__(*args, **kwargs)
  24.  
  25.     @classmethod
  26.     def setUpClass(cls):
  27.  
  28.         cls.server = Process(
  29.             target = django_server_helper,
  30.             args = (None, cls.fixtures),
  31.         )
  32.         cls.server.start()
  33.         super(PhantomTestCase, cls).setUpClass()
  34.  
  35.     def phantom(self, test_filename, **kwargs):
  36.         """PhantomJS test invoker.
  37.  
  38.        Takes a test filename (.js) and optional arguments to pass to the
  39.        phantom test.
  40.  
  41.        Returns True if the test(s) passed, and False if any test failed.
  42.        """
  43.         self.tests = kwargs.get('screenID', 'undefined')
  44.         self.screens = kwargs.get('screens', 'enabled')
  45.         self.port = kwargs.get('port', '46729')
  46.         self.taxyear = kwargs.get('taxyear', 2014)
  47.  
  48.         cmd = [buildconstants.DEPS_PHANTOMJS]
  49.         cmd.append(test_filename)
  50.         cmd.append('--tests={}'.format(self.tests))
  51.         cmd.append('--screens={}'.format(self.screens))
  52.         cmd.append('--port={}'.format(self.port))
  53.         cmd.append('--taxyear={}'.format(self.taxyear))
  54.  
  55.         p = Popen(cmd, stdout=PIPE, stderr=PIPE,
  56.             cwd=os.path.dirname(test_filename))  # flake8: noqa
  57.         out, err = p.communicate()
  58.         if p.returncode != 0:
  59.             sys.stdout.write(out)
  60.             sys.stderr.write(err)
  61.         return p.returncode == 0
  62.  
  63.     @classmethod
  64.     def tearDownClass(cls):
  65.         cls.server.terminate()
  66.         super(PhantomTestCase, cls).tearDownClass()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement