Simplify integration tests by catching ConsoleError

This commit is contained in:
Ivan Habunek 2023-03-30 12:44:32 +02:00
parent e3394c1693
commit 5cb8967c84
No known key found for this signature in database
GPG key ID: CDBD63C43A30BB95
3 changed files with 17 additions and 19 deletions

View file

@ -22,6 +22,8 @@ import uuid
from pathlib import Path
from toot import api, App, User
from toot.console import run_command
from toot.exceptions import ApiError, ConsoleError
from toot.output import print_out
# Host name of a test instance to run integration tests against
# DO NOT USE PUBLIC INSTANCES!!!
@ -84,7 +86,13 @@ def friend(app):
@pytest.fixture
def run(app, user, capsys):
def _run(command, *params, as_user=None):
run_command(app, as_user or user, command, params or [])
# The try/catch duplicates logic from console.main to convert exceptions
# to printed error messages. TODO: could be deduped
try:
run_command(app, as_user or user, command, params or [])
except (ConsoleError, ApiError) as e:
print_out(str(e))
out, err = capsys.readouterr()
assert err == ""
return strip_ansi(out)

View file

@ -1,15 +1,11 @@
import pytest
from tests.integration.conftest import TRUMPET
from toot import api
from toot.exceptions import ConsoleError
from toot.utils import get_text
def test_update_account_no_options(run):
with pytest.raises(ConsoleError) as exc:
run("update_account")
assert str(exc.value) == "Please specify at least one option to update the account"
out = run("update_account")
assert out == "Please specify at least one option to update the account"
def test_update_account_display_name(run, app, user):

View file

@ -1,7 +1,4 @@
import pytest
from tests.integration.conftest import register_account
from toot.exceptions import ConsoleError
def test_lists_empty(run):
@ -36,9 +33,8 @@ def test_list_create_delete(run):
out = run("lists")
assert out == "You have no lists defined."
with pytest.raises(ConsoleError) as exc:
run("list_delete", "mango")
assert str(exc.value) == "List not found"
out = run("list_delete", "mango")
assert out == "List not found"
def test_list_add_remove(run, app):
@ -57,14 +53,12 @@ def test_list_add_remove(run, app):
assert acc.username in out
# Account doesn't exist
with pytest.raises(ConsoleError) as exc:
run("list_add", "foo", "does_not_exist")
assert str(exc.value) == "Account not found"
out = run("list_add", "foo", "does_not_exist")
assert out == "Account not found"
# List doesn't exist
with pytest.raises(ConsoleError) as exc:
run("list_add", "does_not_exist", acc.username)
assert str(exc.value) == "List not found"
out = run("list_add", "does_not_exist", acc.username)
assert out == "List not found"
out = run("list_remove", "foo", acc.username)
assert out == f'✓ Removed account "{acc.username}"'