ฉันมีโปรเจ็กต์เล็กๆ นี้ที่ฉันใช้ pytest และ pytest-dependency กับ tox เพื่อพัฒนาการทดสอบการรวมในโค้ดบางตัว จนถึงตอนนี้ ฉันใช้คลาสพื้นฐานหนึ่งคลาส (BTestClass
) กับการทดสอบทั่วไปบางอย่างในไดเร็กทอรีราก และการทดสอบเฉพาะสำหรับแต่ละองค์ประกอบของโค้ดใน test_Component.py file
ถัดจากคลาสนั้นโดยใช้คลาส TestC
ที่สืบทอดมาจาก BTestClass
ทุกอย่างทำงานได้ดีจนกระทั่งตอนนั้น ตอนนี้ฉันต้องการเพิ่ม BTestClass2
สำหรับส่วนประกอบอีกชุดหนึ่ง ดังนั้นฉันจึงเพิ่มการสืบทอดอีกชั้นหนึ่ง แต่ตอนนี้มันใช้งานไม่ได้ pytest ตรวจสอบความถูกต้องของการทดสอบ A
ทั่วไป แต่จากนั้นก็ข้ามการทดสอบที่ขึ้นอยู่กับมัน ฉันมีความคิดว่าทำไมไม่มี.
นี่คือเค้าโครงระบบไฟล์:
λ tree /F
Folder PATH listing
Volume serial number is F029-7357
C:.
│ B.py
│ requirements-tox.txt
│ tox.ini
│
├───app_C
│ └───tests
│ test_C.py
│
└───common
A.py
common\A.py
import pytest
class ATestClass():
@pytest.mark.dependency(name='test_a')
def test_a(self):
assert True
B.py
import pytest
from common.A import ATestClass
class BTestClass(ATestClass):
@pytest.mark.dependency(name='test_b', depends=['test_a'])
def test_b(self):
assert True
test_C.py
import pytest
import sys
sys.path.append('.')
from B import *
class TestC(BTestClass):
@pytest.mark.dependency(name='test_c', depends=['test_b'])
def test_c(self):
assert True
pytest เอาต์พุต:
λ tox -- -rs
py38 installed: ...
py38 run-test-pre: PYTHONHASHSEED='367'
py38 run-test: commands[0] | pytest -x -v -rs
=============================================== test session starts ===============================================
platform win32 -- Python 3.8.1, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- ...\poc\.tox\py38\scripts\python.exe
cachedir: .tox\py38\.pytest_cache
rootdir: ...\poc
plugins: dependency-0.5.1
collected 3 items
app_C/tests/test_C.py::TestC::test_b SKIPPED [ 33%]
app_C/tests/test_C.py::TestC::test_c SKIPPED [ 66%]
app_C/tests/test_C.py::TestC::test_a PASSED [100%]
============================================= short test summary info =============================================
SKIPPED [1] .tox\py38\lib\site-packages\pytest_dependency.py:103: test_b depends on test_a
SKIPPED [1] .tox\py38\lib\site-packages\pytest_dependency.py:103: test_c depends on test_b
===================================== 1 passed, 2 skipped, 1 warning in 0.14s =====================================
_____________________________________________________ summary _____________________________________________________
py38: commands succeeded
congratulations :)
มีความคิดใดบ้างว่าทำไม test_b
จึงถูกข้ามและไม่ดำเนินการ?
แก้ไข: หากฉันทำ BTestClass
แบบสแตนด์อโลน โดยลบ A
/ ATestClass
ออกจากรูปภาพ ก็ใช้งานได้ดี
collected 2 items
app_C/tests/test_C.py::TestC::test_b PASSED [ 50%]
app_C/tests/test_C.py::TestC::test_c PASSED [100%]
test_C.py
ในcommon\A.py
- เกิดข้อผิดพลาดในการคัดลอกและวางใช่ไหม - person MrBean Bremen   schedule 13.10.2020