From d4f4207880023d9ce447409f7da386a64f5e949d Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Wed, 4 Dec 2024 01:23:19 +0000 Subject: [PATCH 1/2] chore: proof of concept for using custom markers to organize tests env_base_url calculates the base URL based on marker name. we can run tests for a specific environment by passing in the marker name, e.g. pytest -m dev --- tests/playwright/pytest.ini | 4 ++++ tests/playwright/test_healthcheck.py | 34 ++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/playwright/pytest.ini b/tests/playwright/pytest.ini index 03d2265ad..b4f78e4ea 100644 --- a/tests/playwright/pytest.ini +++ b/tests/playwright/pytest.ini @@ -1,2 +1,6 @@ [pytest] addopts = --tracing on -v --template=html1/index.html --report=test-results/report.html --video on +markers = + dev + test + prod diff --git a/tests/playwright/test_healthcheck.py b/tests/playwright/test_healthcheck.py index 51b6ce967..853ad12df 100644 --- a/tests/playwright/test_healthcheck.py +++ b/tests/playwright/test_healthcheck.py @@ -1,7 +1,37 @@ from playwright.sync_api import Page, expect +import pytest -def test_dev_healthcheck(page: Page): - page.goto("https://dev-benefits.calitp.org/healthcheck") +@pytest.fixture +def env_base_url(request): + marker = ( + request.node.get_closest_marker("dev") + or request.node.get_closest_marker("test") + or request.node.get_closest_marker("prod") + ) + print(marker) + + if marker: + env = marker.name + else: + env = None + + if env == "dev": + return "https://dev-benefits.calitp.org" + elif env == "test": + return "https://test-benefits.calitp.org" + elif env == "prod": + return "http://dev-benefits.calitp.org" # use dev for now, don't want to spam prod + else: + return "http://localhost:11369" # use port that will work with IdG + + +def env(*marks): + return pytest.mark.parametrize("env_name", [pytest.param(mark.name, marks=mark) for mark in marks]) + + +@env(pytest.mark.dev, pytest.mark.test, pytest.mark.prod) +def test_healthcheck(page: Page, env_base_url, env_name): + page.goto(env_base_url + "/healthcheck") expect(page.get_by_text("Healthy")).to_be_visible() From 49c58df0032e69a60d1953cf9a5b9254669d0575 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Wed, 4 Dec 2024 01:35:12 +0000 Subject: [PATCH 2/2] chore: add comments and other examples --- tests/playwright/test_healthcheck.py | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/playwright/test_healthcheck.py b/tests/playwright/test_healthcheck.py index 853ad12df..67cbd8865 100644 --- a/tests/playwright/test_healthcheck.py +++ b/tests/playwright/test_healthcheck.py @@ -26,12 +26,63 @@ def env_base_url(request): return "http://localhost:11369" # use port that will work with IdG +# region --- Test that needs to run for multiple environments + + +# region helper function def env(*marks): return pytest.mark.parametrize("env_name", [pytest.param(mark.name, marks=mark) for mark in marks]) +# endregion + + +# this is what the helper function evaluates to +# @pytest.mark.parametrize( +# "env_name", +# [ +# pytest.param("dev", marks=[pytest.mark.dev]), +# pytest.param("test", marks=[pytest.mark.test]), +# pytest.param("prod", marks=[pytest.mark.prod]), +# ], +# ) @env(pytest.mark.dev, pytest.mark.test, pytest.mark.prod) def test_healthcheck(page: Page, env_base_url, env_name): page.goto(env_base_url + "/healthcheck") expect(page.get_by_text("Healthy")).to_be_visible() + + +# endregion + + +# region--- Test that needs to run for multiple environments with parametrized values + + +@pytest.mark.parametrize( + "sub,name", + [ + pytest.param("12345", "Example", marks=[pytest.mark.dev]), + pytest.param("1234", "Le", marks=[pytest.mark.dev]), + pytest.param("4321", "Garcia", marks=[pytest.mark.test]), + pytest.param("54321", "Sample", marks=[pytest.mark.prod]), + ], +) +def test_agency_card_flow(page, env_base_url, sub, name): + print(env_base_url) + print(sub) + print(name) + + +# endregion + + +# region --- Test specific to only one environment that does not need parameterization, can use mark more simply + + +@pytest.mark.dev +def test_only_for_dev(env_base_url): + print(env_base_url) + + +# endregion