Changed unittest execution from script to Makefile

This commit is contained in:
Dirk Alders 2021-02-28 18:24:12 +01:00
parent bb6b46d7bc
commit a70a6c432b
4 changed files with 151 additions and 124 deletions

135
run.py
View File

@ -35,7 +35,7 @@ UNDERLINE = '\033[4m'
ARG_CLEAN = 'clean'
ARG_RUN = 'run'
ARG_FINALISE = 'finalise'
ARG_PDF = 'pdf'
ARG_TEX = 'tex'
ARG_STATUS = 'status'
ARG_COPY = 'copy'
ARG_RELEASE = 'release'
@ -92,7 +92,7 @@ class coverage_info(list):
f[self.KEY_LINE_COVERAGE] = float(c.attributes['line-rate'].value) * 100.
try:
f[self.KEY_BRANCH_COVERAGE] = float(p.attributes['branch-rate'].value) * 100.
except:
except Exception:
f[self.KEY_BRANCH_COVERAGE] = None
f[self.KEY_FRAGMENTS] = []
last_hit = None
@ -193,23 +193,46 @@ def module_uid(path):
def unittest(options, args, unittest_folder):
if ARG_CLEAN in args:
unittest_init(unittest_folder)
elif ARG_RUN in args:
unittest_run(unittest_folder, options)
elif ARG_FINALISE in args:
if 'release_testcases' in args:
unittest_release_testcases(unittest_folder)
elif 'clean' in args:
unittest_clean(unittest_folder)
elif 'prepare' in args:
unittest_prepare(unittest_folder)
elif 'testrun' in args:
unittest_testrun(unittest_folder, options)
elif 'finalise' in args:
unittest_finalise(unittest_folder)
elif ARG_PDF in args:
unittest_pdf(unittest_folder)
elif ARG_STATUS in args:
elif 'status' in args:
unittest_status(unittest_folder)
elif ARG_COPY in args:
unittest_copy(unittest_folder)
elif ARG_RELEASE in args:
unittest_release(unittest_folder)
elif 'publish' in args:
unittest_publish(unittest_folder)
def unittest_init(unittest_folder):
def unittest_release_testcases(unittest_folder):
unittest_uid = module_uid(os.path.join(unittest_folder, 'src', 'tests'))
config_file = os.path.join(unittest_folder, 'src', 'config.py')
print_header('Releasing unittest')
with open(config_file, 'r') as fh:
conf_file = fh.read()
print_action('Setting release_unittest_version = %s in %s' % (unittest_uid, config_file))
with open(config_file, 'w') as fh:
for line in conf_file.splitlines():
if line.startswith('release_unittest_version'):
fh.write("release_unittest_version = '%s'\n" % unittest_uid)
else:
fh.write(line + '\n')
def unittest_clean(unittest_folder):
print_header('Cleaning up...')
print_action('Testresults from last testrun')
for fn in os.listdir(unittest_filename(unittest_folder, '')):
remove_file(unittest_filename(unittest_folder, fn))
remove_file(unittest_filename(unittest_folder, FILES['coverage-xml']))
def unittest_prepare(unittest_folder):
config = imp.load_source('', os.path.join(unittest_folder, 'src', 'config.py'))
#
print_header("Initiating unittest for first testrun...")
@ -217,11 +240,6 @@ def unittest_init(unittest_folder):
print_action('Creating outpout folder %s' % unittest_filename(unittest_folder, ''))
fstools.mkdir(unittest_filename(unittest_folder, ''))
#
print_action('Cleaning up data from last testrun')
for fn in os.listdir(unittest_filename(unittest_folder, '')):
remove_file(unittest_filename(unittest_folder, fn))
remove_file(unittest_filename(unittest_folder, FILES['coverage-xml']))
#
print_action('Creating unittest data-collection: %s' % unittest_filename(unittest_folder, FILES['data-collection']))
#
system_info = {}
@ -268,7 +286,7 @@ def unittest_init(unittest_folder):
fh.write(json.dumps(data_collection, indent=4, sort_keys=True))
def unittest_run(unittest_folder, options):
def unittest_testrun(unittest_folder, options):
tests = imp.load_source('', os.path.join(unittest_folder, 'src', 'tests', '__init__.py'))
config = imp.load_source('', os.path.join(unittest_folder, 'src', 'config.py'))
#
@ -305,12 +323,12 @@ def unittest_run(unittest_folder, options):
def unittest_finalise(unittest_folder):
config = imp.load_source('', os.path.join(unittest_folder, 'src', 'config.py'))
#
print_action('Adding Testrun data to %s' % unittest_filename(unittest_folder, FILES['data-collection']))
print_header("Adding Requirement information")
#
print_action('Loading Testrun data from %s' % unittest_filename(unittest_folder, FILES['data-collection']))
with open(unittest_filename(unittest_folder, FILES['data-collection']), 'r') as fh:
data_collection = json.loads(fh.read())
#
print_header("Adding Requirement information")
#
data_collection['lost_souls'] = {}
#
print_action("Adding Lost Requirement Soul")
@ -340,16 +358,14 @@ def unittest_finalise(unittest_folder):
data_collection[UNITTEST_KEY_COVERAGE_INFO] = coverage_info(unittest_filename(unittest_folder, 'coverage.xml'), os.path.dirname(config.lib_path))
with open(unittest_filename(unittest_folder, FILES['data-collection']), 'w') as fh:
fh.write(json.dumps(data_collection, indent=4, sort_keys=True))
def unittest_pdf(unittest_folder):
print_header("Creating PDF-Report of Unittest")
#
print_header("Creating LaTeX-Report of Unittest")
print_action('Loading Testrun data from %s' % unittest_filename(unittest_folder, FILES['data-collection']))
with open(unittest_filename(unittest_folder, FILES['data-collection']), 'r') as fh:
data_collection = json.loads(fh.read())
if jinja2 is None:
print_action('You need to install jinja2 to create a PDF-Report!', FAIL)
print_action('You need to install jinja2 to create a LaTeX-Report!', FAIL)
else:
fn = unittest_filename(unittest_folder, FILES['tex-report'])
print_action('Creating LaTeX-File %s' % fn)
@ -360,16 +376,25 @@ def unittest_pdf(unittest_folder):
jenv = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path))
template = jenv.get_template(template_filename)
fh.write(template.render(data=data_collection))
print_action('Creating PDF %s' % unittest_filename(unittest_folder, 'unittest.pdf'))
for i in range(3):
sys.stdout.write(' Starting run %d/3 of pdflatex... ' % (i + 1))
sys.stdout.flush()
exit_value = os.system("pdflatex -interaction nonstopmode --output-directory %(path)s %(path)s/unittest.tex 1> /dev/null" % {'path': unittest_filename(unittest_folder, '')})
if exit_value != 0:
print(FAIL + 'FAILED' + ENDC)
break
def unittest_publish(unittest_folder):
config = imp.load_source('', os.path.join(unittest_folder, 'src', 'config.py'))
#
print_header('Copy unittest files to library')
target_folder = os.path.join(config.lib_path, '_testresults_')
print_action('Copying Unittest Files to %s' % target_folder)
if not os.path.exists(target_folder):
print_info('Creating folder %s' % target_folder)
fstools.mkdir(target_folder)
else:
print(OKGREEN + 'SUCCESS' + ENDC)
for fn in os.listdir(target_folder):
remove_file(os.path.join(target_folder, fn))
for fn in REPORT_FILES:
src = unittest_filename(unittest_folder, fn)
dst = os.path.join(target_folder, fn)
print_info('copying %s -> %s' % (src, dst))
shutil.copyfile(src, dst)
def unittest_status(unittest_folder):
@ -438,37 +463,3 @@ def unittest_status(unittest_folder):
print_info("FAILED", FAIL)
else:
print_info("SUCCESS", OKGREEN)
def unittest_copy(unittest_folder):
config = imp.load_source('', os.path.join(unittest_folder, 'src', 'config.py'))
#
print_header('Copy unittest files to library')
target_folder = os.path.join(config.lib_path, '_testresults_')
print_action('Copying Unittest Files to %s' % target_folder)
if not os.path.exists(target_folder):
print_info('Creating folder %s' % target_folder)
fstools.mkdir(target_folder)
else:
for fn in os.listdir(target_folder):
remove_file(os.path.join(target_folder, fn))
for fn in REPORT_FILES:
src = unittest_filename(unittest_folder, fn)
dst = os.path.join(target_folder, fn)
print_info('copying %s -> %s' % (src, dst))
shutil.copyfile(src, dst)
def unittest_release(unittest_folder):
unittest_uid = module_uid(os.path.join(unittest_folder, 'src', 'tests'))
config_file = os.path.join(unittest_folder, 'src', 'config.py')
print_header('Releasing unittest')
with open(config_file, 'r') as fh:
conf_file = fh.read()
print_action('Setting release_unittest_version = %s in %s' % (unittest_uid, config_file))
with open(config_file, 'w') as fh:
for line in conf_file.splitlines():
if line.startswith('release_unittest_version'):
fh.write("release_unittest_version = '%s'\n" % unittest_uid)
else:
fh.write(line + '\n')

