Skip to content

Instantly share code, notes, and snippets.

@magixx
Created August 14, 2015 21:35
Show Gist options
  • Save magixx/184a380c6cb01c54e085 to your computer and use it in GitHub Desktop.
Save magixx/184a380c6cb01c54e085 to your computer and use it in GitHub Desktop.
Mock generator of tests
import pprint
import random
def weighted_random(weights):
number = random.random() * sum(weights.values())
for k, v in weights.iteritems():
if number < v:
break
number -= v
return k
class TestScenario(object):
def __init__(self, file_num=None):
self.file_num = random.randint(1, 5) if not file_num else file_num
self.files = []
for _ in range(0, self.file_num):
self.files.append(TestFile())
def __str__(self):
output = 'SCENARIO:\n'
for i in self.files:
output += str(i) + '\n'
return output
class TestFile(object):
def __init__(self, class_num=None):
self.class_num = random.randint(1, 4) if not class_num else class_num
self.classes = []
for _ in range(0, self.class_num):
self.classes.append(TestClass())
def __str__(self):
output = 'File:\n'
for i in self.classes:
output += '\t' + str(i) + '\n'
return output.strip()
class TestClass(object):
def __init__(self, test_num=None):
self.test_num = random.randint(1, 20) if not test_num else test_num
self.tests = []
for _ in range(0, self.test_num):
self.tests.append(TestCase())
def __str__(self):
output = 'Class:\n'
for i in self.tests:
output += '\t' + str(i) + '\n'
return output.strip()
class TestCase(object):
def __init__(self):
self.resources = []
self.time = random.uniform(0.5, 5)
for _ in range(0, weighted_random({0: 7.5, 1: 70, 2: 25, 3: 2.5})):
self.resources.append(random.choice(['a', 'a', 'b', 'b', 'c']))
def __str__(self):
return '\tTest: minutes: {}, resources: {}'.format(self.time, self.resources)
class Resource(object):
def __init__(self, type, variation=None):
self.type = type
self.variation = variation
def main():
scenario = TestScenario()
print scenario
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment