Skip to content

Commit

Permalink
migrate update reference image commands 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 13fb702 commit 906a4ef
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 196 deletions.
33 changes: 0 additions & 33 deletions tests/integrationtests/cato_cmd/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
import pytest


@pytest.fixture
def dir_changer():
old_dir = os.getcwd()
yield
os.chdir(old_dir)


@pytest.fixture
def cato_config(tmp_path, test_resource_provider):
config_folder = os.path.join(str(tmp_path), "config_folder")
Expand All @@ -32,29 +25,3 @@ def cato_config(tmp_path, test_resource_provider):
with open(config_path, "wb") as f:
f.write(content.encode())
return config_folder, config_path


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

def f(suite_name, test_name):
return os.path.join(
config_folder,
"result",
suite_name,
test_name,
test_name + ".png",
)

return f


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

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

return f
19 changes: 0 additions & 19 deletions tests/integrationtests/cato_cmd/test_run_command.feature

This file was deleted.

This file was deleted.

This file was deleted.

180 changes: 74 additions & 106 deletions tests/integrationtests/cato_cmd/test_update_reference_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,150 +8,118 @@
from tests.integrationtests.command_fixture import run_cato_command


@scenario(
"test_update_reference_image.feature",
"Updating reference image which did not exist before",
)
def test_update_not_existing_before():
pass


@scenario(
"test_update_reference_image.feature",
"Updating reference image which did exist before",
)
def test_update_existing():
pass
def test_updating_reference_image_which_did_not_exist_before(cato_config):
output_image_path, output_image_hash = ensure_image_output_for_a_test(cato_config)
run_update_reference_image_command(cato_config)
assert_output_image_should_be_stored_as_reference_image(
cato_config, output_image_hash
)


@scenario(
"test_update_reference_image.feature",
"Updating reference to a not existing output file fails",
)
def test_update_not_existing_test():
pass
def test_updating_missing_reference_images(cato_config):
output_image_path, output_image_hash = ensure_image_output_for_a_test(cato_config)
ensure_reference_image_for_other_test(cato_config)
run_update_missing_reference_image_command(cato_config)
ensure_output_image_is_stored_as_reference_image(cato_config, output_image_hash)


@scenario(
"test_update_missing_reference_images.feature",
"Updating missing reference image",
)
def test_update_not_existing():
pass
def run_update_reference_image_command(cato_config):
config_folder, config_path = cato_config
os.chdir(config_folder)

command_result = run_cato_command(
["update-reference", "--test-identifier", "WriteImages/write_white_image"]
)

@given("a cato.json file with tests")
def step_impl(cato_config):
pass
assert command_result.exit_code == 0


def write_image(path, color=None):
folder = os.path.dirname(path)
if not os.path.exists(folder):
os.makedirs(folder)
img = Image.new("RGB", (800, 600), (255, 255, 255) if not color else color)
img.save(path, "PNG")
def ensure_image_output_for_a_test(cato_config):
image_path = create_result_path(cato_config, "WriteImages", "write_white_image")
write_image(image_path)
return image_path, hash_file(image_path)


def hash_file(path):
return hashlib.md5(open(path, "rb").read()).hexdigest()
def create_result_path(cato_config, suite_name, test_name):
config_folder, config_path = cato_config
return os.path.join(
config_folder,
"result",
suite_name,
test_name,
test_name + ".png",
)


def hash_directory(path):
return dirhash(path, "md5")
def ensure_reference_image_for_other_test(cato_config):
config_folder, config_path = cato_config
reference_image_folder = os.path.join(config_folder, "WriteImages")

image_path = create_reference_path(cato_config, "WriteImages", "write_black_image")
write_image(image_path, (0, 0, 0))

@given("an output image for a test")
def step_impl(scenario_context, create_result_path):
image_path = create_result_path("WriteImages", "write_white_image")
write_image(image_path)
scenario_context["output_image"] = image_path
scenario_context["output_image_checksum"] = hash_file(image_path)

other_tests_reference_image = image_path
other_tests_reference_checksum = hash_file(image_path)
reference_image_folder = reference_image_folder
reference_dir_checksum = hash_directory(reference_image_folder)

@given("reference images for other tests")
def step_impl(scenario_context, create_reference_path):
reference_image_folder = os.path.join(
scenario_context["config_folder"], "WriteImages"
)

image_path = create_reference_path("WriteImages", "write_black_image")
write_image(image_path, (0, 0, 0))
def create_reference_path(cato_config, suite_name, test_name):
config_folder, config_path = cato_config

scenario_context["other_tests_reference_image"] = image_path
scenario_context["other_tests_reference_checksum"] = hash_file(image_path)
scenario_context["reference_image_folder"] = reference_image_folder
scenario_context["reference_dir_checksum"] = hash_directory(reference_image_folder)
return os.path.join(config_folder, suite_name, test_name, "reference.png")


@given("a reference image exists for the test")
def step_impl(scenario_context, create_reference_path):
image_path = create_reference_path("WriteImages", "write_white_image")
def assert_output_image_should_be_stored_as_reference_image(
cato_config, output_image_checksum
):
reference_image_path = create_reference_path(
cato_config, "WriteImages", "write_white_image"
)

write_image(image_path, (1, 1, 1))
assert os.path.exists(reference_image_path)

scenario_context["test_reference_image"] = image_path
scenario_context["test_reference_checksum"] = hash_file(image_path)
reference_image_checksum = hash_file(reference_image_path)
assert output_image_checksum == reference_image_checksum


@when("I run the update reference image command")
def step_impl(scenario_context, dir_changer):
os.chdir(scenario_context["config_folder"])
def run_update_missing_reference_image_command(cato_config):
config_folder, config_path = cato_config
os.chdir(config_folder)

command_result = run_cato_command(
["update-reference", "--test-identifier", "WriteImages/write_white_image"]
[
"update-missing-reference-images",
]
)

assert command_result.exit_code == 0


@when("I run the update reference image command for a not existing test")
def step_impl(scenario_context, dir_changer):
os.chdir(scenario_context["config_folder"])

command_result = run_cato_command(
["update-reference", "--test-identifier", "WriteImages/NOT_EXISTING"]
def ensure_output_image_is_stored_as_reference_image(
cato_config, output_image_checksum
):
reference_image_path = create_reference_path(
cato_config, "WriteImages", "write_white_image"
)


@then("the output image should be stored as reference image")
def step_impl(scenario_context, create_reference_path):
reference_image_path = create_reference_path("WriteImages", "write_white_image")

assert os.path.exists(reference_image_path)

reference_image_checksum = hash_file(reference_image_path)
assert scenario_context["output_image_checksum"] == reference_image_checksum

assert output_image_checksum == reference_image_checksum

@then("other reference images should be untouched")
def step_impl(scenario_context):
assert (
hash_file(scenario_context["other_tests_reference_image"])
== scenario_context["other_tests_reference_checksum"]
)


@then("all reference images should be untouched")
def step_impl(scenario_context):
assert scenario_context["reference_dir_checksum"] == hash_directory(
scenario_context["reference_image_folder"]
)


@given("no reference image for a test")
def step_impl():
pass
def write_image(path, color=None):
folder = os.path.dirname(path)
if not os.path.exists(folder):
os.makedirs(folder)
img = Image.new("RGB", (800, 600), (255, 255, 255) if not color else color)
img.save(path, "PNG")


@when("I run the update missing reference images command")
def step_impl(scenario_context, dir_changer):
os.chdir(scenario_context["config_folder"])
def hash_file(path):
return hashlib.md5(open(path, "rb").read()).hexdigest()

command_result = run_cato_command(
[
"update-missing-reference-images",
]
)

assert command_result.exit_code == 0
def hash_directory(path):
return dirhash(path, "md5")

0 comments on commit 906a4ef

Please sign in to comment.