【发布时间】:2021-01-11 04:44:09
【问题描述】:
我想知道是否可以对 TDD 和一般测试进行一些澄清。我正在使用python 编写我的程序,并使用pytest 作为我的测试框架。
到目前为止,我一直在做的方式,首先编写这些测试,已经开始让我的大脑对某些任务采取务实的方法。
- 除了 3 个类变量之外的所有内容都是在编写测试之前编写的,这是否意味着我在技术上没有遵循 TDD?
- 为了清楚起见,我是否应该先为未实现的方法编写整个测试,然后创建方法和功能以使测试通过?
- 我遵循 TDD 的主要目标是让每一个测试都通过,无论我对测试数据进行什么更改?(确实如此)(因此消除了潜在的错误?)
- 是否应该对班级进行大量测试(取决于班级人数)?
- 根据下面的代码,你能告诉我我是否在正确的路径上
供您参考的代码
import pytest
import os
import sys
path = os.path.dirname(os.path.abspath(__file__))
path = path.replace("\\pytest", "")
sys.path.append(path)
path += "\\pyffi"
sys.path.append(path)
from NifExplorer import NifExplorer
from NifExplorer import NifFormat
@pytest.fixture(autouse=True, scope='session')
def setup_nifExplorer():
Explorers = []
explorer = NifExplorer()
explorer.SetBlockType(NifFormat.NiNode)
explorer.SetResultPath("\\pytest\\results")
explorer.SetSearchPath("\\pytest\\nif\\base")
explorer2 = NifExplorer()
explorer2.SetBlockType(NifFormat.ATextureRenderData)
explorer2.SetResultPath("\\pytest\\results")
explorer2.SetSearchPath("\\pytest\\nif\\base")
explorer3 = NifExplorer()
explorer3.SetBlockType("NiNode")
explorer3.SetResultPath("\\pytest\\testResults")
explorer3.SetSearchPath("\\pytest\\nif\\base")
Explorers.append(explorer)
Explorers.append(explorer2)
Explorers.append(explorer3)
return Explorers
@pytest.mark.usefixtures("setup_nifExplorer")
class TestNifExplorer:
def NifExlorer_BlockType_Is_Not_None(self, setup_nifExplorer):
assert setup_nifExplorer.BlockType != None
def NifExplorer_SearchPath_Is_Not_None(self, setup_nifExplorer):
assert setup_nifExplorer.SearchPath != None
def NifExplorer_ResultPath_Is_Not_None(self, setup_nifExlorer):
assert setup_nifExlorer.ResultPath != None
@pytest.mark.parametrize('funcs', (NifExplorer_SearchPath_Is_Not_None, NifExplorer_ResultPath_Is_Not_None, NifExlorer_BlockType_Is_Not_None))
def test_NifExplorer_Variables_Equal_Not_None(self, setup_nifExplorer, funcs):
for obj in setup_nifExplorer:
funcs(self,obj)
def NifExplorer_ResultPath_Directory_Exists(self, setup_nifExplorer):
assert os.path.exists(setup_nifExplorer.ResultPath) == True
def NifExplorer_SearchPath_Directory_Exists(self, setup_nifExplorer):
assert os.path.exists(setup_nifExplorer.SearchPath) == True
def NifExplorer_SearchPath_Directory_Contains_No_Forward_Slashes(self, setup_nifExplorer):
assert setup_nifExplorer.SearchPath.count('/') < 1
def NifExplorer_ResultPath_Directory_Contains_No_Forward_Slashes(self, setup_nifExplorer):
assert setup_nifExplorer.ResultPath.count('/') < 1
@pytest.mark.parametrize('funcs', [NifExplorer_ResultPath_Directory_Exists, NifExplorer_SearchPath_Directory_Exists, NifExplorer_SearchPath_Directory_Contains_No_Forward_Slashes, NifExplorer_ResultPath_Directory_Contains_No_Forward_Slashes])
def test_NifExplorer_Directories_Exist_And_Paths_Contain_No_Forward_Slashes(self, setup_nifExplorer, funcs):
for obj in setup_nifExplorer:
funcs(self,obj)
def NifExplorer_SearchPath_Contains_Nif_Files_Recursively(self, setup_nifExplorer):
assert setup_nifExplorer.DirectoryContainsNifRecursively(setup_nifExplorer.SearchPath) == True
@pytest.mark.parametrize('funcs', [NifExplorer_SearchPath_Contains_Nif_Files_Recursively])
def test_NifExplorer_SearchPath_Contains_Nif_Files(self, setup_nifExplorer, funcs):
for obj in setup_nifExplorer:
funcs(self,obj)
if __name__ == "__main__":
pytest.main()
【问题讨论】: