Example #1
0
  def process_response(self, request, response):
    if not settings.DEBUG:
      return response

    if '_prof_heavy' in request.REQUEST:
      self.profiler.create_stats()

      out = StringIO.StringIO()
      old_stdout = sys.stdout 
      sys.stdout = out

      stats = pstats.Stats(self.profiler)
      stats.sort_stats('time', 'calls')

      stats.print_stats()
      sys.stdout = old_stdout
      
      stats_str = out.getvalue()
      
      new_response = http.HttpResponse(stats_str)
      new_response['Content-type'] = 'text/plain'
      return new_response

    # NOTE: this will not work in any environment other than the dev server
    #       as this is not shared-state-safe, only one request is allowed
    #       at a time for this data to be accurate
    if ('_prof_db' in request.REQUEST 
        or request.META.get('HTTP_X_PROFILE', '') == 'db'):
      self.prof_label.stop()
      csv = common_profile.csv()
      common_profile.clear()
      return http.HttpResponse(csv)

    if '_prof_quick' in request.REQUEST:
      self.prof_label.stop()
      html = common_profile.html()
      common_profile.clear()
      response.write(html)
      return response
    
    


    return response
Example #2
0
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], 
              include_coverage=False, include_profile=False, profile_all=False):
    """
    Copy and munge of django's django.test.simple.run_tests method,
    we're extending it to handle coverage
    
    -- Original Docs --

    Run the unit tests for all the test labels in the provided list.
    Labels must be of the form:
     - app.TestClass.test_method
        Run a single specific test method
     - app.TestClass
        Run all the test methods in a given class
     - app
        Search for doctests and unittests in the named application.

    When looking for tests, the test runner will look in the models and
    tests modules for the application.
    
    A list of 'extra' tests may also be provided; these tests
    will be added to the test suite.
    
    Returns the number of tests that failed.
    """
    utils.setup_test_environment()
    
    settings.DEBUG = False
    profile.PROFILE_ALL_TESTS = False
    suite = unittest.TestSuite()
    
    coverage_modules = []
    if include_coverage:
      coverage.start()
    if profile_all:
      profile.PROFILE_ALL_TESTS = True

    if test_labels:
        for label in test_labels:
            if '.' in label:
                suite.addTest(simple.build_test(label))
            else:
                app = models.get_app(label)
                suite.addTest(simple.build_suite(app))
                coverage_modules.append(app)
    else:
        for app in models.get_apps():
            if app.__name__.startswith('appengine_django'):
              continue
            suite.addTest(simple.build_suite(app))
            coverage_modules.append(app)
    
    for test in extra_tests:
        suite.addTest(test)

    old_name = settings.DATABASE_NAME
    from django.db import connection
    connection.creation.create_test_db(verbosity, autoclobber=not interactive)

    result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
    if include_coverage:
      coverage.stop()
      app_names = [mod.__name__.split('.')[0] for mod in coverage_modules]

      coverage_paths = ['%s/*.py' % app 
                        for app in settings.INSTALLED_APPS
                        if _any_startswith(app, app_names)]
                            
      coverage.report(coverage_paths, ignore_errors=1)

    if profile_all or include_profile:
      f = open(settings.PROFILING_DATA_PATH, 'w')
      f.write(profile.csv())
      f.close()
      profile.clear()


    connection.creation.destroy_test_db(old_name, verbosity)
    
    utils.teardown_test_environment()
    
    return len(result.failures) + len(result.errors)