Skip to content

Instantly share code, notes, and snippets.

@yngwie74
Created January 9, 2018 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yngwie74/bee1efafd7018fe2b789b7e76fd2fe36 to your computer and use it in GitHub Desktop.
Save yngwie74/bee1efafd7018fe2b789b7e76fd2fe36 to your computer and use it in GitHub Desktop.
Execute OpenCover with NUnit3 and render output with ReportGenerator
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import sys
import fnmatch
from subprocess import Popen
def norm_join(base, *parts):
return os.path.join(
os.path.normpath(os.path.expanduser(base)),
*map(os.path.normpath, parts)
)
# Definir Comandos
_OPENCOVER_CMD = norm_join('~/OpenCover', 'OpenCover.Console.exe')
_REP_GEN_CMD = norm_join('~/ReportGenerator', 'ReportGenerator.exe')
_NUNIT_CMD = norm_join('~/nunit', 'nunit3-console.exe')
_NUNIT_ARGS = '--inprocess --noresult -where "cat != LongRunning"'
def find_test_assemblies(tst_path):
for (root, dirnames, filenames) in os.walk(tst_path):
for filename in fnmatch.filter(filenames, '*[Tt]est*.[Dd][Ll][Ll]'):
yield os.path.join(root, filename)
def basename_noext(filepath):
return os.path.basename(os.path.splitext(filepath)[0])
def to_exclude_filter(assembly_path):
return '-[{}]*'.format(os.path.basename(os.path.splitext(assembly_path)[0]))
def exec_cmd(command_list):
try:
pipe = Popen(command_list)
pipe.wait()
return pipe.returncode
except Exception, ex:
print(ex, file=sys.stderr)
sys.exit(3)
def main(src_path):
# Define paths
tst_path = norm_join(src_path, 'output')
rpt_path = norm_join(src_path, '_coverage')
if not os.path.isdir(rpt_path):
os.makedirs(rpt_path)
out_file = norm_join(rpt_path, 'result.xml')
# Define default filters
include_filter = '+[*]*'
exclude_filters = ['nunit', 'Moq']
# Discover assemblies to analyze
test_assemblies = list(find_test_assemblies(tst_path))
exclude_filters.extend(test_assemblies)
if not test_assemblies:
print('No se encontraron assemblies a incluir.', file=sys.stderr)
sys.exit(2)
# Execute tests
print('\nEjecutando pruebas y analizando...')
exec_cmd([
_OPENCOVER_CMD,
'-returntargetcode',
'-register:user',
'-target:{}'.format(_NUNIT_CMD),
'-targetargs:{} {}'.format(_NUNIT_ARGS, ' '.join(test_assemblies)),
'-filter:{} {}'.format(include_filter, ' '.join(to_exclude_filter(f) for f in
exclude_filters)),
'-output:{}'.format(out_file),
])
print('\nGenerando reporte...')
exec_cmd([_REP_GEN_CMD, '-reports:{}'.format(out_file), '-targetdir:{}'.format(rpt_path),
'-verbosity:Info'])
# All done!
print('listo.')
if __name__ == '__main__':
if len(sys.argv) < 2:
print('{} <src_path>'.format(sys.argv[0]), file=sys.stderr)
sys.exit(1)
else:
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment