Sublime Forum

Unit Testing Plugins?

#1

New user here. Just wrote my first plugin (fun API!); but I’m having an issue writing tests for the plugin. It could be a simple Python issue, too (I’m relatively new to Python, as well). This is the error:

ImportError: No module named sublime

Here’s the distilled unit test, which resides in my-plugin/tests:

import unittest
import json
from my_plugin import MyCommand

class MyUnitTest(unittest.TestCase):
	
	def test_read_json_data(self):
       ...

Very abbreviated plugin:

import sublime_plugin
import sublime

class MyCommand(sublime_plugin.TextCommand):
    def run(edit):
    ...

It fails on the ‘import sublime’ statement above when I try to instantiate ‘MyCommand’. In other words, the test code doesn’t execute beyond the import statements.

So, where is the sublime module such that I can add it to my path? Or maybe there’s a different (better?) approach to writing tests?

Any help is much appreciated!

-bill

1 Like

#2

This might help for making your plugin: http://sublimetext.info/docs/en/reference/plugins.html

Why I don’t know much about unit testing, I might be about to help with the plugin. Could I ask what you’re trying to do?

1 Like

#3

Thanks. I’ve built a working plugin with a handful of commands, keyboard shortcuts, etc; but I can’t figure out how to write even basic unit tests for it, which is how I’m used to building software (ala TDD).

I’m building unit test runner plugins.

-bill

1 Like

#4

Are you trying to run the unit test with your system’s python? The sublime module doesn’t exist outside of the application, so you need to run your python code with the built in interpreter.

1 Like

#5

Ah, now it’s starting to make sense–thanks! I’m using nose as the runner, which uses my system’s python. So, the built-in interpreter is lib/python26 ?

-bill

1 Like

#6

I’ve found an approach that seems to work acceptably for testing plugins:

github.com/SublimeText/VintageE … ster/tests

There’s more testing-related code in here:

github.com/SublimeText/VintageE … _runner.py

It still needs some work, though.

1 Like

#7

Thanks, guillermoo. That looks interesting. Stupid question: how do you “run” the tests?

bill

1 Like

#8

You need to run them manually by running “vintage_ex_test_runner_commander” (which needs a better name :). I might have restricted some of the necessary commands to run only if the current directory is pointing to the “VintageEx” package. I did this so I wouldn’t be able to accidentally run the tests when I’m not developing “VintageEx”. I put all of this together quite quickly without too much thinking, so I’ll have to make some changes to the code.

1 Like

#9

Thanks for telling about the Unit Test Plugins.

1 Like

#10

This is an ancient thread, so the advice in it may be outdated.

The UnitTesting package provides a full-featured unittest runner. There’s no need to do this manually anymore.

1 Like