github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-tools-src/tests/test_grumpy_tools.py (about)

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  
     4  """Tests for `grumpy_tools` package."""
     5  
     6  import tempfile
     7  import unittest
     8  
     9  import pytest
    10  
    11  from click.testing import CliRunner
    12  
    13  from grumpy_tools import cli
    14  
    15  
    16  @pytest.fixture
    17  def response():
    18      """Sample pytest fixture.
    19  
    20      See more at: http://doc.pytest.org/en/latest/fixture.html
    21      """
    22      # import requests
    23      # return requests.get('https://github.com/audreyr/cookiecutter-pypackage')
    24  
    25  
    26  def test_content(response):
    27      """Sample pytest test function with the pytest fixture as an argument."""
    28      # from bs4 import BeautifulSoup
    29      # assert 'GitHub' in BeautifulSoup(response.content).title.string
    30  
    31  
    32  @pytest.mark.xfail
    33  def test_command_line_interface(capfd):
    34      """Test the CLI."""
    35      runner = CliRunner()
    36      out, err = capfd.readouterr()
    37  
    38      help_result = runner.invoke(cli.main, ['--help'])
    39      assert help_result.exit_code == 0
    40  
    41      result = runner.invoke(cli.main)
    42      assert result.exit_code == 0
    43      assert '>>> ' in out, (result.output, out, err)
    44  
    45  
    46  def test_run_input_inline(capfd):
    47      runner = CliRunner()
    48      result = runner.invoke(cli.main, ['run', '-c', "print('Hello World')",])
    49      out, err = capfd.readouterr()
    50      assert out == 'Hello World\n', (err, result.output)
    51      assert result.exit_code == 0
    52  
    53  
    54  def test_run_input_stdin(capfd):
    55      runner = CliRunner()
    56      result = runner.invoke(cli.main, ['run'], input="print('Hello World')")
    57  
    58      out, err = capfd.readouterr()
    59      assert out == 'Hello World\n', (err, result.output)
    60      assert result.exit_code == 0
    61  
    62  
    63  def test_run_input_file(capfd):
    64      runner = CliRunner()
    65      with tempfile.NamedTemporaryFile() as script_file:
    66          script_file.write("print('Hello World')")
    67          script_file.flush()
    68  
    69          result = runner.invoke(cli.main, ['run', script_file.name])
    70  
    71      out, err = capfd.readouterr()
    72      assert out == 'Hello World\n', (err, result.output)
    73      assert result.exit_code == 0