import sys
import logging
from optparse import OptionParser
from coverage import coverage

from tests.config import configure

logging.disable(logging.CRITICAL)


def run_tests(options, *test_args):
    from django_nose import NoseTestSuiteRunner
    test_runner = NoseTestSuiteRunner(verbosity=options.verbosity,
                                      pdb=options.pdb,
                                      )
    if not test_args:
        test_args = ['tests']
    num_failures = test_runner.run_tests(test_args)
    if num_failures:
        sys.exit(num_failures)


if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option('-c', '--coverage', dest='use_coverage', default=False,
                      action='store_true', help="Generate coverage report")
    parser.add_option('-v', '--verbosity', dest='verbosity', default=1,
                      type='int', help="Verbosity of output")
    parser.add_option('-d', '--pdb', dest='pdb', default=False,
                      action='store_true', help="Whether to drop into PDB on failure/error")
    (options, args) = parser.parse_args()

    # If no args, then use 'progressive' plugin to keep the screen real estate
    # used down to a minimum.  Otherwise, use the spec plugin
    nose_args = ['-s', '-x',
                 '--with-progressive' if not args else '--with-spec']
    configure(nose_args)

    if options.use_coverage:
        print 'Running tests with coverage'
        c = coverage(source=['oscar'])
        c.start()
        run_tests(options, *args)
        c.stop()
        print 'Generate HTML reports'
        c.html_report()
    else:
        run_tests(options, *args)

import sys
import logging
from optparse import OptionParser

from tests.config import configure

logging.disable(logging.CRITICAL)


def run_tests(*test_args):
    from django_nose import NoseTestSuiteRunner
    test_runner = NoseTestSuiteRunner()
    if not test_args:
        test_args = ['tests']
    num_failures = test_runner.run_tests(test_args)
    if num_failures:
        sys.exit(num_failures)


if __name__ == '__main__':
    parser = OptionParser()
    __, args = parser.parse_args()

    # If no args, then use 'progressive' plugin to keep the screen real estate
    # used down to a minimum.  Otherwise, use the spec plugin
    nose_args = ['-s', '-x',
                 '--with-progressive' if not args else '--with-spec']
    nose_args.extend([
        '--with-coverage', '--cover-package=oscar', '--cover-html',
        '--cover-html-dir=htmlcov'])
    configure(nose_args)
    run_tests(*args)

Rework test runner to generate coverage stats correctly.
Rework test runner to generate coverage stats correctly.

Nose seems to pick up argv automatically - which is annoying.  Will need
to look into at some point.

  import sys
  import logging
  from optparse import OptionParser
- from coverage import coverage
  
  from tests.config import configure
  
  logging.disable(logging.CRITICAL)
  
  
- def run_tests(options, *test_args):
+ def run_tests(*test_args):
      from django_nose import NoseTestSuiteRunner
+     test_runner = NoseTestSuiteRunner()
-     test_runner = NoseTestSuiteRunner(verbosity=options.verbosity,
-                                       pdb=options.pdb,
-                                       )
      if not test_args:
          test_args = ['tests']
      num_failures = test_runner.run_tests(test_args)
      if num_failures:
          sys.exit(num_failures)
  
  
  if __name__ == '__main__':
      parser = OptionParser()
-     parser.add_option('-c', '--coverage', dest='use_coverage', default=False,
-                       action='store_true', help="Generate coverage report")
-     parser.add_option('-v', '--verbosity', dest='verbosity', default=1,
-                       type='int', help="Verbosity of output")
-     parser.add_option('-d', '--pdb', dest='pdb', default=False,
-                       action='store_true', help="Whether to drop into PDB on failure/error")
-     (options, args) = parser.parse_args()
+     __, args = parser.parse_args()
  
      # If no args, then use 'progressive' plugin to keep the screen real estate
      # used down to a minimum.  Otherwise, use the spec plugin
      nose_args = ['-s', '-x',
                   '--with-progressive' if not args else '--with-spec']
+     nose_args.extend([
+         '--with-coverage', '--cover-package=oscar', '--cover-html',
+         '--cover-html-dir=htmlcov'])
      configure(nose_args)
+     run_tests(*args)
  
-     if options.use_coverage:
-         print 'Running tests with coverage'
-         c = coverage(source=['oscar'])
-         c.start()
-         run_tests(options, *args)
-         c.stop()
-         print 'Generate HTML reports'
-         c.html_report()
-     else:
-         run_tests(options, *args)
- 
Rework test runner to generate coverage stats correctly.
## Code Before:
import sys
import logging
from optparse import OptionParser
from coverage import coverage

from tests.config import configure

logging.disable(logging.CRITICAL)


def run_tests(options, *test_args):
    from django_nose import NoseTestSuiteRunner
    test_runner = NoseTestSuiteRunner(verbosity=options.verbosity,
                                      pdb=options.pdb,
                                      )
    if not test_args:
        test_args = ['tests']
    num_failures = test_runner.run_tests(test_args)
    if num_failures:
        sys.exit(num_failures)


if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option('-c', '--coverage', dest='use_coverage', default=False,
                      action='store_true', help="Generate coverage report")
    parser.add_option('-v', '--verbosity', dest='verbosity', default=1,
                      type='int', help="Verbosity of output")
    parser.add_option('-d', '--pdb', dest='pdb', default=False,
                      action='store_true', help="Whether to drop into PDB on failure/error")
    (options, args) = parser.parse_args()

    # If no args, then use 'progressive' plugin to keep the screen real estate
    # used down to a minimum.  Otherwise, use the spec plugin
    nose_args = ['-s', '-x',
                 '--with-progressive' if not args else '--with-spec']
    configure(nose_args)

    if options.use_coverage:
        print 'Running tests with coverage'
        c = coverage(source=['oscar'])
        c.start()
        run_tests(options, *args)
        c.stop()
        print 'Generate HTML reports'
        c.html_report()
    else:
        run_tests(options, *args)

## Instruction:
Rework test runner to generate coverage stats correctly.
## Code After:
import sys
import logging
from optparse import OptionParser

from tests.config import configure

logging.disable(logging.CRITICAL)


def run_tests(*test_args):
    from django_nose import NoseTestSuiteRunner
    test_runner = NoseTestSuiteRunner()
    if not test_args:
        test_args = ['tests']
    num_failures = test_runner.run_tests(test_args)
    if num_failures:
        sys.exit(num_failures)


if __name__ == '__main__':
    parser = OptionParser()
    __, args = parser.parse_args()

    # If no args, then use 'progressive' plugin to keep the screen real estate
    # used down to a minimum.  Otherwise, use the spec plugin
    nose_args = ['-s', '-x',
                 '--with-progressive' if not args else '--with-spec']
    nose_args.extend([
        '--with-coverage', '--cover-package=oscar', '--cover-html',
        '--cover-html-dir=htmlcov'])
    configure(nose_args)
    run_tests(*args)
