Skip to content

Commit

Permalink
migrate run command tests away from pytest-bdd
Browse files Browse the repository at this point in the history
  • Loading branch information
Latios96 committed Nov 10, 2021
1 parent e33192c commit 13fb702
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 75 deletions.
24 changes: 10 additions & 14 deletions tests/integrationtests/cato_cmd/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import pytest


@pytest.fixture
def scenario_context():
return {}


@pytest.fixture
def dir_changer():
old_dir = os.getcwd()
Expand All @@ -18,15 +13,13 @@ def dir_changer():


@pytest.fixture
def cato_config(tmp_path, test_resource_provider, scenario_context):
def cato_config(tmp_path, test_resource_provider):
config_folder = os.path.join(str(tmp_path), "config_folder")
shutil.copytree(
test_resource_provider.resource_by_name("cato_cmd_integ_tests"), config_folder
)

scenario_context["config_folder"] = config_folder
config_path = os.path.join(config_folder, "cato.json")
scenario_context["config_path"] = config_path

with open(config_path) as f:
content = f.read()
Expand All @@ -38,13 +31,16 @@ def cato_config(tmp_path, test_resource_provider, scenario_context):

with open(config_path, "wb") as f:
f.write(content.encode())
return config_folder, config_path


@pytest.fixture
def create_result_path(scenario_context):
def create_result_path(cato_config):
config_folder, config_path = cato_config

def f(suite_name, test_name):
return os.path.join(
scenario_context["config_folder"],
config_folder,
"result",
suite_name,
test_name,
Expand All @@ -55,10 +51,10 @@ def f(suite_name, test_name):


@pytest.fixture
def create_reference_path(scenario_context):
def create_reference_path(cato_config):
config_folder, config_path = cato_config

def f(suite_name, test_name):
return os.path.join(
scenario_context["config_folder"], suite_name, test_name, "reference.png"
)
return os.path.join(config_folder, suite_name, test_name, "reference.png")

return f
101 changes: 40 additions & 61 deletions tests/integrationtests/cato_cmd/test_run_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,54 @@
import sys

import requests
from pytest_bdd import scenario, given, when, then

from tests.integrationtests.command_fixture import run_cato_command, run_command


@scenario(
"test_run_command.feature",
"Run all tests the first time should fail because of missing reference images",
)
def test_all_test_failing_not_existing_reference_images():
pass
def test_run_tests_fails_because_of_missing_reference_images(cato_config, live_server):
result = run_the_cato_command(live_server, cato_config)
assert_all_tests_should_have_been_executed(result)
assert_a_failure_message_was_printed(result)
assert_a_failure_message_for_missing_reference_image_was_printed(result)
assert_the_failure_result_is_available_on_the_server(result, live_server)


@scenario(
"test_run_command.feature",
"Run all tests should succeed",
)
def test_all_test_succeed():
pass
def test_running_all_tests_should_succeed(cato_config, live_server):
reference_images_exist(cato_config)
result = run_the_cato_command(live_server, cato_config)
assert_all_tests_should_have_been_executed(result)
assert_no_failure_message_was_printed(result)
assert_success_message_was_printed(result)
assert_the_success_result_is_available_on_the_server(result, live_server)


@given("a cato.json file with tests")
def step_impl(cato_config):
pass


@when("I run the run command")
def step_impl(scenario_context, dir_changer, live_server):
os.chdir(scenario_context["config_folder"])

def run_the_cato_command(live_server, cato_config):
config_folder, config_path = cato_config
os.chdir(config_folder)
result = run_cato_command(["run", "-u", live_server.server_url(), "-vvv"])
scenario_context["command_result"] = result

assert result.exit_code == 0
return result


@given("no reference images for the tests")
def step_impl():
pass
def assert_all_tests_should_have_been_executed(command_result):
assert command_result.output_contains_line("Ran 2 tests")


@then("All tests should have been executed")
def step_impl(scenario_context):
assert scenario_context["command_result"].output_contains_line("Ran 2 tests")
def assert_a_failure_message_was_printed(command_result):
assert command_result.output_contains_line_matching("2 failed")


@then("A failure message should be printed")
def step_impl(scenario_context):
assert scenario_context["command_result"].output_contains_line_matching("2 failed")


@then("Failure Messages for missing reference image should be printed")
def step_impl(scenario_context):
assert scenario_context["command_result"].output_contains_line_matching(
def assert_a_failure_message_for_missing_reference_image_was_printed(command_result):
assert command_result.output_contains_line_matching(
"write_white_image failed .*: Reference image <not found> does not exist!"
)
assert scenario_context["command_result"].output_contains_line_matching(
assert command_result.output_contains_line_matching(
"write_black_image failed .*: Reference image <not found> does not exist!"
)


@then("the result should be available at the server")
def step_impl(live_server, scenario_context):
run_id = _parse_run_id_from_output(scenario_context)
def assert_the_failure_result_is_available_on_the_server(command_result, live_server):
run_id = _parse_run_id_from_output(command_result)
run_json = _read_run_from_server(live_server, run_id)
assert run_json == [
{
Expand All @@ -87,13 +71,13 @@ def step_impl(live_server, scenario_context):
]


@given("reference images exist for the tests")
def step_impl(scenario_context):
def reference_images_exist(cato_config):
config_folder, config_path = cato_config
cato_cmd = [
sys.executable,
os.path.join(scenario_context["config_folder"], "WriteImages", "write.py"),
os.path.join(config_folder, "WriteImages", "write.py"),
os.path.join(
scenario_context["config_folder"],
config_folder,
"WriteImages",
"write_white_image",
"reference.png",
Expand All @@ -105,9 +89,9 @@ def step_impl(scenario_context):

cato_cmd = [
sys.executable,
os.path.join(scenario_context["config_folder"], "WriteImages", "write.py"),
os.path.join(config_folder, "WriteImages", "write.py"),
os.path.join(
scenario_context["config_folder"],
config_folder,
"WriteImages",
"write_black_image",
"reference.png",
Expand All @@ -118,21 +102,16 @@ def step_impl(scenario_context):
assert result.exit_code == 0


@then("no failure message should be printed")
def step_impl(scenario_context):
assert not scenario_context["command_result"].output_contains_line_matching(
".* failed"
)
def assert_no_failure_message_was_printed(command_result):
assert not command_result.output_contains_line_matching(".* failed")


@then("a success message should be printed")
def step_impl(scenario_context):
assert not scenario_context["command_result"].output_contains_line("2 succeded ✅")
def assert_success_message_was_printed(command_result):
assert not command_result.output_contains_line("2 succeded ✅")


@then("the success result should be available at the server")
def step_impl(scenario_context, live_server):
run_id = _parse_run_id_from_output(scenario_context)
def assert_the_success_result_is_available_on_the_server(command_result, live_server):
run_id = _parse_run_id_from_output(command_result)
run_json = _read_run_from_server(live_server, run_id)
assert run_json == [
{
Expand Down Expand Up @@ -162,8 +141,8 @@ def _read_run_from_server(live_server, run_id):
return json


def _parse_run_id_from_output(scenario_context):
match = scenario_context["command_result"].output_contains_line_matching(
def _parse_run_id_from_output(command_result):
match = command_result.output_contains_line_matching(
"You can find your run at http://127.0.0.1:\d+/#/projects/\d+/runs/(\d+)"
)
assert match
Expand Down

0 comments on commit 13fb702

Please sign in to comment.