一个支持用嵌套上下文编写描述/it风格单元测试的nose插件
describe_it的Python项目详细描述
描述它是一个nose插件,允许您编写更多的单元测试 类似于一个组件应该如何工作而不仅仅是测试的可执行示例。
另一个好处是能够描述组件在特定情况下的行为 上下文,其中一个上下文可以通过使用 筑巢。
安装
$ pip install describe-it
写入
以“spec”结尾的任何模块都被视为包含规范/测试 为了描述它。
$ vim myfirst_spec.py
…内容:
fromgameimportGamefromdescribe_itimportdescribe,it,before_each,Fixture@describe# This declares a test context.defa_game():f=Fixture()# Fixture is a hack to get around# Python's implementation of closures.# You can use other methods, such as# nonlocal if you like.@before_each# Will be called before each 'it'defsetup():f.game=Game()@after_each# Will be called after each 'it'defteardown():perform_post_test_cleanup_if_needed()# This should rarely be needed!@it# This marks a test method.defis_player_ones_turn():assertf.game.current_player==1# describe_it doesn't come with an# assertion lib. Pick any one you like.@describe# This is a nested context thatdefin_second_round():# augments the context above.@before_each# Before each 'it' method, anydefsetup():# before_each in outer contexts willf.game.play_round()# be called first. Then this method# will be called.@itdefis_player_twos_turn():assertf.game.current_player==2@xit# You can skip individual test methodsdefskips_tests():# by using '@xit' or '@it_skip'assertTrue@with_data([1,2,3],# You can parameterize tests with[3,4,7])# different combinations of inputsdefadds_numbers(term_1,term_2,expected):assertterm_1+term_2==expected@xdescribe# You can skip whole contexts bydefthis_context_is_marked_as_skipped():# using '@xdescribe' or '@describe_skip'@itdefthis_test_will_be_skipped():assertTrue
运行
$ nosetests --with-describe-it