forked from ChristopherRogers1991/mycroft_routine_skill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skill_developers_testrunner.py
96 lines (75 loc) · 3.02 KB
/
skill_developers_testrunner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright 2017 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Find test files starting from the directory where this file resides.
The skill_developers_testrunner.py is intended to be copied to the
skill developers own directory, where the skills __init__.py file
resides. Running the python unit test in this module from that file
location, will test the skill developers test the same way as
discover_test.py, but running only the skill developers test.
It is executed as a unit test.
"""
import glob
import unittest
import os
from test.integrationtests.skills.skill_tester import MockSkillsLoader
from test.integrationtests.skills.skill_tester import SkillTest
HOME_DIR = os.path.dirname(os.path.abspath(__file__))
def discover_tests():
"""Find skills whith test files
For all skills with test files, starten from current directory,
find the test files in subdirectory test/intent.
:return: skills and corresponding test case files found
"""
tests = {}
skills = [HOME_DIR]
for skill in skills:
test_intent_files = [
f for f
in glob.glob(os.path.join(skill, 'test/intent/*.intent.json'))
]
if len(test_intent_files) > 0:
tests[skill] = test_intent_files
return tests
class IntentTestSequenceMeta(type):
def __new__(mcs, name, bases, d):
def gen_test(a, b):
def test(self):
if not SkillTest(a, b, self.emitter).run(self.loader):
assert False
return test
tests = discover_tests()
for skill in tests.keys():
skill_name = os.path.basename(skill) # Path of the skill
for example in tests[skill]:
# Name of the intent
example_name = os.path.basename(
os.path.splitext(os.path.splitext(example)[0])[0])
test_name = "test_IntentValidation[%s:%s]" % (skill_name,
example_name)
d[test_name] = gen_test(skill, example)
return type.__new__(mcs, name, bases, d)
class IntentTestSequence(unittest.TestCase, metaclass=IntentTestSequenceMeta):
"""This is the TestCase class that pythons unit tester can execute.
"""
loader = None
@classmethod
def setUpClass(cls):
cls.loader = MockSkillsLoader(HOME_DIR)
cls.emitter = cls.loader.load_skills()
@classmethod
def tearDownClass(cls):
cls.loader.unload_skills()
if __name__ == '__main__':
unittest.main()