1import unittest
2from unittest.mock import mock_open, patch
3from secret import validate_recipe, RuinedNikuldenDinnerError
4
5
6class TestNikuldenValidator(unittest.TestCase):
7 def test_valid_recipe(self):
8 # Builtins contains open and simulate the opening of file from validate_recipe()
9 mock_data = "Съпругата изпраща своя мъж на риболов и го съветва: - Ако пъстървата е много скъпа, може да уловиш и шаран. "
10 with patch("builtins.open", mock_open(read_data=mock_data)):
11 result = validate_recipe("mock_file.txt")
12 self.assertTrue(result)
13
14 def test_invalid_recipe(self):
15 # Builtins contains open and simulate the opening of file from validate_recipe()
16 mock_data = """На Никулден:
17 - И да поздравиш жена си за празника.
18 Но тя не празнува днес.
19 Кой, тая скумрия ли?..."""
20 with patch("builtins.open", mock_open(read_data=mock_data)):
21 result = validate_recipe("mock_file.txt")
22 # mock_file is the opened file from validate_recipe().
23 self.assertFalse(result)
24
25 def test_bad_recipe_file(self):
26
27 with patch("builtins.open", side_effect=OSError): # Use only OSError because it contains OSError and IOError.
28 with self.assertRaises(RuinedNikuldenDinnerError):
29 validate_recipe("mock_file.txt")
30
31
32if __name__ == "__main__":
33 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.013s
FAILED (failures=2)
Виктор Бечев
05.12.2024 12:18При така дефинираните тестове (да вземем първия), имаш следния flow:
1. Функцията бива извикана.
2. Вътре в нея се отваря някакъв файл, както ти правилно си отгатнала.
3. Файлът се чете и, тъй като е mock-нат, прочита `"This recipe is valid because it contains fish."`.
4. Функцията прави някакви други проверки, които съответстват на условието.
С така подаденият стринг - очакваш ли функцията да върне `True`?
|
f | 1 | import unittest | f | 1 | import unittest |
2 | from unittest.mock import mock_open, patch | 2 | from unittest.mock import mock_open, patch | ||
3 | from secret import validate_recipe, RuinedNikuldenDinnerError | 3 | from secret import validate_recipe, RuinedNikuldenDinnerError | ||
4 | 4 | ||||
5 | 5 | ||||
6 | class TestNikuldenValidator(unittest.TestCase): | 6 | class TestNikuldenValidator(unittest.TestCase): | ||
7 | def test_valid_recipe(self): | 7 | def test_valid_recipe(self): | ||
8 | # Builtins contains open and simulate the opening of file from validate_recipe() | 8 | # Builtins contains open and simulate the opening of file from validate_recipe() | ||
n | 9 | mock_data = "This recipe is valid because it contains fish." | n | 9 | mock_data = "Съпругата изпраща своя мъж на риболов и го съветва: - Ако пъстървата е много скъпа, може да уловиш и шаран. " |
10 | with patch("builtins.open", mock_open(read_data=mock_data)): | 10 | with patch("builtins.open", mock_open(read_data=mock_data)): | ||
11 | result = validate_recipe("mock_file.txt") | 11 | result = validate_recipe("mock_file.txt") | ||
12 | self.assertTrue(result) | 12 | self.assertTrue(result) | ||
13 | 13 | ||||
14 | def test_invalid_recipe(self): | 14 | def test_invalid_recipe(self): | ||
15 | # Builtins contains open and simulate the opening of file from validate_recipe() | 15 | # Builtins contains open and simulate the opening of file from validate_recipe() | ||
t | 16 | mock_data = "This recipe is invalid because doesn't contain fish." | t | 16 | mock_data = """На Никулден: |
17 | - И да поздравиш жена си за празника. | ||||
18 | Но тя не празнува днес. | ||||
19 | Кой, тая скумрия ли?...""" | ||||
17 | with patch("builtins.open", mock_open(read_data=mock_data)): | 20 | with patch("builtins.open", mock_open(read_data=mock_data)): | ||
18 | result = validate_recipe("mock_file.txt") | 21 | result = validate_recipe("mock_file.txt") | ||
19 | # mock_file is the opened file from validate_recipe(). | 22 | # mock_file is the opened file from validate_recipe(). | ||
20 | self.assertFalse(result) | 23 | self.assertFalse(result) | ||
21 | 24 | ||||
22 | def test_bad_recipe_file(self): | 25 | def test_bad_recipe_file(self): | ||
23 | 26 | ||||
24 | with patch("builtins.open", side_effect=OSError): # Use only OSError because it contains OSError and IOError. | 27 | with patch("builtins.open", side_effect=OSError): # Use only OSError because it contains OSError and IOError. | ||
25 | with self.assertRaises(RuinedNikuldenDinnerError): | 28 | with self.assertRaises(RuinedNikuldenDinnerError): | ||
26 | validate_recipe("mock_file.txt") | 29 | validate_recipe("mock_file.txt") | ||
27 | 30 | ||||
28 | 31 | ||||
29 | if __name__ == "__main__": | 32 | if __name__ == "__main__": | ||
30 | unittest.main() | 33 | unittest.main() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|