Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ch11 uptimer tests #13

Closed
bfmcneill opened this issue May 20, 2021 · 1 comment · Fixed by #14
Closed

ch11 uptimer tests #13

bfmcneill opened this issue May 20, 2021 · 1 comment · Fixed by #14

Comments

@bfmcneill
Copy link

When I run the ch11 tests I seem to get an error with the click style related tests. Do you think is powershell related?

Windows 10
Python 3.8.5

===================================================================== test session starts =====================================================================
platform win32 -- Python 3.8.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- c:\users\bmcneil\git\courses\modern-python-projects\uptimer\.venv\scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\bmcneil\git\courses\modern-python-projects\uptimer
plugins: mock-3.6.1
collected 9 items

tests/test_uptimer.py::test_version PASSED                                                                                                               [ 11%]
tests/test_uptimer.py::test_check_url PASSED                                                                                                             [ 22%]
tests/test_uptimer.py::test_colorize_status PASSED                                                                                                       [ 33%]
tests/test_uptimer.py::test_check_one_url[200-green] FAILED                                                                                              [ 44%]
tests/test_uptimer.py::test_check_one_url[304-yellow] FAILED                                                                                             [ 55%]
tests/test_uptimer.py::test_check_one_url[404-bright_red] FAILED                                                                                         [ 66%]
tests/test_uptimer.py::test_check_one_url[500-red] FAILED                                                                                                [ 77%]
tests/test_uptimer.py::test_check_one_url[1-magenta] FAILED                                                                                              [ 88%]
tests/test_uptimer.py::test_check_multiple_urls FAILED                                                                                                   [100%]

========================================================================== FAILURES ===========================================================================
________________________________________________________________ test_check_one_url[200-green] ________________________________________________________________

mocker = <pytest_mock.plugin.MockerFixture object at 0x0000014DF5C5CE80>, code = 200, color = 'green'

    @pytest.mark.parametrize(
        "code,color",
        [
            (200, "green"),
            (304, "yellow"),
            (404, "bright_red"),
            (500, "red"),
            (1, "magenta"),
        ],
    )
    def test_check_one_url(mocker, code, color):
        mocker.patch("requests.head", return_value=mock_response_object(code))

        runner = CliRunner()
        result = runner.invoke(check, ["dummyurl"], color=True)

        expected_message = click.style(f"dummyurl -> {code}", fg=color)
>       assert result.output == f"{expected_message}\n"
E       AssertionError: assert 'dummyurl -> 200\n' == '\x1b[32mdummyurl -> 200\x1b[0m\n'
E         - dummyurl -> 200
E         ? -----               ----
E         + dummyurl -> 200

tests\test_uptimer.py:58: AssertionError
_______________________________________________________________ test_check_one_url[304-yellow] ________________________________________________________________

mocker = <pytest_mock.plugin.MockerFixture object at 0x0000014DF5C668E0>, code = 304, color = 'yellow'

    @pytest.mark.parametrize(
        "code,color",
        [
            (200, "green"),
            (304, "yellow"),
            (404, "bright_red"),
            (500, "red"),
            (1, "magenta"),
        ],
    )
    def test_check_one_url(mocker, code, color):
        mocker.patch("requests.head", return_value=mock_response_object(code))

        runner = CliRunner()
        result = runner.invoke(check, ["dummyurl"], color=True)

        expected_message = click.style(f"dummyurl -> {code}", fg=color)
>       assert result.output == f"{expected_message}\n"
E       AssertionError: assert 'dummyurl -> 304\n' == '\x1b[33mdummyurl -> 304\x1b[0m\n'
E         - dummyurl -> 304
E         ? -----               ----
E         + dummyurl -> 304

tests\test_uptimer.py:58: AssertionError
_____________________________________________________________ test_check_one_url[404-bright_red] ______________________________________________________________

mocker = <pytest_mock.plugin.MockerFixture object at 0x0000014DF5CBD5E0>, code = 404, color = 'bright_red'

    @pytest.mark.parametrize(
        "code,color",
        [
            (200, "green"),
            (304, "yellow"),
            (404, "bright_red"),
            (500, "red"),
            (1, "magenta"),
        ],
    )
    def test_check_one_url(mocker, code, color):
        mocker.patch("requests.head", return_value=mock_response_object(code))

        runner = CliRunner()
        result = runner.invoke(check, ["dummyurl"], color=True)

        expected_message = click.style(f"dummyurl -> {code}", fg=color)
>       assert result.output == f"{expected_message}\n"
E       AssertionError: assert 'dummyurl -> 404\n' == '\x1b[91mdummyurl -> 404\x1b[0m\n'
E         - dummyurl -> 404
E         ? -----               ----
E         + dummyurl -> 404

tests\test_uptimer.py:58: AssertionError
_________________________________________________________________ test_check_one_url[500-red] _________________________________________________________________

