sublime文本插件测试框架
sublime_plugin_tests的Python项目详细描述
折旧通知
我们决定不赞成sublime-plugin-tests,而赞成randy3k/UnitTesting。
它有更大的平台支持,并且为本地开发设计的脆弱性更小。
https://github.com/randy3k/UnitTesting
开始
使用pip install sublime_plugin_tests
安装模块然后,编写测试:
# Load in test frameworkfromsublime_plugin_testsimportframework# Define a TestCaseclassTestLeftDelete(framework.TestCase):deftest_left_delete_single(self):# Each test function *must* return Python with a `run` function# `run` will be run inside Sublime Text. Perform your assertions etc there.return""" # Use ScratchView utility provided by `sublime_plugin_tests` try: from utils.scratch_view import ScratchView except ImportError: from .utils.scratch_view import ScratchView def run(): # Generate new scratch file scratch_view = ScratchView() try: # Update the content and selection `ab|c` scratch_view.set_content('abc') scratch_view.set_sel([(2, 2)]) # Delete one character to the left `a|c scratch_view.run_command('left_delete') # Assert the current content assert scratch_view.get_content() == 'ac' finally: # No matter what happens, close the view scratch_view.destroy() """
$ # Run tests via nosetests $ nosetests . ---------------------------------------------------------------------- Ran 1test in 0.076s OK
若要对Travis CI中的升华文本2/3运行测试,请将其放入.travis.yml:
language:pythonpython:-"2.7"env:-SUBLIME_TEXT_VERSION="2"-SUBLIME_TEXT_VERSION="3"install:# Install Sublime Text and output version-curl https://raw.githubusercontent.com/twolfson/sublime-installer/0.1.3/install.sh | sh -s $SUBLIME_TEXT_VERSION-subl --version# Install dev dependencies-pip install sublime-plugin-tests# Install our plugin-mkdir -p '~/.config/sublime-text-'$SUBLIME_TEXT_VERSION'/Packages/'-ln -s $PWD '~/.config/sublime-text-'$SUBLIME_TEXT_VERSION'/Packages/YOUR_PLUGIN_NAME'before_script:# Generate a screen buffer to collect Sublime Text window-export DISPLAY=:99.0-sh -e /etc/init.d/xvfb start# Ensure the scripts self-terminate-export SUBLIME_AUTO_KILL=TRUEscript:# Run our tests-nosetests --nocapture --verbose --stop
文件
sublime-plugin-tests由两部分组成:测试框架代码(升华文本外部)和测试帮助程序(升华文本内部)。
测试框架代码在正常的开发环境中运行(例如,nosetestslives所在的环境)。测试助手生活在崇高的文本,使您的测试生活更容易。
测试框架
testcase
TestCase扩展Python’s unittest.TestCase。测试可以被跳过,像平常一样设置/拆除。
每个测试用例都应该返回test_str,一个在Sublime Text上下文中运行的String。此外,它还可以访问测试助手。
test_str必须有一个run函数,这样我们才能钩住它。
classTestLeftDelete(TestCase):deftest_left_delete_single(self):return""" import sublime def run(): # I am run inside of Sublime Text assert sublime.active_window().active_view() """
测试助手
实用程序。拆分选择
sublime_plugin_tests.utils.split_selection通过选择标记将字符串分成content和selection。
split_selection(input)""" @param {String} input Python to parse selection indicators out of @returns {Dictionary} ret_obj Container for selection and content @return {List} ret_obj['selection'] List of tuples for start/end position of selections @return {String} ret_obj['content'] Python with selection characters removed """
示例
输入:
split_selection(""" def abc|(): pas|s """)
输出:
{'content':""" def abc(): pass """,'selection':[(7,7),(18,18)]}
实用程序。草稿视图。草稿视图
utils.scratch_view.ScratchView是用于创建要处理的临时视图的类。这意味着要在崇高的文本中运行,而不是在框架中运行。
初始化后,Sublime Text将在活动窗口中打开一个新文件(未保存到本地磁盘)。完成后,强烈建议运行ScratchView#destroy来清理Sublime文本窗口。
# Open temporary file inside of Sublime Text's active windowtmp_view=ScratchView()
scratchview“运行”命令
在ScratchView上下文中运行命令。函数签名与Sublime Text documentation中的相同。
# Run `left_delete` command inside of `tmp_view`tmp_view.run_command('left_delete')
scratchview设置内容、获取内容、清除内容
方法来调整ScratchView的内容。
# `set_content` replaces all of the content.tmp_view.set_content('Hello World!')# `get_content` returns the current content.tmp_view.get_content()# 'Hello World!'# `clear_content` deletes all of the content.tmp_view.clear_content()
抓取视图设置选择获取选择清除选择
方法调整ScratchView的选择。
# `set_sel` replaces the selection.# For convenience, tuples and lists are coerced to `sublime.Region`.tmp_view.set_sel([(6,6),(7,7)])# `get_sel` returns the current selection.tmp_view.get_sel()# RegionSet([Region(6, 6), Region(7, 7)])# `clear_sel` deselects everything.tmp_view.clear_sel()
scratchview销毁
关闭Scratch视图进行清理。这也保证在关闭时不会运行弹出窗口。
# Close `tmp_view`tmp_view.destroy()
架构
框架接受每个测试函数,将其包装在测试工具中,运行它,并断言工具是否看到错误。
测试工具生成一个临时的sublime文本插件,它在sublime的上下文中运行您的测试。这个工具是通过对sublime文本的cli调用启动的。
每一个测试函数的输出和断言都被返回到一个打印输出,并输出到。贡献
代替正式的形式指南,注意保持现有的编码风格。为任何新的或更改的功能添加单元测试。通过./test.sh进行测试。
如果您希望无头运行测试,则此存储库可以与Vagrant一起使用。
Currently, it is only configured for Sublime Text 3.
$ vagrant up [default] Importing base box 'precise64'... ... $ vagrant ssh vagrant@precise64:~$ cd /vagrant vagrant@precise64:/vagrant$ ./test.sh ... ---------------------------------------------------------------------- Ran 3 tests in 2.651s OK