Repository: pytest-dev/pytest
File: testing/acceptance_test.py (Python)

Code before:
        result = pytester.runpytest_inprocess("--durations=2")
        assert result.ret == 0

        lines = result.stdout.get_lines_after("*slowest*durations*")
        assert "4 passed" in lines[2]

    def test_calls_showall(self, pytester: Pytester, mock_timing) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess("--durations=0")
        assert result.ret == 0
        print(result.stdout)
        TestDurations.check_tests_in_output(result.stdout.lines, "23")

    def test_calls_showall_verbose(self, pytester: Pytester, mock_timing) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess("--durations=0", "-vv")
        assert result.ret == 0
        TestDurations.check_tests_in_output(result.stdout.lines, "123")

    def test_calls_showall_durationsmin(self, pytester: Pytester, mock_timing) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess("--durations=0", "--durations-min=0.015")
        assert result.ret == 0
        TestDurations.check_tests_in_output(result.stdout.lines, "3")

    def test_calls_showall_durationsmin_verbose(
        self, pytester: Pytester, mock_timing
    ) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess(
            "--durations=0", "--durations-min=0.015", "-vv"
        )
        assert result.ret == 0
        TestDurations.check_tests_in_output(result.stdout.lines, "3")

    @staticmethod
    def check_tests_in_output(lines: Sequence[str], expected_test_numbers: str) -> None:
        found_test_numbers = "".join(
            test_number
            for test_number in "123"
            if any(f"test_{test_number}" in line and " call " in line for line in lines)
        )
        assert found_test_numbers == expected_test_numbers

    def test_with_deselected(self, pytester: Pytester, mock_timing) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess("--durations=2", "-k test_3")
        assert result.ret == 0

        result.stdout.fnmatch_lines(["*durations*", "*call*test_3*"])


Review comment (suggestion):
Looking for `"call"` is not sufficient, as the output in question contains `test_calls_showall_verbose.py::test_X PASSED` (e.g.) for all test numbers `X`, which contains `call` as well, even though the call might be missing in the durations list. Added a space before and after `call` to fix this.

Code after:
        result = pytester.runpytest_inprocess("--durations=2")
        assert result.ret == 0

        lines = result.stdout.get_lines_after("*slowest*durations*")
        assert "4 passed" in lines[2]

    def test_calls_showall(self, pytester: Pytester, mock_timing) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess("--durations=0")
        assert result.ret == 0
        TestDurations.check_tests_in_output(result.stdout.lines, 2, 3)

    def test_calls_showall_verbose(self, pytester: Pytester, mock_timing) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess("--durations=0", "-vv")
        assert result.ret == 0
        TestDurations.check_tests_in_output(result.stdout.lines, 1, 2, 3)

    def test_calls_showall_durationsmin(self, pytester: Pytester, mock_timing) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess("--durations=0", "--durations-min=0.015")
        assert result.ret == 0
        TestDurations.check_tests_in_output(result.stdout.lines, 3)

    def test_calls_showall_durationsmin_verbose(
        self, pytester: Pytester, mock_timing
    ) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess(
            "--durations=0", "--durations-min=0.015", "-vv"
        )
        assert result.ret == 0
        TestDurations.check_tests_in_output(result.stdout.lines, 3)

    @staticmethod
    def check_tests_in_output(
        lines: Sequence[str], *expected_test_numbers: int, number_of_tests: int = 3
    ) -> None:
        found_test_numbers = set(
            test_number
            for test_number in range(1, number_of_tests + 1)
            if any(
                line.endswith(f"test_{test_number}") and " call " in line
                for line in lines
            )
        )
        assert found_test_numbers == set(expected_test_numbers)

    def test_with_deselected(self, pytester: Pytester, mock_timing) -> None:
        pytester.makepyfile(self.source)
        result = pytester.runpytest_inprocess("--durations=2", "-k test_3")