Skip to content

Instantly share code, notes, and snippets.

@cb109
Last active May 30, 2025 06:51
Show Gist options
  • Save cb109/f480e6890b543d4a6614f8776a9b8a9a to your computer and use it in GitHub Desktop.
Save cb109/f480e6890b543d4a6614f8776a9b8a9a to your computer and use it in GitHub Desktop.
Passing information from pytest to cypress for e2e tests using an env var and a test-specific view
import os
def e2e_my_test(db):
"""We assume this fixture is run before launching the cypress spec using the same name."""
my_object = ...
# Note: We may have to make this URL relative or replace its domain
# with live_server.url to avoid running into same-origin constraints
# in the browser. Though cypress v12 upwards is fine with that!
os.environ["MY_DYNAMIC_URL"] = my_object.url
describe('Test against a dynamic URL', () => {
it('should visit the dynamic URL', () => {
const envLookupURL = Cypress.env('apiBaseUrl') + 'e2e/env' + '?env_var=MY_DYNAMIC_URL';
cy.request({
method: 'GET',
url: envLookupURL,
})
.then((response) => {
const dynamicURL = response.body;
cy.visit(dynamicURL);
});
});
});
...
if is_testrun():
urlpatterns.extend(
[
# This view takes an env_var GET parameter and returns its value from os.environ.
re_path(r"^e2e/env$", e2e_env_lookup),
]
)
@api_view(("GET",))
@permission_classes(())
def e2e_env_lookup(request):
if not is_testrun():
raise PermissionDenied()
env_var = request.GET["env_var"]
value = os.environ.get(env_var, None)
return Response(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment