Skip to content

Commit e5df97f

Browse files
committed
Feature 12: Fixed the unit tests, fixed a bug with the number of errors reported, refactored and added testes
1 parent e400d31 commit e5df97f

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

python 2/runner/runner_tests/test_mountain.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ def setUp(self):
1414
self.mountain = Mountain()
1515
self.mountain.stream.writeln = Mock()
1616

17-
def test_it_retrieves_some_koans_tests(self):
18-
self.mountain.walk_the_path()
19-
self.assertTrue(self.mountain.tests, "No test suite")
20-
2117
def test_it_gets_test_results(self):
2218
self.mountain.lesson.learn = Mock()
2319
self.mountain.walk_the_path()
2420
self.assertTrue(self.mountain.lesson.learn.called)
25-
21+

python 2/runner/runner_tests/test_sensei.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from runner.sensei import Sensei
1111
from runner.writeln_decorator import WritelnDecorator
1212
from runner.mockable_test_result import MockableTestResult
13+
from runner import path_to_enlightenment
1314

1415
class AboutParrots:
1516
pass
@@ -85,6 +86,9 @@ class TestSensei(unittest.TestCase):
8586
def setUp(self):
8687
self.sensei = Sensei(WritelnDecorator(sys.stdout))
8788
self.sensei.stream.writeln = Mock()
89+
path_to_enlightenment.koans = Mock()
90+
self.tests = Mock()
91+
self.tests.countTestCases = Mock()
8892

8993
def test_that_it_delegates_testing_to_test_cases(self):
9094
MockableTestResult.startTest = Mock()
@@ -228,6 +232,15 @@ def test_that_end_report_displays_something(self):
228232
self.sensei.learn()
229233
self.assertTrue(self.sensei.stream.writeln.called)
230234

235+
def test_that_end_report_shows_student_progress(self):
236+
self.sensei.errorReport = Mock()
237+
self.sensei.total_lessons = Mock()
238+
self.sensei.total_koans = Mock()
239+
240+
self.sensei.learn()
241+
self.assertTrue(self.sensei.total_lessons.called)
242+
self.assertTrue(self.sensei.total_koans.called)
243+
231244
def test_that_end_report_shows_the_failure_report(self):
232245
self.sensei.errorReport = Mock()
233246
self.sensei.learn()
@@ -370,3 +383,26 @@ def test_that_if_there_is_37_successes_it_will_say_the_first_zen_of_python_koans
370383

371384
m = re.search("Beautiful is better than ugly", words)
372385
self.assertTrue(m and m.group(0))
386+
387+
def test_that_total_lessons_return_7_if_there_are_7_lessons(self):
388+
self.sensei.filter_all_lessons = Mock()
389+
self.sensei.filter_all_lessons.return_value = [1,2,3,4,5,6,7]
390+
391+
self.assertEqual(7, self.sensei.total_lessons())
392+
393+
def test_that_total_lessons_return_0_if_all_lessons_is_none(self):
394+
self.sensei.filter_all_lessons = Mock()
395+
self.sensei.filter_all_lessons.return_value = None
396+
397+
self.assertEqual(0, self.sensei.total_lessons())
398+
399+
def test_total_koans_return_43_if_there_are_43_test_cases(self):
400+
self.sensei.tests.countTestCases = Mock()
401+
self.sensei.tests.countTestCases.return_value = 43
402+
403+
self.assertEqual(43, self.sensei.total_koans())
404+
405+
def test_filter_all_lessons_will_discover_test_classes_if_none_have_been_discovered_yet(self):
406+
self.sensei.all_lessons = 0
407+
self.assertTrue(self.sensei.filter_all_lessons() > 10)
408+
self.assertTrue(self.sensei.all_lessons > 10)

python 2/runner/sensei.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
import helper
88
from mockable_test_result import MockableTestResult
9+
from runner import path_to_enlightenment
910

1011
from libs.colorama import init, Fore, Style
1112
import glob
12-
import path_to_enlightenment
1313

1414
init()
1515

@@ -19,13 +19,10 @@ def __init__(self, stream):
1919
unittest.TestResult.__init__(self)
2020
self.stream = stream
2121
self.prevTestClassName = None
22+
self.tests = path_to_enlightenment.koans()
2223
self.pass_count = 0
2324
self.lesson_pass_count = 0
24-
self.tests = path_to_enlightenment.koans()
25-
self.all_lessons = glob.glob('koans/about*.py')
26-
self.all_lessons.remove('koans/about_extra_credit.py')
27-
self.total_lessons = len(self.all_lessons)
28-
self.total_koans = self.tests.countTestCases()
25+
self.all_lessons = None
2926

3027
def startTest(self, test):
3128
MockableTestResult.startTest(self, test)
@@ -161,13 +158,11 @@ def scrapeInterestingStackDump(self, err):
161158
return scrape.replace(sep, '\n').strip('\n')
162159

163160
def report_progress(self):
164-
lesson_progress = self.total_lessons - self.lesson_pass_count
165-
koans_progress = self.total_koans - self.pass_count
166161
return ("You are now {0}/{1} lessons and {2}/{3} koans away from " \
167-
"reaching enlightenment".format(lesson_progress,
168-
self.total_lessons,
169-
koans_progress,
170-
self.total_koans))
162+
"reaching enlightenment".format(self.lesson_pass_count,
163+
self.total_lessons(),
164+
self.pass_count,
165+
self.total_koans()))
171166

172167
# Hat's tip to Tim Peters for the zen statements from The Zen
173168
# of Python (http://www.python.org/dev/peps/pep-0020/)
@@ -233,3 +228,20 @@ def say_something_zenlike(self):
233228

234229
# Hopefully this will never ever happen!
235230
return "The temple in collapsing! Run!!!"
231+
232+
def total_lessons(self):
233+
all_lessons = self.filter_all_lessons()
234+
if all_lessons:
235+
return len(all_lessons)
236+
else:
237+
return 0
238+
239+
def total_koans(self):
240+
return self.tests.countTestCases()
241+
242+
def filter_all_lessons(self):
243+
if not self.all_lessons:
244+
self.all_lessons = glob.glob('koans/about*.py')
245+
self.all_lessons.remove('koans/about_extra_credit.py')
246+
247+
return self.all_lessons

0 commit comments

Comments
 (0)