此项目帮助您在单元测试时腾出特定的文件路径或将资源文件部署到特定的文件路径中。

fixturefilehandler的Python项目详细描述


夹具文件处理程序

此项目帮助您在单元测试时腾出特定的文件路径或将资源文件部署到特定的文件路径中。

上下文

关于单元测试中的文件系统,最流行的安装/拆卸任务是 差不多有两种。

  1. 为测试文件导出功能腾出特定的文件路径
  2. 将fixture文件/目录部署到特定的文件路径中,以测试文件导入/导出功能

我们必须考虑如何在单元测试之间备份现有的文件/目录。 因为也许开发人员希望保留这些手写文件用于开发。

Fixture File Handler是实现简单实现的框架 在保存现有文件的同时腾空和部署动作。 当然,即使目标路径中没有文件,也可以正常工作。

无处不在的语言

目标

为单元测试腾空或部署文件/目录的目标文件路径。

备份

文件备份路径,用于在单元测试之间备份目标文件路径上的现有文件/目录。

资源

要部署并让产品代码在单元测试中读/写的文件/目录。 它可以测试资源文件或模板文件,如*.dist文件。

基本行为

假期

target pathbackup path
existing file /dir

下降设置

target pathbackup path
existing file / dir

向下撕裂

target pathbackup path
existing file / dir

展开器

target pathbackup pathresource path
existing file / dirresource file / dir

下降设置

target pathbackup pathresource path
resource file / direxisting file /dirresource file / dir

向下撕裂

target pathbackup pathresource path
existing file / dirresource 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"""

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
空字符串检查在java中未按预期工作   JavaSpringWebClient:自动计算主体的HMAC签名并将其作为头传递   foreach是否有一个Java等效的foreach循环和一个引用变量?   java如何在Eclipse中导入jar   使用特定第三方或java时lombok触发错误。*方法或构造函数   安卓 java将对象数组转换为int数组   java使一定百分比的JUnit测试通过   java Android:将Seekbar的一个值与另一个值进行比较   java将int数组(图像数据)写入文件的最佳方式是什么   java取代了系统。yml的构造函数内的getProperty   sqlite Java将公钥和私钥转换为字符串,然后再转换回字符串   安卓获取白色像素并将其保存到java opencv中的数组中   java为什么是ServerSocket。setSocketFactory静态?   Java数组似乎在不直接修改的情况下更改值