File: test_isolated_cli_runner.py

package info (click to toggle)
python-pytest-click 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 168 kB
  • sloc: python: 111; sh: 6; makefile: 2
file content (59 lines) | stat: -rw-r--r-- 1,648 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def test_fixture(testdir):
    testdir.makeconftest(
        """
    try:
        from mock import patch, Mock
    except ImportError:
        from unittest.mock import patch, Mock

    def pytest_configure(config):
        config.patched = patch('pytest_click.fixtures.CliRunner.isolated_filesystem')
        config.patched.start()

    def pytest_unconfigure(config):
        config.patched.stop()
    """
    )
    testdir.makepyfile(
        """
    from click.testing import CliRunner

    def test_fixture(isolated_cli_runner):
        assert isinstance(isolated_cli_runner, CliRunner)
        assert isolated_cli_runner.isolated_filesystem.called
    """
    )
    result = testdir.runpytest("--verbose")
    result.stdout.fnmatch_lines("test_fixture.py::test_fixture PASSED*")


def test_real_invocation(testdir):
    testdir.makepyfile(
        """
    import click

    def test_fixture(isolated_cli_runner):

        @click.command()
        @click.argument('f', type=click.File())
        def cat(f):
            click.echo(f.read())

        with open('hello.txt', 'w') as f:
            f.write('Hello World!')

        result = isolated_cli_runner.invoke(cat, ['hello.txt'])
        assert result.exit_code == 0
        assert result.output == 'Hello World!\\n'
    """
    )
    result = testdir.runpytest("--verbose")
    result.stdout.fnmatch_lines("test_real_invocation.py::test_fixture PASSED*")


def test_docstring(testdir):
    result = testdir.runpytest("--fixtures")
    assert (
        "    Instance of `click.testing.CliRunner` with "
        "automagically `isolated_filesystem()` called." in result.stdout.lines
    )