-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dev -> Master | Added LTF (Lucifer Testing Framework) | 0.8.6r1
Added LTF, which is the Lucifer Testing Framework, it allows for easy written test cases and adds a -T argument to lucifer which will run all the lucifer tests.
- Loading branch information
Showing
26 changed files
with
453 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class LFTError(Exception): | ||
def __init__(self, message): | ||
self.message = message | ||
self.__class__.__module__ = "LTF" | ||
|
||
def raiseError(self): | ||
raise self | ||
|
||
def __str__(self): | ||
return f"{self.message}" | ||
|
||
def __repr__(self): | ||
return f"<{self.__class__.__name__}, message='{self.message}'>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .LTFError import LFTError | ||
|
||
|
||
class NotLTFTestError(LFTError): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .LTFError import LFTError | ||
from .NotLTFTestError import NotLTFTestError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from .Utils import map_failed_test | ||
|
||
|
||
class Basic: | ||
def __init__(self, statistics): | ||
self.statistics = statistics | ||
|
||
def generate_display(self): | ||
data = "" | ||
for testSet in self.statistics: | ||
data += "====Test: " + testSet.__class__.__name__ + "====\n\n" | ||
for test in self.statistics[testSet]: | ||
testData = self.statistics[testSet][test] | ||
data += "----" + test + "----\n" | ||
data += "Succeeded: " + map_failed_test(testData["failed"]) + "\n" | ||
data += "Time Taken: " + str(testData["time"]) + "\n\n" | ||
return data | ||
|
||
def show(self): | ||
print(self.generate_display()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .Basic import Basic | ||
from .Utils import generate_percent_breakdown | ||
|
||
|
||
class PercentageBasic(Basic): | ||
def generate_display(self): | ||
data = super().generate_display() | ||
data += generate_percent_breakdown(self.statistics) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .Short import Short | ||
from .Utils import generate_percent_breakdown | ||
|
||
|
||
class PercentageShort(Short): | ||
def generate_display(self): | ||
data = super().generate_display() | ||
data += generate_percent_breakdown(self.statistics) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from .Utils import map_failed_test | ||
|
||
|
||
class Short: | ||
def __init__(self, statistics): | ||
self.statistics = statistics | ||
|
||
def generate_display(self): | ||
data = "" | ||
for testSet in self.statistics: | ||
data += "====Test: " + testSet.__class__.__name__ + "====\n" | ||
for test in self.statistics[testSet]: | ||
testData = self.statistics[testSet][test] | ||
data += test + ": " + map_failed_test(testData["failed"]) + "\n" | ||
data += "\n" | ||
return data | ||
|
||
def show(self): | ||
print(self.generate_display()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def generate_percent_breakdown(statistics): | ||
data = "" | ||
failedTests = 0 | ||
totalTests = 0 | ||
failedTestSets = 0 | ||
totalTestSets = 0 | ||
for testSet in statistics: | ||
setFailed = False | ||
for test in statistics[testSet]: | ||
testData = statistics[testSet][test] | ||
if testData["failed"]: | ||
failedTests += 1 | ||
setFailed = True | ||
totalTests += 1 | ||
if setFailed: | ||
failedTestSets += 1 | ||
totalTestSets += 1 | ||
data += "#### Final Breakdown ####\n" | ||
data += "Failed Tests: " + str(failedTests) + "/" + str(totalTests) + "\n" | ||
data += "Failed Test Sets: " + str(failedTestSets) + "/" + str(totalTestSets) + "\n" | ||
data += "Test Succeeded: " + str(round(((totalTests - failedTests) / totalTests) * 100, 2)) + "%\n" | ||
data += "Test Sets Succeeded: " + str(round( | ||
((totalTestSets - failedTestSets) / totalTestSets) * 100, 2) | ||
) + "%\n" | ||
return data | ||
|
||
|
||
def map_failed_test(failed_value): | ||
return { | ||
True: "no", | ||
False: "yes", | ||
None: "skipped" | ||
}.get(failed_value, "unknown") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .Basic import Basic | ||
from .PercentageBasic import PercentageBasic | ||
from .PercentageShort import PercentageShort | ||
from .Short import Short |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from .Requirement import Requirement | ||
|
||
|
||
class RequireLuciferManager(Requirement): | ||
def satisfyRequirement(self): | ||
from LMI import LMI | ||
self.instance.luciferManager = LMI.luciferManager | ||
|
||
def check_satisfied(self): | ||
if hasattr(self.instance, "luciferManger"): | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Requirement: | ||
def __init__(self, instance): | ||
self.instance = instance | ||
self.isSatisfied = False | ||
|
||
def check_satisfied(self): | ||
return self.isSatisfied | ||
|
||
def satisfyRequirement(self): | ||
self.isSatisfied = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .RequireLuciferManager import RequireLuciferManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from .Errors import NotLTFTestError | ||
from .Tests import AssertTest, LTFTest | ||
|
||
|
||
class TestsRunner: | ||
def __init__(self): | ||
self.tests = {} | ||
self.statistics = {} | ||
|
||
def runAll(self): | ||
for test in self.tests.keys(): | ||
if isinstance(test, LTFTest): | ||
test.run() | ||
self.statistics[test] = test.test_mappings | ||
else: | ||
raise NotLTFTestError("Not a LTF Test compatible Test!") | ||
|
||
def add_function_assert_test(self, testFunction): | ||
self.tests[AssertTest(testFunction)] = { | ||
"hasRun": False, | ||
"Failed": False, | ||
"Time": None, | ||
"Error": None | ||
} | ||
|
||
def add_LTF_test(self, LTFClass): | ||
if isinstance(LTFClass, type): | ||
LTFClass = LTFClass() | ||
self.tests[LTFClass] = { | ||
"hasRun": False, | ||
"Failed": False, | ||
"Time": None, | ||
"Error": None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from .LTFTest import LTFTest | ||
|
||
|
||
class AssertTest(LTFTest): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(**kwargs) | ||
for test_func in args: | ||
self.extra_tests.append(test_func) | ||
|
||
def run(self): | ||
self.findFunctions() | ||
self.satisfyRequirements() | ||
for functionName, function in zip(self.all_functions.keys(), self.all_functions.values()): | ||
self.setDefaultTestValues(functionName) | ||
try: | ||
timeTaken = self.timeFunction(function) | ||
self.test_mappings[functionName]["time"] = timeTaken | ||
except Exception as e: | ||
self.addError(functionName, e) | ||
self.test_mappings[functionName]["has_run"] = True | ||
self.has_run = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from .LTFTest import LTFTest | ||
|
||
|
||
class BooleanTest(LTFTest): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(**kwargs) | ||
for test_func in args: | ||
self.extra_tests.append(test_func) | ||
|
||
def run(self): | ||
self.findFunctions() | ||
self.satisfyRequirements() | ||
for functionName, function in zip(self.all_functions.keys(), self.all_functions.values()): | ||
self.setDefaultTestValues(functionName) | ||
try: | ||
timeTaken, outValue = self.timeWithReturnFunction(function) | ||
self.test_mappings[functionName]["time"] = timeTaken | ||
if outValue is None: | ||
self.test_mappings[functionName]["failed"] = None | ||
elif not outValue: | ||
self.addError(functionName, Exception("Failed boolean test")) | ||
except Exception as e: | ||
self.addError(functionName, e) | ||
self.test_mappings[functionName]["has_run"] = True | ||
self.has_run = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import abc | ||
import timeit | ||
|
||
from ...Utils import RunTimeReturn | ||
|
||
|
||
class LTFTest(metaclass=abc.ABCMeta): | ||
testIdentifiers = ["test_"] | ||
|
||
def __init__(self, requirements=None): | ||
self.requirements = [] | ||
self.all_functions = {} | ||
self.extra_tests = [] | ||
self.test_mappings = {} | ||
self.has_run = False | ||
if requirements is not None: | ||
for requirement in requirements: | ||
self.requirements.append(requirement(self)) | ||
|
||
def findFunctions(self): | ||
self.all_functions = {} | ||
for function in self.extra_tests: | ||
self.all_functions["extra-" + function.__name__] = function | ||
for functionName in self.__class__.__dict__.keys(): | ||
for identifier in LTFTest.testIdentifiers: | ||
if functionName.startswith(identifier): | ||
self.all_functions[functionName] = (self.__class__.__dict__.get(functionName)) | ||
break | ||
|
||
def satisfyRequirements(self): | ||
for requirement in self.requirements: | ||
if not requirement.check_satisfied(): | ||
requirement.satisfyRequirement() | ||
|
||
def timeWithReturnFunction(self, function): | ||
with RunTimeReturn() as RTR: | ||
function = function.__get__(self) | ||
timeTaken, outValue = RTR.run(function, number=1) | ||
return timeTaken, outValue | ||
|
||
def timeFunction(self, function): | ||
function = function.__get__(self) | ||
timeTaken = timeit.timeit(function, number=1) | ||
return timeTaken | ||
|
||
def addError(self, functionName, error, failed=True): | ||
self.test_mappings[functionName]["errors"].append(error) | ||
self.test_mappings[functionName]["failed"] = failed | ||
|
||
def setDefaultTestValues(self, functionName): | ||
self.test_mappings[functionName] = { | ||
"time": None, | ||
"has_run": False, | ||
"errors": [], | ||
"failed": False | ||
} | ||
|
||
@abc.abstractmethod | ||
def run(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .AssertTest import AssertTest | ||
from .BooleanTest import BooleanTest | ||
from .LTFTest import LTFTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
""" LTF - Lucifer Test Framework | ||
This is a testing framework built into lucifer which allows for modules to have an easy to use test API within lucifer, | ||
lucifer tests also use this same framework! | ||
""" | ||
from . import Formatters | ||
from . import Requirements | ||
from . import Tests | ||
from .Runner import TestsRunner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.