Ⅰ. 插件描述
Unit Testing plugin for VimⅡ. 基本信息
|
Ⅲ. 安装方法
使用Vundle管理器安装
在你的.vimrc下添加:Plugin 'luchermitte/vim-ut'
… 然后在Vim中运行以下命令:
:source %
:PluginInstall
对于Vundle版本 < 0.10.2,请用上面的Bundle替换Plugin。
使用NeoBundle管理器安装
在你的.vimrc下添加:NeoBundle 'luchermitte/vim-ut'
… 然后在Vim中运行以下命令:
:source %
:NeoBundleInstall
使用VimPlug管理器安装
在你的.vimrc下添加:Plug 'luchermitte/vim-ut'
… 然后在Vim中运行以下命令:
:source %
:PlugInstall
使用Pathogen管理器安装
在终端中运行以下命令:cd ~/.vim/bundle
git clone https://github.com/luchermitte/vim-ut
Ⅳ. 文档说明
Introduction
UT is another Test Unit Framework for Vim, which main particularity is to fill the |quickfix| window with the assertion failures.
Features
- Assertion failures are reported in the quickfix window
- Assertion syntax is simple, check Tom Link's suite, it's the same
- Supports banged
:Assert!to stop processing a given test on failed assertions - All the
s:Test*functions of a suite are executed (almost) independently (i.e., a critical:Assert!failure will stop the Test of the function, andlh#UTwill proceed to the nexts:Testfunction - Lightweight and simple to use: there is only one command defined, all the other definitions are kept in an autoload plugin.
- A suite == a file
- Several
s:TestXxx()per suite - +optional
s:Setup(),s:Teardown() - Supports
:Comments s:LocalFunctions(),s:variables, andl:variablesare supported- Takes advantage of BuildToolsWrapper's
:COpencommand if installed - Count successful tests and failed assertions
- Short-cuts to run the Unit Tests associated to a given vim script; Relies on: Let-Modeline/local_vimrc/Project to set
g:UTfiles(space separated list of glob-able paths), and onlh-vim-lib#path - Command to exclude, or specify the tests to play =>
:UTPlay,UTIgnore - Helper scripts are provided to help integration
with vimrunner+rspec. See examples of use in
lh-vim-lib and
lh-brackets. - Callstack is decoded and expanded in the quickfix window on uncaught
exceptions.
Usage
- Create a new vim script, it will be a Unit Testing Suite.
- One of the first lines must contain
UTSuite Some intelligible name for the suite- Then you are free to directly assert anything you wish as long as it is a valid vim |expression|, e.g.
Assert 1 > 2
Assert 1 > 0
Assert s:foo > s:Bar(g:var + 28) / strlen("foobar")
AssertTxt! (s:foo > s:Bar(g:var+28),
\, s:foo." isn't bigger than s:Bar(".g:var."+28)")
AssertEquals('a', 'a')
AssertDiffers('a', 'b')
let dict = {}
AssertIs(dict, dict)
AssertMatch('abc', 'a')
AssertRelation(1, '<', 2)
AssertThrows 0 + [0]- or to define as many independent tests as you wish. A test is a function with a name starting with
s:Test. Even if a test critically fails, the next test will be executed, e.g.
function s:Test1()
let var = SomeFunction()
Assert! type(var) == type(0)
Assert var < 42
Assert! var > 0
" Some other code that won't be executed if the previous assertion failed
" /*the wiki does not recognizes vim comments*/
let i = var / 42.0
Comment This comment may never be displayed if {var} is negative or not a number
endfunction
function s:Test2()
Assert s:what != Ever()
endfunction- If you wish to see a set-up function executed before each test, define the
s:Setup()function. - If you wish to see a clean-up function executed after each test, define the
s:Teardown()function. - Now run
:UTRunof your test script (filename), and ... debug your failed assertions.
Examples
See:
- tests/lh/UT.vim for a classical test,
- tests/lh/UT-fixtures.vim for a test with fixtures.
To Do
- Add
&efmfor VimL errors like the one produced by:Assert 0 + [0] - Check UT works fine under windows (where paths have spaces, etc.), and on UTF-8 files
- Simplify
s:errorsfunctions - Merge with Tom Link's tAssert plugin? (the UI is quite different)
- Support Embedded comments like for instance:
Assert 1 == 1 " 1 must value 1- Ways to test buffers produced
- Always execute s:Teardown() -- move its call to a :finally bloc
- Find a way to prevent the potential script scope pollution
Design Choices
- The assertions supported by this plugin are expected to be made in a Unit Testing file, they are not to be used in regular VimL scripts as a Design by Contract tool. Check my DbC framework in lh-vim-lib, or even Thomas Link's plugin, it is much more suited for that kind of assertions.
- In order to be able to produce the quickfix entries, the plugin first parses the Unit Test file to complete all
:Assertoccurrences with extra information about the line number where the assertion is made.
Installation
- Requirements: Vim 7.+, lh-vim-lib v4.0.0
- With vim-addon-manager, install vim-UT (this is the preferred method because of the dependencies)
ActivateAddons UT- or with vim-flavor, which also
handles dependencies
flavor 'LucHermitte/vim-UT'- or you can clone the git repositories
git clone git@github.com:LucHermitte/lh-vim-lib.git
git clone git@github.com:LucHermitte/vim-UT.git- or with Vundle/NeoBundle:
Bundle 'LucHermitte/lh-vim-lib'
Bundle 'LucHermitte/vim-UT'Other Tests related plugins for Vim
- Tom Link's tAssert plugin, and spec_vim plugin,
- Staale Flock's vimUnit plugin,
- Meikel Brandmeyer's vimTAP plugin,
- Ben Fritz's vim-2html-test plugin,
- Ingo Karkat's runVimTests plugin,
- See also Paul Mucur article's: Testing Vim Plugins on Travis CI with RSpec and Vimrunner,
- Andrew Radev's vimrunner,
- Kana's vim-spec
