minitest的灵感来自ruby minispec。
minitest的Python项目详细描述
这个项目的灵感来自于ruby minispec,但是现在它只是实现了 一些方法包括:
must_equal, must_true, must_false, must_raise, must_output, only_test.
以及其他一些有用的功能:
p, pp, pl, ppl, length, size, inject, flag_test, p_format, pp_format, pl_format, ppl_format, capture_output.
github:https://github.com/jichen3000/minitest
PYPI:https://pypi.python.org/pypi/minitest
如何安装
pip install minitest
如何使用
举个简单的例子,你只要写一个名为x的函数,我就可以 喜欢将unittest写入与以下文件相同的文件:code:
if __name__ == '__main__': # import the minitest from minitest import * import operator # declare a variable for test tself = get_test_self() # you could put all your test variables on tself # just like declare your variables on setup. tself.jc = "jc" # declare a test with test(object.must_equal): tself.jc.must_equal('jc') None.must_equal(None) with test(object.must_true): True.must_true() False.must_true() with test(object.must_false): True.must_false() False.must_false() # using a funcation to test equal. with test("object.must_equal_with_func"): (1).must_equal(1, key=operator.eq) (1).must_equal(2, key=operator.eq) def div_zero(): 1/0 # test exception with test("test must_raise"): (lambda : div_zero()).must_raise(ZeroDivisionError) (lambda : div_zero()).must_raise(ZeroDivisionError, "integer division or modulo by zero") (lambda : div_zero()).must_raise(ZeroDivisionError, "in") # when assertion fails, it will show the failure_msg with test("with failure_msg"): the_number = 10 (the_number % 2).must_equal(1, failure_msg="{0} is the number".format(the_number)) # it wont show the failure_msg (the_number % 2).must_equal(0, failure_msg="{0} is the number".format(the_number)) (True).must_false( failure_msg="{0} is the number".format(the_number)) (lambda : div_zero()).must_raise(ZeroDivisionError, "in", failure_msg="{0} is the number".format(the_number)) def print_msg_twice(msg): print msg print msg return msg with test("capture_output"): with capture_output() as output: result = print_msg_twice("foobar") result.must_equal("foobar") output.must_equal(["foobar","foobar"]) with test("must output"): (lambda : print_msg_twice("foobar")).must_output( ["foobar","foobar"]) (lambda : print_msg_twice("foobar")).must_output( ["foobar","wrong"])
结果:
Running tests: .FFFF. Finished tests in 0.013165s. 1) Failure: File "/Users/Colin/work/minitest/test.py", line 29, in <module>: EXPECTED: True ACTUAL: False 2) Failure: File "/Users/Colin/work/minitest/test.py", line 32, in <module>: EXPECTED: False ACTUAL: True 3) Failure: File "/Users/Colin/work/minitest/test.py", line 38, in <module>: EXPECTED: 2 ACTUAL: 1 4) Failure: File "/Users/Colin/work/minitest/test.py", line 47, in <module>: EXPECTED: 'in' ACTUAL: 'integer division or modulo by zero' 5) Failure: File "/Users/colin/work/minitest/test.py", line 86, in <module>: MESSAGE: '10 is the number' EXPECTED: 1 ACTUAL: 0 6) Failure: File "/Users/colin/work/minitest/test.py", line 92, in <module>: MESSAGE: '10 is the number' EXPECTED: False ACTUAL: True 7) Failure: File "/Users/colin/work/minitest/test.py", line 95, in <module>: MESSAGE: '10 is the number' EXPECTED: 'in' ACTUAL: 'integer division or modulo by zero' 8) Failure: File "/Users/colin/work/minitest/test.py", line 102, in <module>: EXPECTED: ['foobar', 'wrong'] ACTUAL: ['foobar', 'foobar'] 12 tests, 22 assertions, 8 failures, 0 errors. [Finished in 0.1s]
仅限_测试功能
如果只想运行一些测试函数,则只能使用 指定它们的功能。注意,你必须把它放在测试的顶端 函数,就像下面的例子。代码:
def foo(): return "foo" def bar(): return "bar" if __name__ == '__main__': from minitest import * only_test("for only run", foo) with test("for only run"): (1).must_equal(1) (2).must_equal(2) pass with test("other"): (1).must_equal(1) (2).must_equal(2) pass with test(foo): foo().must_equal("foo") with test(bar): bar().must_equal("bar")
它将只为您运行test(“for only run”)和test(foo)。
其他有用功能
捕获输出,p,pp,pl,ppl,长度,大小,p_格式,pp_格式, pl_format,ppl_format这些函数可以被任何对象使用。
捕获输出,捕获标准输出。此函数将打印 变量名作为标题。代码:def print_msg_tween(msg):print msg 打印消息返回消息
with capture_output() as output: result = print_msg_twice("foobar") print result print output
打印结果:
"foobar" ["foobar","foobar"]
P,打印标题。此函数将变量名打印为 头衔。代码:
value = "Minitest" value.p() value.p("It is a value:") value.p(auto_get_title=False)
打印结果:
value : 'Minitest' It is a value: 'Minitest' 'Minitest'
pp,有标题的漂亮印刷品。此函数将变量名打印为 标题在第一行,然后漂亮地打印变量的内容 在标题下面。代码:
value = "Minitest" value.pp() value.pp("It is a value:") value.pp(auto_get_title=False)
打印结果:
value : 'Minitest' It is a value: 'Minitest' 'Minitest'
打印标题和代码位置。这个函数和pt一样,但是 将在第一行打印代码位置。一些编辑支持 转到该文件的行,例如sublime2。代码:
value = "Minitest" value.pl() value.pl("It is a value:") value.pl(auto_get_title=False)
打印结果:
File "/Users/Colin/work/minitest/test.py", line 76 value : 'Minitest' File "/Users/Colin/work/minitest/test.py", line 77 It is a value: 'Minitest' File "/Users/Colin/work/minitest/test.py", line 78 'Minitest'
PPL,漂亮的打印标题和代码位置。这个函数就像 但会在第一行打印代码位置。注意:会的 先打印空行。代码:
value = "Minitest" value.ppl() value.ppl("It is a value:") value.ppl(auto_get_title=False)
打印结果:
File "/Users/Colin/work/minitest/test.py", line 76 value : 'Minitest' File "/Users/Colin/work/minitest/test.py", line 77 It is a value: 'Minitest' File "/Users/Colin/work/minitest/test.py", line 78 'Minitest'
p_u格式,获取字符串,就像p函数打印一样。我把它用在 使用日志进行调试,例如:logging.debug(value.p_format())代码:
value = "Minitest" value.p_format()
返回结果:
value : 'Minitest'
pp_格式,获取字符串,就像pp函数打印一样。我把它用在 使用日志进行调试,例如:logging.debug(value.p p_format())代码:
value = "Minitest" value.pp_format()
返回结果:
value :\n'Minitest'
pl_format,获取字符串,就像pl函数打印一样。我把它用在 使用日志进行调试,例如:logging.debug(value.pl_format())代码:
value = "Minitest" value.pl_format()
返回结果:
line info: File "/Users/Colin/work/minitest/test.py", line 76, in <module>\nvalue : 'Minitest'
ppl_格式,获取字符串,就像ppl函数打印一样。我把它用在 使用日志进行调试,例如:logging.debug(value.ppl_format())代码:
value = "Minitest" value.ppl_format()
返回结果:
line info: File "/Users/Colin/work/minitest/test.py", line 76, in <module>\nvalue :\n'Minitest'
长度和大小将调用调用方对象的len函数。代码:
[1,2].length() # 2, just like len([1,2]) (1,2).size() # 2, just like len((1,2))
注入自定义的注入方法,否则注入函数将注入 自定义的功能。我为什么要做这个功能?因为在很多地方 如果我使用numpy数组。在比较两个numpy数组时, 我必须使用:
import numpy numpy.array([1]).must_equal(numpy.array([1.0]), numpy.allclose)
为了方便起见,我使用了自定义的注入方法或注入 功能如下:
import numpy inject(numpy.allclose, 'must_close') numpy.array([1]).must_close(numpy.array([1.0]))
flag_test将打印一条消息“此处有测试代码 地方!'和代码位置。代码:
flag_test() # add a title flag_test("for test")
打印结果:
File "/Users/colin/work/minitest/test.py", line 97, in <module>: There are test codes in this place! File "/Users/colin/work/minitest/test.py", line 101, in <module>: for test : There are test codes in this place!