Sublime Forum

How to run ST plugin unittests?

#1

Question about newterm usage.

I’d like to know how to run this test_runner but I don’t know how to execute it on the sublime text python interpreter.

I’ve used succesfully in the past UnitTesting to test stuff like this one but it doesn’t work (or i don’t know how to use it) with the above UnitTest mentioned code.

Thanks in advance

1 Like

#2

I use UnitTesting here and there and it works pretty, well. Just installed via Package Control, opened a file of a package-project to let UnitTesting know about the package path and called UnitTesting: Test Current Package ... from command panel.

That’s it.

It requires a unittesting.json in the root of a package, with some setups. Main part is the “test_dir” setting which points to the directory of the test files.

The rest is pretty much the same like py.test.

All test_ ... methods of all unittest.TestCase classes are executed.

Should look like this.

import unittest

from MyPackage.module import MyClass

class test_my_class(unittest.TestCase):

    def test_first_thing(self):
        obj = MyClass()
        self.assertFalse(obj.doSomething()
1 Like

#3

I didn’t know it was required having a file called unittesting.json in the root of the package. When I had tried the easyclang test suite i just assumed that UnitTesting was making its magic because that plugin had a folder called tests… So it’s good UnitTesting allows you to specify where the tests are placed.

That said, I’ve tried right now testing wbond’s newterm package so i’ve created this unittesting.json file in the root of the newterm directory such as:

{
    "tests_dir" : "dev",
    "pattern" : "test*.py",
    "async": true,
    "deferred": false,
    "verbosity": 5,
    "output": "<panel>",
    "capture_console": true
}

I’ve called UnitTesting: Test Current Package ... from the command pannel and then just used UnitTesting command. As a result I’ve got:

tests (unittest.loader.ModuleImportFailure) ... ERROR

======================================================================
ERROR: tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./python3.3/unittest/case.py", line 384, in _executeTestPart
  File "./python3.3/unittest/loader.py", line 32, in testFailure
ImportError: Failed to import test module: tests
Traceback (most recent call last):
  File "./python3.3/unittest/loader.py", line 261, in _find_tests
  File "./python3.3/unittest/loader.py", line 239, in _get_module_from_name
  File "D:\software\SublimeText 3_x64\Data\Packages\newterm\dev\tests.py", line 13, in <module>
    from .unittest_data import data, data_class
SystemError: Parent module '' not loaded, cannot perform relative import


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

UnitTesting: Done.

So, do you know how to test newterm’s package?

Btw, I wonder how @wbond tested newterm… there isn’t any unittesting.json file in that package so maybe he used another method to test it out.

0 Likes

#4

I am not sure whether these test cases were designed to be used by UnitTesting package.

Issue 1:

If you just cloned the repo into your Packages folder, you’ve got a newterm package. Therefore the import in line 11 does import that instead of the required newterm.all.newterm.

Fix: import newterm.all.newterm as newterm

Issue 2:

ST creates python packages, but they are not present while unittests. Therefore relative imports fail.

Fix: from newterm.dev.unittest_data import data, data_class

Issue 3:

With 1 and 2 fixed, tests start, but there is still an issue with the thread model being used, which I did not yet fully analyze.

0 Likes

#5

The tests for newterm were written to be used with https://packagecontrol.io/packages/Package%20Coverage. It allows consolidating test coverage tracking from different operating systems.

4 Likes