此项目帮助您在单元测试时腾出特定的文件路径或将资源文件部署到特定的文件路径中。
fixturefilehandler的Python项目详细描述
夹具文件处理程序
此项目帮助您在单元测试时腾出特定的文件路径或将资源文件部署到特定的文件路径中。
上下文
关于单元测试中的文件系统,最流行的安装/拆卸任务是 差不多有两种。
- 为测试文件导出功能腾出特定的文件路径
- 将fixture文件/目录部署到特定的文件路径中,以测试文件导入/导出功能
我们必须考虑如何在单元测试之间备份现有的文件/目录。 因为也许开发人员希望保留这些手写文件用于开发。
Fixture File Handler
是实现简单实现的框架
在保存现有文件的同时腾空和部署动作。
当然,即使目标路径中没有文件,也可以正常工作。
无处不在的语言
目标
为单元测试腾空或部署文件/目录的目标文件路径。
备份
文件备份路径,用于在单元测试之间备份目标文件路径上的现有文件/目录。
资源
要部署并让产品代码在单元测试中读/写的文件/目录。
它可以测试资源文件或模板文件,如*.dist
文件。
基本行为
假期
target path | backup path |
---|---|
existing file /dir |
下降设置
target path | backup path |
---|---|
existing file / dir |
向下撕裂
target path | backup path |
---|---|
existing file / dir |
展开器
target path | backup path | resource path |
---|---|---|
existing file / dir | resource file / dir |
下降设置
target path | backup path | resource path |
---|---|---|
resource file / dir | existing file /dir | resource file / dir |
向下撕裂
target path | backup path | resource path |
---|---|---|
existing file / dir | resource file / dir |
一般行为
如果备份路径上已经存在文件/目录,
设置提升BackupAlreadyExistError
因为这是意外情况,开发人员可能需要重新设置这些备份文件的大小。
快速启动
1.安装
pip install fixturefilehandler
2-1.unittest时的情况:implement setup()和docleanups()
frompathlibimportPathimportunittestfromfixturefilehandler.factoriesimportVacatorFactoryfromfixturefilehandler.file_pathsimportRelativeVacateFilePathVACATOR=VacatorFactory.create(RelativeVacateFilePath(Path('test.txt'),Path('test.txt.bak'),Path(__file__).parent))classConfigurableTestCase(unittest.TestCase):defsetUp(self):VACATOR.setup()defdoCleanups(self):VACATOR.teardown()
2-2.pytest时的情况:工具夹具
frompathlibimportPathimportpytestfromfixturefilehandler.factoriesimportDeployerFactoryfromfixturefilehandler.file_pathsimportRelativeDeployFilePathDEPLOYER=DeployerFactory.create(RelativeDeployFilePath(Path('test.txt'),Path('test.txt.bak'),Path('testresources/test.txt.dist'),Path(__file__).parent))@pytest.fixturedeffixture_file():DEPLOYER.setup()yieldDEPLOYER.FILE_PATHDEPLOYER.teardown()deftest_something(fixture_file):"""test something"""
API
文件路径
simplevacateFilePath
此实例保留目标和备份的路径。 每条路都是相互独立的。
简单部署文件路径
此实例保存目标、备份和资源的路径。 每条路都是相互独立的。
相对路径
此实例保存到目标、备份和基的路径。 每个路径都是基于基本路径的相对路径。
相对部署文件路径
此实例保存到目标、备份、资源和基的路径。 每个路径都是基于基本路径的相对路径。
如何…
对每个测试使用不同的路径?
setup()
和teardown()
也接受文件路径参数。
单元测试时的情况:
frompathlibimportPathimportunittestfromfixturefilehandlerimportResourceFileDeployerfromfixturefilehandler.file_pathsimportRelativeDeployFilePathclassAdvancedConfigurableTestCase(unittest.TestCase):@propertydeffile_path(self)->RelativeDeployFilePath:returnRelativeDeployFilePath(Path('test.txt'),Path('test.txt.bak'),Path(f'testresources/{self._testMethodName}.txt'),Path(__file__).parent)defsetUp(self):ResourceFileDeployer.setup(self.file_path)defdoCleanups(self):ResourceFileDeployer.teardown(self.file_path)
pytest时的情况:
frompathlibimportPathimportpytestfromfixturefilehandlerimportResourceFileDeployerfromfixturefilehandler.file_pathsimportRelativeDeployFilePath@pytest.fixturedeffixture_file_advanced(request):file_path=RelativeDeployFilePath(Path('test.txt'),Path('test.txt.bak'),Path(f'testresources/{request.node.name}.txt'),Path(__file__).parent)ResourceFileDeployer.setup(file_path)yieldfile_pathResourceFileDeployer.teardown(file_path)deftest_something(fixture_file_advanced):"""test something"""