From b216171d908eedada4a1ba0fc15106ac5e1d570f Mon Sep 17 00:00:00 2001 From: Nataliia Date: Tue, 4 Oct 2022 14:59:32 +0300 Subject: [PATCH 1/2] added inspect --- tests/test_main.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 794a811c..19d7abf8 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,7 +1,7 @@ import pytest +import inspect -from app.main import count_occurrences - +from app import main @pytest.mark.parametrize( "phrase,letter,count", @@ -15,16 +15,13 @@ ] ) def test_count_occurrences(phrase, letter, count): - assert count_occurrences(phrase, letter) == count, ( + assert main.count_occurrences(phrase, letter) == count, ( f"Function 'count_occurrences' should return {count}, " f"when 'phrase'='{phrase}' and 'letter'='{letter}'" ) - -def test_removed_comment(): - import app - with open(app.main.__file__, "r") as f: - file_content = f.read() - assert "# write your code here" not in file_content, \ - "You have to remove the unnecessary comment '# write your code here'" +def test_removed_comment(): + lines = inspect.getsource(main) + assert "# write your code here" not in lines, ("You have to" + " remove the unnecessary comment '# write your code here'") From 874f08be32f5296add9550a7067f733f3cecf16a Mon Sep 17 00:00:00 2001 From: Nataliia Date: Fri, 4 Nov 2022 12:49:07 +0200 Subject: [PATCH 2/2] inspect for function, not for all the file --- tests/test_main.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 19d7abf8..ab49b418 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,7 +1,9 @@ -import pytest import inspect -from app import main +import pytest + +from app.main import count_occurrences + @pytest.mark.parametrize( "phrase,letter,count", @@ -12,16 +14,17 @@ ("Abracadabra", "A", 5), ("", "a", 0), ("Samsung", "b", 0), - ] + ], ) def test_count_occurrences(phrase, letter, count): - assert main.count_occurrences(phrase, letter) == count, ( + assert count_occurrences(phrase, letter) == count, ( f"Function 'count_occurrences' should return {count}, " f"when 'phrase'='{phrase}' and 'letter'='{letter}'" ) def test_removed_comment(): - lines = inspect.getsource(main) - assert "# write your code here" not in lines, ("You have to" - " remove the unnecessary comment '# write your code here'") + lines = inspect.getsource(count_occurrences) + assert "# write your code here" not in lines, ( + "You have to" " remove the unnecessary comment '# write your code here'" + )