mocker = <pytest_mock.plugin.MockerFixture object at 0x0000014DF5CF2040>, code = 500, color = 'red'

    @pytest.mark.parametrize(
        "code,color",
        [
            (200, "green"),
            (304, "yellow"),
            (404, "bright_red"),
            (500, "red"),
            (1, "magenta"),
        ],
    )
    def test_check_one_url(mocker, code, color):
        mocker.patch("requests.head", return_value=mock_response_object(code))

        runner = CliRunner()
        result = runner.invoke(check, ["dummyurl"], color=True)

        expected_message = click.style(f"dummyurl -> {code}", fg=color)
>       assert result.output == f"{expected_message}\n"
E       AssertionError: assert 'dummyurl -> 500\n' == '\x1b[31mdummyurl -> 500\x1b[0m\n'
E         - dummyurl -> 500
E         ? -----               ----
E         + dummyurl -> 500

tests\test_uptimer.py:58: AssertionError
________________________________________________________________ test_check_one_url[1-magenta] ________________________________________________________________

mocker = <pytest_mock.plugin.MockerFixture object at 0x0000014DF5CCEB80>, code = 1, color = 'magenta'

    @pytest.mark.parametrize(
        "code,color",
        [
            (200, "green"),
            (304, "yellow"),
            (404, "bright_red"),
            (500, "red"),
            (1, "magenta"),
        ],
    )
    def test_check_one_url(mocker, code, color):
        mocker.patch("requests.head", return_value=mock_response_object(code))

        runner = CliRunner()
        result = runner.invoke(check, ["dummyurl"], color=True)

        expected_message = click.style(f"dummyurl -> {code}", fg=color)
>       assert result.output == f"{expected_message}\n"
E       AssertionError: assert 'dummyurl -> 1\n' == '\x1b[35mdummyurl -> 1\x1b[0m\n'
E         - dummyurl -> 1
E         ? -----             ----
E         + dummyurl -> 1

tests\test_uptimer.py:58: AssertionError
__________________________________________________________________ test_check_multiple_urls ___________________________________________________________________

mocker = <pytest_mock.plugin.MockerFixture object at 0x0000014DF5D00700>

    def test_check_multiple_urls(mocker):
        mocker.patch(
            "requests.head",
            side_effect=[mock_response_object(200), mock_response_object(500)],
        )

        runner = CliRunner()
        result = runner.invoke(check, ["dummyurl1", "dummyurl2"], color=True)

        expected_message1 = click.style("dummyurl1 -> 200", fg="green")
        expected_message2 = click.style("dummyurl2 -> 500", fg="red")
>       assert result.output == f"{expected_message1}\n{expected_message2}\n"
E       AssertionError: assert 'dummyurl1 -> 200\ndummyurl2 -> 500\n' == '\x1b[32mdummyurl1 -> 200\x1b[0m\n\x1b[31mdummyurl2 -> 500\x1b[0m\n'
E         - dummyurl1 -> 200
E         ? -----                ----
E         + dummyurl1 -> 200
E         - dummyurl2 -> 500
E         ? -----                ----
E         + dummyurl2 -> 500

tests\test_uptimer.py:72: AssertionError
=================================================================== short test summary info ===================================================================
FAILED tests/test_uptimer.py::test_check_one_url[200-green] - AssertionError: assert 'dummyurl -> 200\n' == '\x1b[32mdummyurl -> 200\x1b[0m\n'
FAILED tests/test_uptimer.py::test_check_one_url[304-yellow] - AssertionError: assert 'dummyurl -> 304\n' == '\x1b[33mdummyurl -> 304\x1b[0m\n'
FAILED tests/test_uptimer.py::test_check_one_url[404-bright_red] - AssertionError: assert 'dummyurl -> 404\n' == '\x1b[91mdummyurl -> 404\x1b[0m\n'
FAILED tests/test_uptimer.py::test_check_one_url[500-red] - AssertionError: assert 'dummyurl -> 500\n' == '\x1b[31mdummyurl -> 500\x1b[0m\n'
FAILED tests/test_uptimer.py::test_check_one_url[1-magenta] - AssertionError: assert 'dummyurl -> 1\n' == '\x1b[35mdummyurl -> 1\x1b[0m\n'
FAILED tests/test_uptimer.py::test_check_multiple_urls - AssertionError: assert 'dummyurl1 -> 200\ndummyurl2 -> 500\n' == '\x1b[32mdummyurl1 -> 200\x1b[0m\n\...
================================================================= 6 failed, 3 passed in 0.57s =================================================================
@switowski
Copy link
Collaborator

Hi Ben,

Thanks for reporting this! It was an interesting rabbit hole to follow while trying to figure out what's going on :)

It turns out that Windows handles ANSI escape sequences differently than Linux or Mac. For a long time, it didn't support them at all, and apparently with the new "Windows Terminal" app or PowerShell, they should be supported now (https://en.wikipedia.org/wiki/ANSI_escape_code#DOS,_OS/2,_and_Windows).

However, click uses colorama, and colorama has some custom API calls to print colors in Windows terminal. At the same time, it strips the ANSI escape codes. So while running the uptimer from this chapter in the terminal prints colors correctly, tests are failing because they expect the ANSI escape characters.

There is a pending PR to colorama to change this behavior, but it's still open since 2016, so I don't think it will be implemented anytime soon.

Click simply skips testing colorized output on Windows, so I'm afraid that's what I will have to do for now as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants