Предизвикателства > Валидатор за Никулденска рецепта > Решения > Решението на Марина Господинова

Резултати
1 точки от тестове
0 точки от учител

1 точки общо

2 успешни теста
2 неуспешни теста
Код
Скрий всички коментари

 1import unittest
 2
 3from secret import validate_recipe, RuinedNikuldenDinnerError
 4from unittest.mock import mock_open, patch
 5
 6class TestNikuldenValidator(unittest.TestCase):
 7    def test_valid_recipe(self):
 8        nikulden_mock = "Рибата (шаран, сьонга или друга) върви с чаша ракия за добър апетит!"
 9        with patch("builtins.open", mock_open(read_data=nikulden_mock)):
10            self.assertTrue(validate_recipe("nevermind.txt"))
11
12    def test_invalid_recipe(self):
13        nikulden_mock = "За съжаление рецептата не е валидна, ако само пием ракия."
14        with patch("builtins.open", mock_open(read_data=nikulden_mock)):
15            self.assertFalse(validate_recipe("nevermind.txt"))
16
17    def test_bad_recipe_file(self):
18        nikulden_errors = [IOError, OSError]
19        for error in nikulden_errors:
20            with patch("builtins.open", side_effect=error):
21                with self.assertRaises(RuinedNikuldenDinnerError):
22                    validate_recipe("nevermind.txt")
23
24
25if __name__ == "__main__":
26    unittest.main()

F.F.
======================================================================
FAIL: test_naive_in_validator (test.TestTestNikuldenValidator.test_naive_in_validator)
Test with implementation missing word splits.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 92, in test_naive_in_validator
self.assertFalse(result.wasSuccessful(),
AssertionError: True is not false : Expecting test_invalid_recipe to fail with an implementation missing word splits.

======================================================================
FAIL: test_no_lower_validator (test.TestTestNikuldenValidator.test_no_lower_validator)
Test with implementation missing lower.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 62, in test_no_lower_validator
self.assertFalse(result.wasSuccessful(),
AssertionError: True is not false : Expecting test_valid_recipe to fail with an implementation missing lower.

----------------------------------------------------------------------
Ran 4 tests in 0.014s

FAILED (failures=2)

Дискусия
История

f1import unittestf1import unittest
22
3from secret import validate_recipe, RuinedNikuldenDinnerError3from secret import validate_recipe, RuinedNikuldenDinnerError
4from unittest.mock import mock_open, patch4from unittest.mock import mock_open, patch
55
6class TestNikuldenValidator(unittest.TestCase):6class TestNikuldenValidator(unittest.TestCase):
7    def test_valid_recipe(self):7    def test_valid_recipe(self):
8        nikulden_mock = "Рибата (шаран, сьонга или друга) върви с чаша ракия за добър апетит!"8        nikulden_mock = "Рибата (шаран, сьонга или друга) върви с чаша ракия за добър апетит!"
9        with patch("builtins.open", mock_open(read_data=nikulden_mock)):9        with patch("builtins.open", mock_open(read_data=nikulden_mock)):
10            self.assertTrue(validate_recipe("nevermind.txt"))10            self.assertTrue(validate_recipe("nevermind.txt"))
1111
12    def test_invalid_recipe(self):12    def test_invalid_recipe(self):
13        nikulden_mock = "За съжаление рецептата не е валидна, ако само пием ракия."13        nikulden_mock = "За съжаление рецептата не е валидна, ако само пием ракия."
14        with patch("builtins.open", mock_open(read_data=nikulden_mock)):14        with patch("builtins.open", mock_open(read_data=nikulden_mock)):
15            self.assertFalse(validate_recipe("nevermind.txt"))15            self.assertFalse(validate_recipe("nevermind.txt"))
1616
17    def test_bad_recipe_file(self):17    def test_bad_recipe_file(self):
t18        nikulden_error = IOErrort18        nikulden_errors = [IOError, OSError]
19        for error in nikulden_errors:
19        with patch("builtins.open", side_effect=nikulden_error):20            with patch("builtins.open", side_effect=error):
20            with self.assertRaises(RuinedNikuldenDinnerError):21                with self.assertRaises(RuinedNikuldenDinnerError):
21                validate_recipe("nevermind.txt")22                    validate_recipe("nevermind.txt")
2223
2324
24if __name__ == "__main__":25if __name__ == "__main__":
25    unittest.main()26    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op