Sublime Forum

SublimePluginTesting package

#1

Get it from here: guillermooo@bitbucket.org/guill … gintesting

This is a very experimental package to enable users to test plugins easily.

Basically, it sets up an environment where you can use unittest to test your plugin’s code.

Currently it doesn’t do much, but here’s how it works:

  1. write your plugin. For example:

[code]import sublime, sublimeplugin

class SampleCommand(sublimeplugin.TextCommand):
def run(self, view, args):
sublime.statusMessage(args)

def aTest(self, view):
    return 0[/code]
  1. view.runCommand(“newTestFile”) (there are no keybindings/menus yet, sorry)
  2. write tests like so:

[code]import unittest
import sublimeunittest
#===============================================================================

Import your plugins and any Python lib available on your system.

Add your tests below this header.

Remember you’re using your system’s Python!

#===============================================================================

sample imports

import sublime
import echoes
import sublimeplugin

class ATestCase(unittest.TestCase):
def setUp(self):
self.v = sublime.View()

def testThisThing(self):
    echoes.SampleCommand().run(self.v, "hey")
    self.assertEquals("hey", sublime.MESSAGE_SINK"statusMessage"])

def testThisoOtherThing(self):
    self.assertEquals(0, echoes.SampleCommand().aTest(self.v))

if name == “main”:
unittest.main()[/code]

  1. $ /Packages//tests/test.py

About 0.00000000000001 % of ST functionality is implemented, so use it mainly to test functions that don’t use the ST API.

0 Likes

#2

Guillermooo,

How is this package working out? I have the ST community packages at GoogleCode setup for continuous integration testing upon any checkin of code. It would be great to setup some standard way of going package testing, so that I can wire it up in the CI build.

I would also like to add support to the package wiki for linking to and/or building bitbucket packages as well, btw.

Greg

0 Likes

#3

Hi Greg,

This package turned out to be a really bad idea. However, I’m now using the python mock library (by Michael Foord) to test plugins and is working like a charm. I’ll rewrite this package to use mock at some point.

0 Likes

#4

Cool. I will be much happier having mocks. So you plan on using the mocks for mocking out ST calls as well? That would rock!

Keep me posted. I will for sure add tests for my stuff once we have something.

0 Likes

#5

If I’m doing it the right way, this is trivial with mock:

bitbucket.org/guillermooo/sublim … s.py#cl-16

The only “problem” I’ve seen so far is that you need to create sublime.py and sublimeplugin.py along with your tests and stub out some classes there too. Check out the link above for an example.

0 Likes

#6

Cool, thanks for the info. I’ll have to get fluent with the Python mocking magic…

0 Likes

#7

UPDATED!

  • Now it uses Michael Foord’s Mock library.
  • Still a work in progress, but you can check out the tests under /tests to get an idea of how this works.
  • No keybinding/menu items are defined yet (and most likely won’t ever be), so you have to do view.runCommand(“newTestFile”) or define your own.
0 Likes