django测试帮助程序来管理文件存储的副作用。

django-override-storage的Python项目详细描述


不要用测试文件或文件系统模拟代码填满磁盘!

这个项目提供了一些工具来帮助您减少使用 测试期间的文件字段。

安装

pip install django-override-storage

简单用法

不带任何参数调用override_storage()将修补所有 FileField将文件内容存储在内存缓存中的字段 并在离开其上下文时将字段返回到其以前的存储。这个 在每次测试结束时或退出上下文时都会删除存储缓存。 管理器,具体取决于如何调用它。

它可以类似于django.test.utils.override_settings:作为类使用 decorator,方法decorator或上下文管理器。

fromdjango.core.files.baseimportContentFilefromdjango.testimportTestCasefromoverride_storageimportoverride_storagefromoverride_storage.storageimportLocMemStoragefrom.modelsimportSimpleModelclassOverrideStorageTestCase(TestCase):deftest_context_manager(self):withoverride_storage():# By default, all files saved to in memory cache.obj=SimpleModel()obj.upload_file.save('test.txt',ContentFile('content'))# Get your file back!content=obj.upload_file.read()@override_storage(storage=LocMemStorage())deftest_method_decorator(self):# You can also specify to replace all storage backends with a# storage instance of your choosing. Depending on the storage type,# this could mean all writes will persist for the life of the# instance. This does not really matter if you wanted to pass in a# FileSystemStorage instance as those writes will be persisted# regardless....@override_storage(storage=LocMemStorage)deftest_method_decorator(self):# Passing in a class will create a new instance for every test.@override_storage()deftest_method_decorator(self):# Used as a method decorator....@override_storage()classOverrideStorageClassTestCase(TestCase):# You can also wrap classes....

它还可以通过自定义测试运行程序全局使用。这是可以实现的 通过在设置文件中设置TEST_RUNNER设置或其他方式 您可以选择定义django测试运行器。

警告

TEST_RUNNER只在 测试,因为没有钩子指向的setUp/tearDown方法 测试类。使用override_storage.LocMemStorageDiscoverRunner将共享 所有测试中的单个内存缓存。但这不会影响你 测试,如果你写了很多大文件,你可能会耗尽内存。

TEST_RUNNER='override_storage.LocMemStorageDiscoverRunner'

存储信息

override_storagelocmem_stats_override_storage所有补丁 FileField将文件内容存储在内存缓存中的字段 并在离开其上下文时将字段返回到其以前的存储。

除了常规功能外,它还返回一个包含信息的对象 关于对测试存储的_open_save方法的调用。在 通用IT记录哪些字段有读取或写入的文件,以及 记录文件的名称。

fromdjango.core.files.baseimportContentFilefromdjango.testimportTestCasefromoverride_storageimportlocmem_stats_override_storagefrom.modelsimportSimpleModelclassOverrideStorageTestCase(TestCase):deftest_context_manager(self):withlocmem_stats_override_storage()asstorage_stats:# All files saved to in memory cache.obj=SimpleModel()obj.upload_file.save('test.txt',ContentFile('content'))# Check how many files have been savedself.storage_stats.save_cnt# Check which fields were read or savedself.storage_stats.fields_savedself.storage_stats.fields_read# Get a list of names, by field, which have been saved or read.self.storage_stats.reads_by_fieldself.storage_stats.saves_by_field# Get your file back!content=obj.upload_file.read()@locmem_stats_override_storage(name='storage_stats')deftest_method_decorator(self,storage_stats):# access to storage stats by specifying `name` which is the name of# the kwarg to be used in the function signature....@locmem_stats_override_storage(name='storage_stats')classOverrideStorageClassTestCase(TestCase):storage_stats=None# access to storage stats by specifying attr_name...

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

推荐PyPI第三方库


热门话题
java为什么整数构造函数方法不再需要属性?   java单函数从广播接收器检索数据   Java类中的Scala设置器和获取器   JAVA MVC表选择的行ID和mysql db ID不同   java在Multimaps Guava中获取值   jakarta ee Intellij IDEA Java类在保存时不自动编译   java在另一个Spring实体中使用Spring实体   JTwitter库的安卓类一直在给我java。朗朗:温特韦尔。杰特维特。啁啾   java停止启动应用程序   java Jersey测试NoSuchMethodError   java如何处理合成元素上的注释   java崩溃shell无法通过ssh连接   java计算两个日期之间的天数后(取自edittext)如何在新的edittext中显示天数