Skip to content

Commit e83fb49

Browse files
njlralexeagle
andauthored
docs/example-pytest-fixtures (#452)
Adds an example of test fixtures and `chdir`. This option was quite hard to discover, however test fixtures are common. Co-authored-by: Alex Eagle <alex@aspect.dev>
1 parent 1d6cd38 commit e83fb49

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

examples/pytest/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ py_library(
88

99
py_pytest_main(
1010
name = "__test__",
11+
chdir = package_name(), # So that test fixtures are available at the correct path
1112
deps = [
1213
"@pypi_coverage//:pkg",
1314
"@pypi_pytest//:pkg",
@@ -24,6 +25,9 @@ py_test(
2425
imports = ["../.."],
2526
main = ":__test__.py",
2627
package_collisions = "warning",
28+
data = glob([
29+
"fixtures/*.json",
30+
]),
2731
deps = [
2832
":__test__",
2933
":lib",
@@ -43,6 +47,9 @@ py_test(
4347
imports = ["../.."],
4448
main = ":__test__.py",
4549
package_collisions = "warning",
50+
data = glob([
51+
"fixtures/*.json",
52+
]),
4653
deps = [
4754
":__test__",
4855
":lib",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"message": "Hello, world."
3+
}

examples/pytest/foo_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import pytest
2+
import json
23

34
from examples.pytest.foo import add
45

56
def test_add():
67
assert add(1, 1) == 2, "Expected 1 + 1 to equal 2"
8+
9+
def test_hello_json():
10+
content = open('fixtures/hello.json', 'r').read()
11+
data = json.loads(content)
12+
assert data["message"] == "Hello, world.", "Message is as expected"

0 commit comments

Comments
 (0)