|  | 
| 7 | 7 | import mock | 
| 8 | 8 | path = os.path.dirname(os.path.abspath(__file__))+'/app.py' | 
| 9 | 9 | 
 | 
|  | 10 | + | 
| 10 | 11 | @pytest.mark.it('The function generate_random should exist') | 
| 11 | 12 | def test_function_exists(): | 
| 12 | 13 |  try: | 
| 13 | 14 |  from app import generate_random | 
| 14 | 15 |  except ImportError: | 
| 15 |  | - raise ImportError("The function 'generate_random' should exist on app.py") | 
|  | 16 | + raise ImportError( | 
|  | 17 | + "The function 'generate_random' should exist on app.py") | 
|  | 18 | + | 
| 16 | 19 | 
 | 
| 17 | 20 | @pytest.mark.it("The function 'generate_random' should return a random number between 0 and 9") | 
| 18 | 21 | def test_for_return(): | 
| 19 | 22 |  from app import generate_random | 
| 20 | 23 |  result = generate_random() | 
| 21 | 24 |  assert result is not None | 
| 22 |  | - for x in range(0,20): | 
|  | 25 | + for x in range(0,20): | 
| 23 | 26 |  result = generate_random() | 
| 24 |  | - assert result <= 9 and result >= 0  | 
|  | 27 | + assert result <= 9 and result >= 0 | 
|  | 28 | + | 
| 25 | 29 | 
 | 
| 26 | 30 | @pytest.mark.it('Use the function randinit() or randrange()') | 
| 27 | 31 | def test_for_type_random(): | 
| 28 | 32 |  with open(path, 'r') as content_file: | 
| 29 | 33 |  content = content_file.read() | 
| 30 | 34 |  regex = re.compile(r"random.randint\s*\(") | 
| 31 | 35 |  regex2 = re.compile(r"random.randrange\s*\(") | 
| 32 |  | - assert bool(regex.search(content)) == True or bool(regex2.search(content)) == True | 
|  | 36 | + assert bool(regex.search(content)) == True or bool( | 
|  | 37 | + regex2.search(content)) == True | 
|  | 38 | + | 
| 33 | 39 | 
 | 
| 34 | 40 | @pytest.mark.it('You should print() the output of the function') | 
| 35 | 41 | def test_function_called_for(): | 
| 36 |  | - | 
| 37 |  | - with open(path, 'r') as content_file: | 
| 38 |  | - content = content_file.read() | 
| 39 |  | - regex = re.compile(r"print\s*\(\s*generate_random\s*\(\s*\)\s*\)") | 
| 40 |  | - assert bool(regex.search(content)) == True | 
|  | 42 | + captured_output = io.StringIO() | 
|  | 43 | + sys.stdout = captured_output | 
|  | 44 | + app.generate_random() | 
|  | 45 | + sys.stdout = sys.__stdout__ | 
|  | 46 | + output = captured_output.getvalue() | 
|  | 47 | + regex = re.compile(r"\d{0,9}") | 
|  | 48 | + assert bool(regex.search(output)) == True | 
0 commit comments