From afcf14ce0738f8bf5f46b677b225497333f06013 Mon Sep 17 00:00:00 2001 From: njlr Date: Fri, 29 Nov 2024 19:52:21 +0000 Subject: [PATCH] Add example of test fixtures and chdir --- examples/pytest/BUILD.bazel | 4 ++++ examples/pytest/fixtures/hello.json | 3 +++ examples/pytest/foo_test.py | 6 ++++++ 3 files changed, 13 insertions(+) create mode 100644 examples/pytest/fixtures/hello.json diff --git a/examples/pytest/BUILD.bazel b/examples/pytest/BUILD.bazel index 24c0d607..c0f5b9d1 100644 --- a/examples/pytest/BUILD.bazel +++ b/examples/pytest/BUILD.bazel @@ -3,6 +3,7 @@ load("@aspect_rules_py//py:defs.bzl", "py_pytest_main", "py_test") py_pytest_main( name = "__test__", deps = ["@pypi_pytest//:pkg"], + chdir = package_name(), # So that test fixtures are available at the correct path ) py_test( @@ -14,6 +15,9 @@ py_test( imports = ["../.."], main = ":__test__.py", package_collisions = "warning", + data = glob([ + "fixtures/*.json", + ]), deps = [ ":__test__", "@pypi_ftfy//:pkg", diff --git a/examples/pytest/fixtures/hello.json b/examples/pytest/fixtures/hello.json new file mode 100644 index 00000000..83f157b0 --- /dev/null +++ b/examples/pytest/fixtures/hello.json @@ -0,0 +1,3 @@ +{ + "message": "Hello, world." +} \ No newline at end of file diff --git a/examples/pytest/foo_test.py b/examples/pytest/foo_test.py index 0c309f86..33a7ab7c 100644 --- a/examples/pytest/foo_test.py +++ b/examples/pytest/foo_test.py @@ -1,5 +1,11 @@ import pytest +import json def test_add(): assert 1 + 1 == 2, "Expected 1 + 1 to equal 2" + +def test_hello_json(): + content = open('fixtures/hello.json', 'r').read() + data = json.loads(content) + assert data["message"] == "Hello, world.", "Message is as expected"