1import io
2import sys
3import unittest
4
5
6class StdBuffer:
7 def __init__(self):
8 self.buffer = io.StringIO()
9
10 def __enter__(self):
11 self.stds = sys.stdin, sys.stderr, sys.stdout
12 sys.stdin = self.buffer
13 sys.stderr = self.buffer
14 sys.stdout = self.buffer
15
16 def __exit__(self, *args, **kwargs):
17 sys.stdin, sys.stderr, sys.stdout = self.stds
18
19
20with StdBuffer():
21 # Creating a clue instance that the solution expects
22 import secret
23 secret.clue = secret.Clue(0)
24 import solution
25
26
27class TestLuck(unittest.TestCase):
28
29 def test_lucky(self):
30 self.assertIn('lucky', dir(solution),
31 'Убеди се, че имаш функция "lucky"')
32
33
34if __name__ == "__main__":
35 unittest.main()
1import unittest
2from importlib import reload
3
4import secret
5secret.clue = secret.Clue(0)
6import solution
7
8
9class TestLucky(unittest.TestCase):
10 """Test lucky function."""
11
12 def separate_test(self, value):
13 """Perform a separate test and reload the modules for the next one."""
14 # Normally mocking will do, but students can
15 # import clue using various ways:
16 # import secret
17 # from secret import clue
18 # from secret import clue as something_else
19 # First two cases can be covered, but the third one cannot be covered
20 # easily using mock, so let's do some monkey patching instead
21 reload(secret)
22 secret.clue = secret.Clue(value)
23 reload(solution)
24 self.assertEqual(solution.lucky(), value)
25
26 def test_lucky(self):
27 """Test with various cases."""
28 # All tests in a single test case so that only 100% gives a point
29 self.separate_test(0)
30 self.separate_test(255)
31 self.separate_test(42)
32 self.separate_test(69)
33
34
35if __name__ == '__main__':
36 unittest.main()
Георги Балтиев
22.12.2024 16:08Здравейте, имам бърз въпрос по условието. Всеки kwarg от посочените може да се ползва точно веднъж ако се извиква, но може ли value стойностите да бъдат повтаряни - тоест да извикам right\_shift и left\_shift със 7 примерно?
| ||||||||||||||||||||||
Нишка
|
Нишка
22.12.2024 17:28