81
scripts/Makefile Normal file
View File

@ -0,0 +1,81 @@
COV2_CMD=python2-coverage
COV3_CMD=python3-coverage
PYT2_CMD=python2
PYT3_CMD=python3
PDF_CMD=xdg-open
OUTDIR=testresults
TEXFILE=$(OUTDIR)/unittest.tex
PDFFILE=$(OUTDIR)/unittest.pdf
.PHONY: all help release full short smoke single testrun_full testrun_short testrun_smoke testrun_single coverage_analysis finalise status compile release_testcases publish view clean
all: full
help:
@echo "make [target]"
@echo " Here is a list of the most helpfull targets:"
@echo " - release: Release the testcases, creates a pdf ducument including all testcases and copy testdata to the module"
@echo " ----------------------------------------------------------------------------------------------------------------"
@echo " - full: Create a pdf ducument including all testcases"
@echo " - short: Create a pdf ducument including most testcases"
@echo " - smoke: Create a pdf ducument including some testcases"
@echo " - single: Create a pdf ducument including one testcases"
@echo " ----------------------------------------------------------------------------------------------------------------"
@echo " - testrun_full: Run all testcases"
@echo " - testrun_short: Run most testcases"
@echo " - testrun_smoke: Run some testcases"
@echo " - testrun_single: Run one testcases"
release: release_testcases full publish
full: clean prepare testrun_full coverage_analysis finalise compile status
short: clean prepare testrun_short coverage_analysis finalise compile status
smoke: clean prepare testrun_smoke coverage_analysis finalise compile status
single: clean prepare testrun_single coverage_analysis finalise compile status
clean:
@$(PYT3_CMD) src/unittest/scripts/unittest.py clean
@echo "\e[1m * Collected coverage information\e[0m"
@$(COV3_CMD) erase
release_testcases:
@$(PYT3_CMD) src/unittest/scripts/unittest.py release_testcases
prepare:
@$(PYT3_CMD) src/unittest/scripts/unittest.py prepare
testrun_full:
@$(COV2_CMD) run -a --branch --source=`$(PYT3_CMD) src/config.py -p` src/unittest/scripts/unittest.py testrun -e full
@$(COV3_CMD) run -a --branch --source=`$(PYT3_CMD) src/config.py -p` src/unittest/scripts/unittest.py testrun -e full
testrun_short:
@$(COV2_CMD) run -a --branch --source=`$(PYT3_CMD) src/config.py -p` src/unittest/scripts/unittest.py testrun -e short
@$(COV3_CMD) run -a --branch --source=`$(PYT3_CMD) src/config.py -p` src/unittest/scripts/unittest.py testrun -e short
testrun_smoke:
@$(COV2_CMD) run -a --branch --source=`$(PYT3_CMD) src/config.py -p` src/unittest/scripts/unittest.py testrun -e smoke
@$(COV3_CMD) run -a --branch --source=`$(PYT3_CMD) src/config.py -p` src/unittest/scripts/unittest.py testrun -e smoke
testrun_single:
@$(COV2_CMD) run -a --branch --source=`$(PYT3_CMD) src/config.py -p` src/unittest/scripts/unittest.py testrun -e single
@$(COV3_CMD) run -a --branch --source=`$(PYT3_CMD) src/config.py -p` src/unittest/scripts/unittest.py testrun -e single
coverage_analysis:
@echo "\e[1m\e[93mCreating Coverage-XML-File: $(pwd)/testresults/coverage.xml\e[0m"
@$(COV3_CMD) xml -o testresults/coverage.xml
finalise:
@$(PYT3_CMD) src/unittest/scripts/unittest.py finalise
compile:
@latexmk -pdf -quiet -pdflatex="pdflatex -interaction=nonstopmode" -output-directory=$(OUTDIR) -use-make $(TEXFILE) > /dev/null
status:
@$(PYT3_CMD) src/unittest/scripts/unittest.py status
publish:
@$(PYT3_CMD) src/unittest/scripts/unittest.py publish
view:
@$(PDF_CMD) $(PDFFILE)

