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

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

1 точки общо

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

 1import unittest
 2from secret import validate_recipe, RuinedNikuldenDinnerError
 3from unittest.mock import patch, mock_open
 4
 5class TestNikuldenValidator(unittest.TestCase):
 6    def test_valid_recipe(self):
 7        valid_file = "доста доста риба лала Рибена глава съм аз" # fake file content
 8        with patch("builtins.open", mock_open(read_data = valid_file)): # immitates open built in func and gives my fake file content
 9            self.assertTrue(validate_recipe("someFile.txt"))
10
11    def test_invalid_recipe(self):
12        invalid_file = "рецепта с рЙба няма нищо гладни сме малко соона"
13        with patch("builtins.open", mock_open(read_data = invalid_file)):
14            self.assertFalse(validate_recipe("somefile.txt"))
15
16    def test_bad_recipe_file(self):
17        errors = [OSError, IOError]
18        for error in errors:
19            with patch("builtins.open", side_effect = error):
20                with self.assertRaises(RuinedNikuldenDinnerError):
21                    validate_recipe("file.txt")
22
23if __name__ == '__main__':
24    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
2from secret import validate_recipe, RuinedNikuldenDinnerError2from secret import validate_recipe, RuinedNikuldenDinnerError
n3from unittest.mock import patch, Mockn3from unittest.mock import patch, mock_open
44
5class TestNikuldenValidator(unittest.TestCase):5class TestNikuldenValidator(unittest.TestCase):
6    def test_valid_recipe(self):6    def test_valid_recipe(self):
7        valid_file = "доста доста риба лала Рибена глава съм аз" # fake file content7        valid_file = "доста доста риба лала Рибена глава съм аз" # fake file content
n8        mock_open  = Mock(read_data = valid_file) # immitates open built in func and gives my fake file contentn8        with patch("builtins.open", mock_open(read_data = valid_file)): # immitates open built in func and gives my fake file content
9        with patch("builtins.open", mock_open):
10            self.assertTrue(validate_recipe("someFile.txt"))9            self.assertTrue(validate_recipe("someFile.txt"))
1110
12    def test_invalid_recipe(self):11    def test_invalid_recipe(self):
13        invalid_file = "рецепта с рЙба няма нищо гладни сме малко соона"12        invalid_file = "рецепта с рЙба няма нищо гладни сме малко соона"
t14        mock_open  = Mock(read_data = invalid_file)t
15        with patch("builtins.open", mock_open):13        with patch("builtins.open", mock_open(read_data = invalid_file)):
16            self.assertFalse(validate_recipe("somefile.txt"))14            self.assertFalse(validate_recipe("somefile.txt"))
1715
18    def test_bad_recipe_file(self):16    def test_bad_recipe_file(self):
19        errors = [OSError, IOError]17        errors = [OSError, IOError]
20        for error in errors:18        for error in errors:
21            with patch("builtins.open", side_effect = error):19            with patch("builtins.open", side_effect = error):
22                with self.assertRaises(RuinedNikuldenDinnerError):20                with self.assertRaises(RuinedNikuldenDinnerError):
23                    validate_recipe("file.txt")21                    validate_recipe("file.txt")
2422
25if __name__ == '__main__':23if __name__ == '__main__':
26    unittest.main()24    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op