轻松生成随机Unicode测试数据
testdata的Python项目详细描述
生成随机测试数据。
这些只是一些方便的函数,旨在使 测试代码。
要在测试中使用测试数据,只需将其导入测试模块:
import testdata
要安装,请使用pip:
pip install testdata
或者,pip使用github:
pip install --upgrade "git+https://github.com/Jaymon/testdata#egg=testdata"
功能性
这是在 testdata模块,还有其他函数(比如get_birthday) 没有在这里列出,完整的列表请看 source。
补丁
修补模块和类
patch(mod,**patches)
用给定的修补程序修补模块、实例或类。
假设您有一个这样的模块:
# module foo.bardefboom():return1classFooPatch(object):@classmethoddefbam(cls):returnboom()
现在您可以轻松地对其进行修补以进行测试:
defmock_boom():return2foo_bar=testdata.patch('foo.bar',boom=mock_boom)printfoo_bar.FooPatch.bam()# 2# but you can also just pass in objects or modulesfromfoo.barimportFooPatchFooPatch=testdata.patch(FooPatch,boom=mock_boom)printFooPatch.bam()# 2fromfooimportbarbar=testdata.patch(bar,boom=mock_boom)printbar.FooPatch.bam()# 2
修补类实例
您还可以修补特定实例
假设您有一个这样的模块:
# module foo.barclassFoo(object):defboom(self):return1
现在您可以轻松地对其进行修补以进行测试:
defmock_boom():return2foo=Foo()foo_patched=testdata.patch(foo,boom=mock_boom)printfoo_patched.boom()# 2# be aware though, the original instance was modified, foo_patched == fooprintfoo.boom()# 2
运行
在命令行上运行命令
r=testdata.run("echo 1")print(r)# 1
获取
请求URL
r=testdata.fetch("http://example.com")print(r.code)# 200print(r.body)# the html body of example.com
捕捉
输出缓冲,当您想确保日志记录或打印时很方便 陈述是在做你认为应该做的事情。
withtestdata.capture()asc:print("foo")if"foo"inc:print("foo was captured")
螺纹
python内置类的包装器 将错误冒泡到主线程,因为默认情况下,python的 线程类抑制错误,这使得在使用 测试用螺纹。
defrun():raiseValueError("join_2")thread=testdata.Thread(target=run)thread.start()print(thread.exception)
文件服务器
有时需要测试获取远程文件
importrequestsserver=testdata.create_fileserver({"foo.txt":["foo"],"bar.txt":["bar"],})withserver:# the with handles starting and stopping the serverres=testdata.fetch(server.url("foo.txt"))print(res.body)# foo
环境
如果不传入,请使用此上下文管理器更改环境 对象作为第一个值,它将默认为os.environ
withtestdata.enviroment(FOO=1):print(os.environ["FOO"])# 1print(os.environ["FOO"])# raises KeyError# you can also modify objects:d={}withtestdata.enviroment(d,FOO=1):print(d["FOO"])# 1print(d["FOO"])# raises KeyError
创建目录
create_dir(path,tmpdir="")
创建目录层次结构
base_dir="/tmp"d=testdata.create_dir("/foo/bar",base_dir)printd# /tmp/foo/bar
创建文件
create_file(path,contents="",tmpdir="",encoding="")
创建包含内容的文件
base_dir="/tmp"f=testdata.create_file("/foo/bar.txt","The file contents",base_dir)printf# /tmp/foo/bar.txt
创建文件
create_files(file_dict,tmpdir="",encoding="")
创建一大堆文件,file_dict键是文件名, 值是文件的内容。file_dict非常相似 到create_modulesparammodule_dict
file_dict={"foo/bar.txt":"the foo file contents","baz.txt":"the baz file contents",}f=testdata.create_files(file_dict)
获取文件
get_file(path="",tmpdir="")
这将返回一个Filepath实例,您可以对其进行操作,但是 与create_file不同的是,它实际上不会创建文件,只需给您 可以创建的文件的路径。
创建模块
create_module(module_name,contents="",tmpdir="",make_importable=True)
使用可导入的python内容创建模块
base_dir="/tmp"f=testdata.create_module("foo.bar","class Che(object): pass",base_dir)printf# /tmp/foo/bar.py
创建模块
create_modules(module_dict,tmpdir="",make_importable=True)
一次创建一大堆模块
f=testdata.create_modules({"foo.bar":"class Che(object): pass","foo.bar.baz":"class Boom(object): pass","foo.che":"class Bam(object): pass",})
获取ascii
get_ascii(str_size=0)
返回一个ascii字符字符串
>>> testdata.get_ascii() u'IFUKzVAauqgyRY6OV'
获得MD5
get_md5(val="")
如果val为空,则返回val的md5散列(如果传入)或随机val
>>> testdata.get_md5() 'e165765400b30772f1d9b3975ce77320'
获取hash
get_hash(str_size=32)
返回随机散列
>>> testdata.get_hash() "jYw3HseUl8GLoMc8QejLYFogC2lUYoUs"
获取布尔
get_bool()
返回布尔值(可以是true或false)
>>> testdata.get_bool() False >>> testdata.get_bool() True
### get_float |
^{tt10}$ |
return a floating point number between ^{tt11}$ and ^{tt12}$. |
^{pr 34}$ |
获取积分
get_int(min_size=1,max_size=sys.maxsize)
返回一个介于min_size和max_size之间的整数。
>>> testdata.get_int() 3820706953806377295
获取名称
get_name(name_count=2,as_str=True)
返回一个可以超出ascii范围的随机名称(例如,name can 采用Unicode编码)
>>> testdata.get_name() u'jamel clarke-cabrera'
获取电子邮件
get_email(name=u'')
返回ascii范围内的随机电子邮件地址。
>>> testdata.get_email() u'shelley@gmail.com'
获取str
get_str(str_size=0,chars=None)
返回已运行dom字符,可以是unicode。
>>> testdata.get_str() "q\x0bwZ\u79755\ud077\u027aYm\ud0d8JK\x07\U0010df418tx\x16"
获取URL
get_url()
返回一个随机的url。
>>> testdata.get_url() u'https://sK6rxrCa626TkQddTyf.com'
获取单词
get_words(word_count=0,as_str=True)
返回任意数量的单词,可以是unicode。
>>> testdata.get_words() "\u043f\u043e\u043d\u044f\u0442\u044c \u043c\u043e\u0436\u043d\u043e felis, habitasse ultrices Nam \u0436\u0435\u043d\u0430"
获取“超过”日期时间
get_past_datetime([now])
返回从now
>>> testdata.get_past_datetime() datetime.datetime(2000, 4, 2, 13, 40, 11, 133351)
获取未来日期时间
get_future_datetime([now])
从now
>>> testdata.get_future_datetime() datetime.datetime(2017, 8, 3, 15, 54, 58, 670249)
在日期时间
之间获取get_between_datetime(start[,stop])
返回从start到 来自stop
的过去>>> start = datetime.datetime.utcnow() - datetime.timedelta(days=100) >>> testdata.get_between_datetime(start) datetime.datetime(2017, 8, 3, 15, 54, 58, 670249)
测试
在大多数系统上使用2.7进行测试:
$ python -m unittest testdata_test
在MacOS上进行3.5版测试:
$ python3.5 -m unittest testdata_test