用于处理带有临时目录和文件的python测试用例的实用程序,供使用“pyunit”或“unittest”的用户使用。

tempcase的Python项目详细描述


温度

Build Status

用于处理带有临时目录和文件的python测试用例的实用程序,供使用pyunit/unittest的人使用。

支持Python2.7和3.4+。

N.B.,该库的存在是^ {STR 1 } $ < <强> > ^ {< CD2>}。 如果您想要一个强大的、现代的、pythonic的测试范式,请使用^{}。 拜托。

动机

单元测试应该尽可能隔离,但是在测试文件生成方法时,通常不方便 手动处理具有信息性名称且远离代码的单独输出目录。

pytest使用tmpdir夹具很容易解决这个问题,但是unittest没有这样的实用程序。

tempcaseunittest样式的测试用例提供了一个基类,其中包含用于创建临时 目录根据需要,具有自动清除功能,可出于调试目的禁用。

安装

pip install tempcase

用法

importosimporttempcaseclassMyTestCase(tempcase.TempCase):_project_name='mylibrary'deftest_creates_file(self):"""        Test that ``my_file.txt`` is successfully created.        The first call to ``path_to`` for a ``TestCase`` will create a directory in your default temp directory,         which has the name of the project as defined above, the name of the ``TestCase``, a timestamp, and a random        alphanumeric string.        The first call to ``path_to`` for a test method will create a subdirectory within that, named for the         test method.        The test method directory and its contents will be deleted by ``tearDown``.        The ``TestCase`` directory, if empty, will be cleaned up by ``tearDownClass``.        """fpath=self.path_to('my_file.txt')# os.path.join-like syntaxopen(fpath,'w').close()self.assertTrue(os.path.isfile(fpath))deftest_something_else(self):"""No unnecessary directories are created"""self.assertTrue(True)deftest_creates_file_no_cleanup(self):"""        Setting ``self._cleanup = False`` anywhere in a test method will disable cleanup just for that method,         allowing you to look at the output for debugging purposes.        The containing ``TestCase`` directory will also not be deleted.        """fpath=self.path_to('my_other_file.txt')open(fpath,'w').close()self.assertTrue(os.path.isfile(fpath))self._cleanup=FalsedeftearDown(self):"""Be sure to call the super() tearDown if you override it! Same goes for tearDownClass."""super().tearDown()# python 3+print("I did a tearDown")classMyTestCaseWithNoCleanup(tempcase.TempCase):_project_name='mylibrary'_cleanup=Falsedeftest_creates_file(self):"""This will not be cleaned up, by default"""fpath=self.path_to('my_file.txt')open(fpath,'w').close()deftest_creates_file_with_cleanup(self)"""You can clean up individual methods if you like"""self._cleanup=Trueopen(self.path_to('my_file.txt'),'w').close()
<>对于已经具有脆弱的继承链的现有项目,每个测试用例和定义的本地路径 方法体(不在setUp)中,in_tempdir装饰器可能有用。它创建一个临时目录, 更改工作目录,然后在执行后更改回并清除临时目录。

importunittestimporttempcaseclassMyOldTestCase(unittest.TestCase):@tempcase.in_tempdir('my_project')deftest_old_code(self):"""        This method has now be ``os.chdir()``'d into a temporary directory which will be cleaned up.        The working directory will then automatically switch back to whatever it was before.        The directory's cleanup cannot be prevented.        """open('my_local_file.txt','w').close()_project_name='my_project'# can be defined in the class or passed to the decorator@tempcase.in_tempdirdeftest_slightly_newer_code(self):pass

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

推荐PyPI第三方库


热门话题
检查java中两个链表之间的子集   java仅在findViewById()下显示一个ImageButton   在Java Graphics2D中,文本如何在矩形上居中对齐?   java需要一个用于电子表格计算器的正则表达式   ^java中的运算符   java通过一些属性配置文件根据环境动态更改wsdl端点URL   java Ebean/Play框架关系未更新   集合如何将POJO列表转换为Java流中的映射<String,List>?   java为什么JFrame不显示整个图像?   java如何将调用静态导入的泛型方法的结果传递给另一个方法?   迭代器或foreach中的java延迟   需要java socket logback日志接收器   在Java中初始化Map的静态数组   雅加达邮件Java MimeMail:解码后获得额外字符   java为什么这个xmldom解析器不能正确解析rtept、name和cmt标记?   java如何刷新Log4J2中的异步记录器(带中断器)   java使用构建插件pom生成的jar。xml作为同一pom中的依赖项   java基于位置的序列ADT如何在O(1)时间内插入元素?   java ORM实体与DDD实体   Java对象分配