View File

@ -1,11 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
sys.path.insert(0, 'src')
import optparse
import os
import sys
BASEPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
sys.path.insert(0, os.path.join(BASEPATH, 'src'))
import report
import tests
import unittest
@ -17,4 +19,4 @@ parser.add_option("-e", "--execution-level", dest="execution_level", default="fu
if report.TCEL_REVERSE_NAMED.get(tests.execution_level, report.TCEL_FULL) < report.TCEL_REVERSE_NAMED.get(options.execution_level, report.TCEL_FULL):
options.execution_level = tests.execution_level
unittest.run.unittest(options, args, os.path.abspath(os.path.join(os.path.dirname(__file__))))
unittest.run.unittest(options, args, BASEPATH)

View File

@ -1,47 +0,0 @@
#!/bin/bash
#
# Set commands depending on distribution
#
. /etc/os-release
# python2
if [[ "$ID" == "arch" || "$ID" == "manjaro" || "$ID_LIKE" == *"opensuse"* || "$ID" == "solus" ]]; then
COV2_CMD="coverage2"
PYT2_CMD="python2"
else
COV2_CMD="python2-coverage"
PYT2_CMD="python2"
fi
# python3
if [[ "$ID" == "arch" || "$ID" == "manjaro" || "$ID_LIKE" == *"opensuse"* || "$ID" == "solus" ]]; then
COV3_CMD="coverage3"
PYT3_CMD="python3"
else
COV3_CMD="python3-coverage"
PYT3_CMD="python3"
fi
# pdf viewer
PDF_CMD="xdg-open"
if [[ $# -eq 0 || ($# -eq 2 && $1 == '-e') ]]; then
#
# Unittest Flow
#
$PYT3_CMD unittest.py clean
echo -e "\e[1m * Erasing collected coverage information\e[0m"
$COV2_CMD erase
$COV2_CMD run -a --branch --source=$($PYT3_CMD src/config.py -p) unittest.py run $*
$COV3_CMD run -a --branch --source=$($PYT3_CMD src/config.py -p) unittest.py run $*
echo -e "\e[1m\e[93mCreating Coverage-XML-File: $(pwd)/testresults/coverage.xml\e[0m"
$COV3_CMD xml -o testresults/coverage.xml
$PYT3_CMD unittest.py finalise
$PYT3_CMD unittest.py status
$PYT3_CMD unittest.py pdf
$PDF_CMD testresults/unittest.pdf
else
$PYT3_CMD unittest.py $*
fi
exit 0