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

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 MongoOperations Upert/FindModify删除mongo中的所有字段   java括号中的二叉树,而!=空值无效   java com.*.*.**。安全CustomUserDetail无法强制转换为com。***。安全CustomUserDetail   java如何编写多线程算法来发现可用socket   java Spring Boot Solr:子文档的索引列表   java如何向maven shade插件添加外部jar文件   java zebra条形码扫描仪Android studio集成   java为什么一个地方的原始类型会导致其他地方的通用调用站点被视为原始类型?   java将swing控件拆分为相等的部分   java如何在jshell中为spring项目设置类路径   java关于如何从API级别19的时间选择器中删除此白边的提示?   存储二进制字符串所有可能序列的算法,Java   测试JUnitJava。lang.NullPointerException   java Google AppEngine数据库   JavaSpring如何向数据对象注入@Value?   在eclipse java项目中包含mavenant构建库   带有JSP/EL的java省略号(缩写文本)