Skip to content

Commit

Permalink
Add TestCaseParser module to DocGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
palaciosjeremias committed Jul 28, 2021
1 parent 8a2eaf9 commit fc20b17
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 8 additions & 1 deletion docs/DocGenerator/CodeParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import json
import yaml
from Config import Config
from TestCaseParser import TestCaseParser
from docstring_parser import parse
from comment_parser import comment_parser
import warnings

class CodeParser:
def __init__(self):
self.conf = Config()
self.test_case_parser = TestCaseParser()
self.function_regexes = []
for regex in self.conf.function_regex:
self.function_regexes.append(re.compile(regex))
Expand Down Expand Up @@ -58,18 +60,23 @@ def parse_test(self, code_file, id, group_id):
module_doc['Id'] = id
module_doc['Group Id'] = group_id

test_cases = self.test_case_parser.collect(code_file)

functions_doc = []
for function in functions:
if self.is_documentable_function(function):
function_doc = self.parse_comment(function)
if function_doc:
if test_cases[function.name]:
function_doc["Test Cases"] = test_cases[function.name]
functions_doc.append(function_doc)

if not functions_doc:
warnings.warn("Module doesn´t contain any test function")

module_doc['Tests'] = functions_doc

self.remove_ignored_fields(module_doc)
#self.remove_ignored_fields(module_doc)

return module_doc

Expand Down
33 changes: 33 additions & 0 deletions docs/DocGenerator/TestCaseParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest

class PytestPlugin:
def __init__(self):
self.collected = []

def pytest_collection_modifyitems(self, items):
for item in items:
self.collected.append(item.nodeid)

class TestCaseParser:
def __init__(self):
self.plugin = PytestPlugin()

def collect(self, path):
pytest.main(['--collect-only', path], plugins=[self.plugin])
output = {}
for item in self.plugin.collected:
tmp = item.split("::")
file = tmp[0]
tmp = tmp[1].split("[")
test = tmp[0]
if not test in output:
output[test] = []
if len(tmp) >= 2:
tmp = tmp[1].split("]")
test_case = tmp[0]
output[test].append(test_case)
return output


test = TestCaseParser()
test.collect("../../tests/integration/test_wazuh_db/test_wazuh_db.py")

0 comments on commit fc20b17

Please sign in to comment.