Commit f77add48 authored by Bogdan's avatar Bogdan

Cleaned up code in testing.py

parent fb012428
...@@ -13,13 +13,12 @@ Use command line argument '-w' to run on windows. ...@@ -13,13 +13,12 @@ Use command line argument '-w' to run on windows.
''' '''
PY = sys.argv[2] if (len(sys.argv) > 1 and sys.argv[1] == '-py') else 'python3.7' # use -py to use your own python command PY = sys.argv[2] if (len(sys.argv) > 1 and sys.argv[1] == '-py') else 'python3.7' # use -py to use your own python command
COVERAGE = "coverage run --append --omit=*/site-packages*" COVERAGE = "python3.7 -m coverage"
ROOT = pathlib.Path(__file__).parent.parent.absolute() ROOT = pathlib.Path(__file__).parent.parent.absolute()
REPORTS = ROOT / 'reports' REPORTS = ROOT / 'reports'
TESTS_FOLDER_NAME = os.path.normpath("/tests") TESTS_FOLDER_NAME = os.path.normpath("/tests")
print("\nSearching for tests at the path: "+ str(ROOT)) print("\nSearching for tests at the path: "+ str(ROOT) +"\n")
count = 0 count = 0
resultCodeList = [] resultCodeList = []
coverage_paths_set = set() coverage_paths_set = set()
...@@ -32,15 +31,13 @@ for (dirname, dirs, files) in os.walk(ROOT): ...@@ -32,15 +31,13 @@ for (dirname, dirs, files) in os.walk(ROOT):
and not("venv" in str(dirname)) \ and not("venv" in str(dirname)) \
and not("Lib" in str(dirname)): and not("Lib" in str(dirname)):
try: try:
print(f"Executing tests in {dirname}") print(f"Executing tests in {dirname} \n")
os.chdir(os.path.normpath(dirname)) os.chdir(os.path.normpath(dirname))
# TODO do this during docker image setup # TODO do this during docker image setup
exit_val = os.system(f"{PY} -m pip install -r ../requirements.txt") # install pip dependencies exit_val = os.system(f"{PY} -m pip install -r ../requirements.txt") # install pip dependencies
#resultCodeList.append(exit_val) #exit_val = os.system(f"{PY} -m unittest discover") # OLD execute the tests
#exit_val = os.system(f"{PY} -m unittest discover") # execute the tests exit_val = os.system(f"{COVERAGE} --append --omit=*/site-packages*,*/dist-packages* -m unittest discover") #TEST CODE COVERAGE
exit_val = os.system(f"python3.7 -m coverage run --append --omit=*/site-packages*,*/dist-packages* -m unittest discover") #TEST CODE COVERAGE coverage_paths_set.add(os.path.normpath(dirname))
coverage_paths_set.add(os.path.normpath(dirname))
resultCodeList.append(exit_val) #once per folder i.e if 3 tests are in a folder and crash, there will be just one exit val resultCodeList.append(exit_val) #once per folder i.e if 3 tests are in a folder and crash, there will be just one exit val
except Exception as e: except Exception as e:
print(e) print(e)
...@@ -53,31 +50,28 @@ for (dirname, dirs, files) in os.walk(ROOT): ...@@ -53,31 +50,28 @@ for (dirname, dirs, files) in os.walk(ROOT):
for filename in os.listdir(cur_dir): for filename in os.listdir(cur_dir):
if filename_regular_expresion.match(filename): if filename_regular_expresion.match(filename):
#gets here only if there is a test file which matches the regular expression in the app folder, #gets here only if there is a test file which matches the regular expression in the app folder,
#cur_dir = os.path(dirname).parent()
os.chdir(cur_dir) os.chdir(cur_dir)
print(f"Executing coverage test in {cur_dir}") print(f"Executing coverage test in {cur_dir} \n")
exit_val = os.system(f"python3.7 -m coverage run --append --omit=*/site-packages* -m unittest discover") exit_val = os.system(f"{COVERAGE} run --append --omit=*/site-packages* -m unittest discover")
coverage_paths_set.add(os.path.normpath(cur_dir)) coverage_paths_set.add(os.path.normpath(cur_dir))
except Exception as e: except Exception as e:
print(e) print(e)
continue continue
#CHANGE FOLDER TO REPORTS, in order to combine the coverage #CHANGE FOLDER TO REPORTS, in order to combine the coverage
try: try:
if not os.path.exists(REPORTS): if not os.path.exists(REPORTS):
os.makedirs(REPORTS) os.makedirs(REPORTS)
except: except:
pass pass
os.chdir(REPORTS)
target = REPORTS
target = os.path.normpath( str(target) + f'/.coverage' )
try: try:
os.chdir(REPORTS)
target = REPORTS
target = os.path.normpath( str(target) + f'/.coverage' )
except:
pass
try:
os.remove(target) #Try to Remove old coverage file, if exists os.remove(target) #Try to Remove old coverage file, if exists
except Exception as e: except Exception as e:
pass pass
...@@ -91,7 +85,6 @@ for path in coverage_paths_set: ...@@ -91,7 +85,6 @@ for path in coverage_paths_set:
target = REPORTS target = REPORTS
target = os.path.normpath( str(target) + f'/.coverage.{counter}' ) target = os.path.normpath( str(target) + f'/.coverage.{counter}' )
counter += 1 counter += 1
shutil.copyfile(original,target) #copy new generated coverage files shutil.copyfile(original,target) #copy new generated coverage files
os.remove(original) os.remove(original)
except Exception as e: except Exception as e:
...@@ -99,18 +92,15 @@ for path in coverage_paths_set: ...@@ -99,18 +92,15 @@ for path in coverage_paths_set:
continue continue
print("Generating Combined report") print("Generating Combined report")
os.system("python3.7 -m coverage combine") os.system(f"{COVERAGE} combine")
os.system("python3.7 -m coverage xml") os.system(f"{COVERAGE} xml")
os.system("python3.7 -m coverage html") #if you want to generate the html as well os.system(f"{COVERAGE} html")
firstError = -1 firstError = -1
i = 0 i = 0
while i < len(resultCodeList): while i < len(resultCodeList):
if resultCodeList[i] != 0: if resultCodeList[i] != 0:
# print("\nA test failed with code: "+ str(resultCodeList[i])) print("\nA test failed with code: "+ str(resultCodeList[i]))
if (firstError < 0): if (firstError < 0):
firstError = i firstError = i
i += 1 i += 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment