将 class 导入测试文件的正确方法

Correct way of importing a class into a testfile

我正在尝试为 Python 项目编写一些单元测试。我将 class 导入测试文件的方式对我来说似乎是错误的,或者至少不是最有效的方式。

我的文件夹结构是:

我将 ClassA 导入 test_ClassA 的方式是:

import pytest
import sys

sys.path.append(r'C:\python-projects\pytestmethodoverridetest')

from src.ClassA import ClassA

def testtest():
    # arrange
    sut = ClassA()
    expected = 5
    # act
    actual = sut.party(4)
    # assert
    assert expected == actual

我有什么方法可以改进吗?

您可以使其相对于您的文件位置:

sys.path.append( os.path.realpath(os.path.dirname(__file__)+'/..'))