如何使 PyTest 使用父目录的 `conftest.py` 文件。

huangapple go评论99阅读模式
英文:

How to make PyTest use the `conftest.py` of a parent directory

问题

I am working on a suite of tests for a project that were not written by me. The way they were set up is as follows

  1. conftest.py
  2. testType1/
  3. a.py
  4. testType2/
  5. etc.

The file a.py is as follows:

  1. value_thats_important = None
  2. def test_to_test(self):
  3. do work that uses self.value_thats_important

Heres the problem I am encountering, the way it was originally developer, self.value_thats_important is set during the conftest.py generate tests. So if I run

or

It works totally fine. However, I also need to be able to run the test individually without calling every other test, and when I run

I get the error that Nonetype not subscriptable, which is telling me that the conftest is not getting called. I'm pretty new to pytest, so is there a way I can salvage this without having to rewrite the entire test?

英文:

I am working on a suite of tests for a project that were not written by me. The way they were set up is as follows

  1. root/
  2. conftest.py
  3. testType1/
  4. a.py
  5. testType2/
  6. etc.

The file a.py is as follows

  1. class TestClass:
  2. value_thats_important = None
  3. def test_to_test(self):
  4. do work that uses self.value_thats_important

Heres the problem I am encountering, the way it was originally developer, self.value_thats_important is set during the conftest.py generate tests. So if I run

  1. pytest root/ arg=ARG_THAT_I_NEED

or

  1. pytest testType1/ arg=ARG_THAT_I_NEED

It works totally fine. However, I also need to be able to run the test individually without calling every other test, and when I run

  1. pytest testType1/a.py::TestClass arg=ARG_THAT_I_NEED

I get the error that Nonetype not subscriptable, which is telling me that the conftest is not getting called. I'm pretty new to pytest so is there a way I can salvage this without having to rewrite the entire test?

答案1

得分: 2

这是一个用于 fixtures 的任务。具体细节会根据您的实现的特定情况而变化,因此以下是一个演示功能的示例。

目录结构

  1. tests/
  2. conftest.py
  3. testType1/
  4. a.py

tests/conftest.py

  1. import pytest
  2. @pytest.fixture(scope="session")
  3. def my_arg():
  4. yield "foo"

tests/testType1/a.py

  1. import pytest
  2. @pytest.mark.usefixtures("my_arg")
  3. class TestClass():
  4. value_thats_important = None
  5. def test_to_test(self, my_arg):
  6. self.value_thats_important = my_arg
  7. assert self.value_thats_important == "foo"

运行测试

  1. PS myproj\tests\testType1> pytest -v a.py
  2. ================================================= test session starts =================================================
  3. platform win32 -- Python 3.10.6, pytest-7.1.2, pluggy-1.0.0 -- myproj\.venv\Scripts\python.exe
  4. cachedir: .pytest_cache
  5. rootdir: myproj\tests\testType1
  6. collected 1 item
  7. a.py::TestClass::test_to_test PASSED
英文:

This is a job for fixtures. The details will vary based on the specifics of your implementation, so here's a toy example that demonstrates the functionality.

Directory structure

  1. tests/
  2. conftest.py
  3. testType1/
  4. a.py

tests/conftest.py

  1. import pytest
  2. @pytest.fixture(scope="session")
  3. def my_arg():
  4. yield "foo"

tests/testType1/a.py

  1. import pytest
  2. @pytest.mark.usefixtures("my_arg")
  3. class TestClass():
  4. value_thats_important = None
  5. def test_to_test(self,my_arg):
  6. self.value_thats_important = my_arg
  7. assert self.value_thats_important == "foo"

Running the test

  1. PS myproj\tests\testType1> pytest -v a.py
  2. ================================================= test session starts =================================================
  3. platform win32 -- Python 3.10.6, pytest-7.1.2, pluggy-1.0.0 -- myproj\.venv\Scripts\python.exe
  4. cachedir: .pytest_cache
  5. rootdir: myproj\tests\testType1
  6. collected 1 item
  7. a.py::TestClass::test_to_test PASSED

huangapple
  • 本文由 发表于 2023年4月7日 00:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定