cli/main.py
Код: Выделить всё
import click
from datetime import *
from .utils import fn
@click.group(invoke_without_command=True, context_settings = { 'ignore_unknown_options': True, 'allow_extra_args': True })
@click.argument('args', nargs=-1)
def cli(args):
print(fn())
if __name__ == "__main__":
cli([])
Код: Выделить всё
def fn():
return("Example")
Код: Выделить всё
from click.testing import CliRunner
from mock import *
from cli.main import cli
@patch('cli.utils.fn')
class Test:
def test_patch_fn(self, mock_fn: Mock):
mock_fn.return_value = "Mocked return"
runner = CliRunner()
result = runner.invoke(cli, [])
assert result.exit_code == 0
assert result.stdout == "Mocked return"
< /code>
Запуск теста дает: < /p>
FAILED tests/test.py::Test::test_patch_fn - AssertionError: assert 'Example\n' == 'Mocked return'
< /code>
ОК, поэтому исправление не удалось. I tried the following with varying results:
patch string
error
cli.main.cli.utils.fn
Код: Выделить всё
ModuleNotFoundError: No module named 'cli.main.cli'; 'cli.main' is not a package< /code> < /td>
< /tr>
tests.test.cli.fn
Код: Выделить всё
AttributeError: does not have the attribute 'fn'< /code> < /td>
< /tr>
cli.fn
Код: Выделить всё
AttributeError: does not have the attribute 'fn'< /code> < /td>
< /tr>
cli.main.cli.fn
Код: Выделить всё
AttributeError: does not have the attribute 'fn'
Какую магическую строку мне нужно, чтобы эта работа?
Подробнее здесь: https://stackoverflow.com/questions/772 ... it-testing