
import subprocess
from invoke import task


@task
def test(cover=False):
    # Run tests using nose called with coverage
    code = subprocess.call(['coverage', 'run', '-m', 'nose', '--rednose'])
    # Also generate coverage reports when --cover flag is given
    if cover and code == 0:
        # Add blank line between test report and coverage report
        print('')
        subprocess.call(['coverage', 'report'])
        subprocess.call(['coverage', 'html'])


import subprocess
from invoke import task


@task
def test(cover=False):
    if cover:
        # Run tests via coverage and generate reports if --cover flag is given
        code = subprocess.call(['coverage', 'run', '-m', 'nose', '--rednose'])
        # Only show coverage report if all tests have passed
        if code == 0:
            # Add blank line between test report and coverage report
            print('')
            subprocess.call(['coverage', 'report'])
            subprocess.call(['coverage', 'html'])
    else:
        # Otherwise, run tests via nose (which is faster)
        code = subprocess.call(['nosetests', '--rednose'])

Improve performance of test task
Improve performance of test task

  
  import subprocess
  from invoke import task
  
  
  @task
  def test(cover=False):
-     # Run tests using nose called with coverage
+     if cover:
+         # Run tests via coverage and generate reports if --cover flag is given
-     code = subprocess.call(['coverage', 'run', '-m', 'nose', '--rednose'])
+         code = subprocess.call(['coverage', 'run', '-m', 'nose', '--rednose'])
-     # Also generate coverage reports when --cover flag is given
-     if cover and code == 0:
+         # Only show coverage report if all tests have passed
+         if code == 0:
-         # Add blank line between test report and coverage report
+             # Add blank line between test report and coverage report
-         print('')
+             print('')
-         subprocess.call(['coverage', 'report'])
+             subprocess.call(['coverage', 'report'])
-         subprocess.call(['coverage', 'html'])
+             subprocess.call(['coverage', 'html'])
+     else:
+         # Otherwise, run tests via nose (which is faster)
+         code = subprocess.call(['nosetests', '--rednose'])
  
Improve performance of test task
## Code Before:

import subprocess
from invoke import task


@task
def test(cover=False):
    # Run tests using nose called with coverage
    code = subprocess.call(['coverage', 'run', '-m', 'nose', '--rednose'])
    # Also generate coverage reports when --cover flag is given
    if cover and code == 0:
        # Add blank line between test report and coverage report
        print('')
        subprocess.call(['coverage', 'report'])
        subprocess.call(['coverage', 'html'])

## Instruction:
Improve performance of test task
## Code After:

import subprocess
from invoke import task


@task
def test(cover=False):
    if cover:
        # Run tests via coverage and generate reports if --cover flag is given
        code = subprocess.call(['coverage', 'run', '-m', 'nose', '--rednose'])
        # Only show coverage report if all tests have passed
        if code == 0:
            # Add blank line between test report and coverage report
            print('')
            subprocess.call(['coverage', 'report'])
            subprocess.call(['coverage', 'html'])
    else:
        # Otherwise, run tests via nose (which is faster)
        code = subprocess.call(['nosetests', '--rednose'])
