Ⅰ. 插件描述
A Django aware way of running python tests from within VimⅡ. 基本信息
|
Ⅲ. 安装方法
使用Vundle管理器安装
在你的.vimrc下添加:Plugin 'jarrodctaylor/vim-python-test-runner'
… 然后在Vim中运行以下命令:
:source %
:PluginInstall
对于Vundle版本 < 0.10.2,请用上面的Bundle替换Plugin。
使用NeoBundle管理器安装
在你的.vimrc下添加:NeoBundle 'jarrodctaylor/vim-python-test-runner'
… 然后在Vim中运行以下命令:
:source %
:NeoBundleInstall
使用VimPlug管理器安装
在你的.vimrc下添加:Plug 'jarrodctaylor/vim-python-test-runner'
… 然后在Vim中运行以下命令:
:source %
:PlugInstall
使用Pathogen管理器安装
在终端中运行以下命令:cd ~/.vim/bundle
git clone https://github.com/jarrodctaylor/vim-python-test-runner
Ⅳ. 文档说明
# vim-python-test-runner.vim
A simple way of running tests for your python files from within VIM.
This plugin was created to allow running Django tests with django-nose that
require database interaction from within Vim. Thus avoiding the need to toggle
between your vim session and the shell for longer periods of time. It will
also run your regular python unit tests with nosetests as well when not
working on a Django project.
Demo

Installation
Use your plugin manager of choice.
git clone https://github.com/JarrodCTaylor/vim-python-test-runner ~/.vim/bundle/vim-python-test-runner
- Add
Plugin 'JarrodCTaylor/vim-python-test-runner'to .vimrc - Run
:PluginInstall
- Add
- Add
NeoBundle 'https://github.com/JarrodCTaylor/vim-python-test-runner'to .vimrc - Run
:NeoBundleInstall
- Add
- Add
Plug 'https://github.com/JarrodCTaylor/vim-python-test-runner'to .vimrc - Run
:PlugInstall
- Add
Requirements
You need a VIM version that was compiled with python support, which is typical
for most distributions on Linux/Mac. You can check this by runningvim --version | grep +python
if you get a hit you are in business.
Tests are ran with either django-nose or nosetest so these will need to be
pip installed in order for the plugin to function properly.
Usage
The plugin provides nine commands:
DjangoTestApp: Run all tests for the current appDjangoTestFile: Run all tests in the current fileDjangoTestClass: Run all tests in the current classDjangoTestMethod: Run test for the current methodNosetestFile: Run all tests for the current fileNosetestClass: Run all tests in the current classNosetestMethod: Run the current test method (inside of a class)NosetestBaseMethod: Run the current test method (outside of a class)RerunLastTests: Rerun the last tests
All arguments can be tab-completed. Ensure that your cursor is within a
file, class or method as appropriate for the command being called.
For ease of usage you can map the above actions to a shortcut. For example,
if you wanted leader mappings you could set something like the following in
your vimrc:
nnoremap <Leader>da :DjangoTestApp<CR>
nnoremap <Leader>df :DjangoTestFile<CR>
nnoremap <Leader>dc :DjangoTestClass<CR>
nnoremap <Leader>dm :DjangoTestMethod<CR>
nnoremap <Leader>nf :NosetestFile<CR>
nnoremap <Leader>nc :NosetestClass<CR>
nnoremap <Leader>nm :NosetestMethod<CR>
nnoremap <Leader>nb :NosetestBaseMethod<CR>
nnoremap <Leader>rr :RerunLastTests<CR>Quickfix Results
Your tests results will be available in the quickfix window after they finish
running and you return to your Vim buffer. Open quickfix with :copen and
you can jump to failing tests by placing your cursor on the desired test and
pressing enter.
Required Configuration File for Django Tests
To make use of the plugin for Django projects you will need to create a small
config file named .vim-django in the root of your project that defines some
information about the apps you would like to run tests for. Assuming a basic
folder structure the config file would be saved in the following location.
── Project Root
├── .vim-django
├── manage.py
├── app1
│ └── tests
│ └── testsa1.py
└── app2
└── tests
├── testsb1.py
└── testsb2.pyConfig file contents
Required fields
The only required field is a list of the app names that you will be running
tests for."app_name": "app1, app2"
Optional fields
Optional fields that can be set in the vim-django config file are as follows:
environment: If you have modified your manage.py file to accept an environment argument
then you may use the environment flag to specify which one to run tests for.
Example"environment": "dev"If you haven't modified your manage.py file
then you don't need to use this.flags: An array of flags that you would like passed to the test runner
See example config file below for usage.test-runner: Set if you are using the default Django test runner
Example"test-runner": "django"
vim-django config file example
Using the example project above we would set the app name to "app1, app2"
The environment field is optional. We are also saying that we want the test to use
the fail fast option.
{"app_name": "app1, app2",
"environment": "OptionalNameOfEnv",
"flags": ["failfast"]}NOTE be sure to use double quotes in the config file as it is parsed as json
vim-django config file example for namespace packages
Namespace packages are supported, but require slightly different placement of
the .vim-django config file. Rather than placing it in the project root place
it in the lowest level package. For example, given a namespace package calledorganization.department.team the .vim-django file would be inside of theteam directory.
── Project Root
├── manage.py
└── organization
└── department
└── team
└── .vim-django
└── testsa1.py
└── tests
├── testsb1.py
└── testsb2.pyThe .vim-django file will contain only the top level package name, in this caseorganization, for example:
{"app_name": "organization"}Outside of Django
Nothing other than nose is required to use this plugin for tests that are
outside of a Django application.