pythonpytest

Fixture `record_property` not found


I'm trying to use the record_property fixture (https://docs.pytest.org/en/stable/reference/reference.html#record-property). My build file looks like this:

load("@pip//:requirements.bzl", "requirement")
py_test(
    name = "test_foo",
    srcs = ["test_foo.py"],
    deps = [
        requirement("pytest"),
    ],
)

And test_foo.py looks like this:

import pytest
import unittest
from unittest import TestCase

class TestFoo(TestCase):
    def test_escape(self, record_property):
        pass

if __name__ == "__main__":
    unittest.main()

When I run the test (bazel test //path/to:test_foo), I get an error about the record_property fixture being undefined:

TypeError: test_escape() missing 1 required positional argument: 'record_property'

I suspect this is due to some nuance in my build rule, bazel invocation, or Python set up. I've researched this a bit, and it does sound like other folks have encountered "missing fixture" errors, though not this flavor in particular. Any pointers are appreciated :)


Solution

  • You can't use pytest fixtures in this way with unittest.TestCase classes. From https://docs.pytest.org/en/stable/how-to/unittest.html:

    unittest.TestCase methods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run general unittest.TestCase test suites.

    The above usefixtures and autouse examples should help to mix in pytest fixtures into unittest suites.

    You can also gradually move away from subclassing from unittest.TestCase to plain asserts and then start to benefit from the full pytest feature set step by step.