
import coverage
import sys
import unittest
import os

def main():
    #Cleanup old html report:
    for root, dirs, files in os.walk('test/output_coverage_html/'):
        for f in files:
            if f == '.gitignore' or f == '.empty_dir':
                continue
            os.unlink(os.path.join(root, f))
        for d in dirs:
            shutil.rmtree(os.path.join(root, d))

    #Perform coverage analisys:
    cov = coverage.coverage()

    cov.start()
    #Discover the tests and execute them:
    loader = unittest.TestLoader()
    tests = loader.discover('./test/')
    testRunner = unittest.runner.TextTestRunner(descriptions=True, verbosity=1)
    testRunner.run(tests)
    cov.stop()

    cov.html_report()

if __name__ == '__main__':
    main()


try:
    import coverage
except ImportError:
    pass
import sys
import unittest
import os

def main():
    #Cleanup old html report:
    for root, dirs, files in os.walk('test/output_coverage_html/'):
        for f in files:
            if f == '.gitignore' or f == '.empty_dir':
                continue
            os.unlink(os.path.join(root, f))
        for d in dirs:
            shutil.rmtree(os.path.join(root, d))

    #Perform coverage analisys:
    if "coverage" in sys.modules:
        cov = coverage.coverage()
        cov.start()

    #Discover the tests and execute them:
    loader = unittest.TestLoader()
    tests = loader.discover('./test/')
    testRunner = unittest.runner.TextTestRunner(descriptions=True, verbosity=1)
    testRunner.run(tests)

    if "coverage" in sys.modules:
        cov.stop()
        cov.html_report()

if __name__ == '__main__':
    main()

Make coverage module optional during test run
Make coverage module optional during test run

Change-Id: I79f767a90a84c7b482e0cc9acd311619611802e9

  
+ try:
- import coverage
+     import coverage
+ except ImportError:
+     pass
  import sys
  import unittest
  import os
  
  def main():
      #Cleanup old html report:
      for root, dirs, files in os.walk('test/output_coverage_html/'):
          for f in files:
              if f == '.gitignore' or f == '.empty_dir':
                  continue
              os.unlink(os.path.join(root, f))
          for d in dirs:
              shutil.rmtree(os.path.join(root, d))
  
      #Perform coverage analisys:
+     if "coverage" in sys.modules:
-     cov = coverage.coverage()
+         cov = coverage.coverage()
+         cov.start()
  
-     cov.start()
      #Discover the tests and execute them:
      loader = unittest.TestLoader()
      tests = loader.discover('./test/')
      testRunner = unittest.runner.TextTestRunner(descriptions=True, verbosity=1)
      testRunner.run(tests)
-     cov.stop()
  
+     if "coverage" in sys.modules:
+         cov.stop()
-     cov.html_report()
+         cov.html_report()
  
  if __name__ == '__main__':
      main()
  
Make coverage module optional during test run
## Code Before:

import coverage
import sys
import unittest
import os

def main():
    #Cleanup old html report:
    for root, dirs, files in os.walk('test/output_coverage_html/'):
        for f in files:
            if f == '.gitignore' or f == '.empty_dir':
                continue
            os.unlink(os.path.join(root, f))
        for d in dirs:
            shutil.rmtree(os.path.join(root, d))

    #Perform coverage analisys:
    cov = coverage.coverage()

    cov.start()
    #Discover the tests and execute them:
    loader = unittest.TestLoader()
    tests = loader.discover('./test/')
    testRunner = unittest.runner.TextTestRunner(descriptions=True, verbosity=1)
    testRunner.run(tests)
    cov.stop()

    cov.html_report()

if __name__ == '__main__':
    main()

## Instruction:
Make coverage module optional during test run
## Code After:

try:
    import coverage
except ImportError:
    pass
import sys
import unittest
import os

def main():
    #Cleanup old html report:
    for root, dirs, files in os.walk('test/output_coverage_html/'):
        for f in files:
            if f == '.gitignore' or f == '.empty_dir':
                continue
            os.unlink(os.path.join(root, f))
        for d in dirs:
            shutil.rmtree(os.path.join(root, d))

    #Perform coverage analisys:
    if "coverage" in sys.modules:
        cov = coverage.coverage()
        cov.start()

    #Discover the tests and execute them:
    loader = unittest.TestLoader()
    tests = loader.discover('./test/')
    testRunner = unittest.runner.TextTestRunner(descriptions=True, verbosity=1)
    testRunner.run(tests)

    if "coverage" in sys.modules:
        cov.stop()
        cov.html_report()

if __name__ == '__main__':
    main()
