-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI tool for running schemathesis test cases
- Fix: #30
- Loading branch information
1 parent
f7affd0
commit e4c2236
Showing
9 changed files
with
230 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Iterable | ||
from urllib.parse import urlparse | ||
|
||
import click | ||
|
||
from . import runner | ||
|
||
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) | ||
|
||
DEFAULT_CHECKS_NAMES = tuple(check.__name__ for check in runner.DEFAULT_CHECKS) | ||
|
||
|
||
@click.group(context_settings=CONTEXT_SETTINGS) | ||
@click.version_option() | ||
def main() -> None: | ||
"""Command line tool for testing your web application built with Open API / Swagger specifications.""" | ||
|
||
|
||
@main.command(short_help="Perform schemathesis test.") | ||
@click.argument("schema", type=str) | ||
@click.option( | ||
"--checks", | ||
"-c", | ||
multiple=True, | ||
help="List of checks to run.", | ||
type=click.Choice(DEFAULT_CHECKS_NAMES), | ||
default=DEFAULT_CHECKS_NAMES, | ||
) | ||
def run(schema: str, checks: Iterable[str] = DEFAULT_CHECKS_NAMES) -> None: | ||
"""Perform schemathesis test against an API specified by SCHEMA. | ||
SCHEMA must be a valid URL pointing to an Open API / Swagger specification. | ||
""" | ||
if not urlparse(schema).netloc: | ||
raise click.UsageError("Invalid SCHEMA, must be a valid URL.") | ||
|
||
selected_checks = tuple(check for check in runner.DEFAULT_CHECKS if check.__name__ in checks) | ||
|
||
click.echo("Running schemathesis test cases ...") | ||
|
||
runner.execute(schema, checks=selected_checks) | ||
|
||
click.echo("Done.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import tempfile | ||
from functools import partial | ||
|
||
import pytest | ||
from click.testing import CliRunner | ||
|
||
from schemathesis import commands, runner | ||
|
||
|
||
@pytest.fixture() | ||
def schemathesis_cmd(testcmd): | ||
return partial(testcmd, "schemathesis") | ||
|
||
|
||
def test_commands_help(schemathesis_cmd): | ||
result = schemathesis_cmd() | ||
|
||
assert result.ret == 0 | ||
assert result.stdout.get_lines_after("Commands:") == [" run Perform schemathesis test."] | ||
|
||
result_help = schemathesis_cmd("--help") | ||
result_h = schemathesis_cmd("-h") | ||
|
||
assert result.stdout.lines == result_h.stdout.lines == result_help.stdout.lines | ||
|
||
|
||
def test_commands_version(schemathesis_cmd): | ||
result = schemathesis_cmd("--version") | ||
|
||
assert result.ret == 0 | ||
assert "version" in result.stdout.lines[0] | ||
|
||
|
||
def test_commands_run_errors(schemathesis_cmd): | ||
result_no_args = schemathesis_cmd("run") | ||
|
||
assert result_no_args.ret == 2 | ||
assert "Missing argument" in result_no_args.stderr.lines[-1] | ||
|
||
result_invalid = schemathesis_cmd("run", "not-url") | ||
|
||
assert result_invalid.ret == 2 | ||
assert "Invalid SCHEMA" in result_invalid.stderr.lines[-1] | ||
|
||
|
||
def test_commands_run_help(schemathesis_cmd): | ||
result_help = schemathesis_cmd("run", "--help") | ||
|
||
assert result_help.ret == 0 | ||
assert result_help.stdout.lines == [ | ||
"Usage: schemathesis run [OPTIONS] SCHEMA", | ||
"", | ||
" Perform schemathesis test against an API specified by SCHEMA.", | ||
"", | ||
" SCHEMA must be a valid URL pointing to an Open API / Swagger", | ||
" specification.", | ||
"", | ||
"Options:", | ||
" -c, --checks [not_a_server_error]", | ||
" List of checks to run.", | ||
" -h, --help Show this message and exit.", | ||
] | ||
|
||
|
||
def test_commands_run_schema_uri(mocker): | ||
m_execute = mocker.patch("schemathesis.runner.execute") | ||
cli = CliRunner() | ||
|
||
schema_uri = "https://example.com/swagger.json" | ||
result_schema_uri = cli.invoke(commands.run, [schema_uri]) | ||
|
||
assert result_schema_uri.exit_code == 0 | ||
m_execute.assert_called_once_with(schema_uri, checks=runner.DEFAULT_CHECKS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters