-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SystemError: Parent module '' not loaded, cannot perform relative import #67
Comments
Changing the initial import structure from: from .text_extraction_unit_tests import PrefixStrippingViewUnitTests
wrap_plus_module = sys.modules["Wrap Plus.wrap_plus"]
def run_unit_tests(unit_tests_to_run=[]):
runner = unittest.TextTestRunner()
classes = \
[
PrefixStrippingViewUnitTests,
SemanticLineWrapUnitTests,
LineBalancingUnitTests,
]
... to: wrap_plus_module = sys.modules["Wrap Plus.wrap_plus"]
def run_unit_tests(unit_tests_to_run=[]):
runner = unittest.TextTestRunner()
classes = \
[
sys.modules["Wrap Plus.tests.text_extraction_unit_tests"].PrefixStrippingViewUnitTests,
SemanticLineWrapUnitTests,
LineBalancingUnitTests,
]
... Seems to solved the problem. Now all the Unit Tests ran with success:
|
SystemError: Parent module '' not loaded, cannot perform relative import https://github.com/randy3k/UnitTesting/issues/67
issue: SystemError: Parent module '' not loaded, cannot perform relative import https://github.com/randy3k/UnitTesting/issues/67
I am not sure why, but I tried this fix on another package, and it did not worked. Then I searched and found this other solution by changing this: class ClearCursorsCaretsLastSelectionUnitTests(sys.modules["ClearCursorsCarets.tests.utilities"].BasicSublimeTextViewTestCase): To this: import os
import sys
import importlib
CURRENT_DIRECTORY = os.path.dirname( os.path.dirname( os.path.realpath( __file__ ) ) )
CURRENT_PACKAGE_NAME = os.path.basename( CURRENT_DIRECTORY ).rsplit('.', 1)[0]
utilities = importlib.import_module( CURRENT_PACKAGE_NAME + ".tests.utilities" )
class ClearCursorsCaretsLastSelectionUnitTests(utilities.BasicSublimeTextViewTestCase):
def test_1_selections_at_first_word(self):
pass |
I am not sure what you want to achieve. You need to make sure that the tests are organized such as they could be discovered by |
They are being discovered, the error was the importing of helper functions/classes from helper files. I had created an $ tree
.
├── clear_cursors_carets.py
├── tests
│ ├── __init__.py
│ ├── clear_cursors_carets_first_selection_unit_tests.py
│ ├── clear_cursors_carets_last_selection_unit_tests.py
│ ├── unit_tests_runner.py
│ └── utilities.py
└── unittesting.json On the file
import unittest
import textwrap
import sublime
import sublime_plugin
def wrap_text(text):
return textwrap.dedent( text ).strip( " " ).strip( "\n" )
class BasicSublimeTextViewTestCase(unittest.TestCase):
@classmethod
def setUp(self):
self.maxDiff = None
# Create a new Sublime Text view to perform the Unit Tests
self.view = sublime.active_window().new_file()
self.view.set_syntax_file( "Packages/Text/Plain text.tmLanguage" )
# make sure we have a window to work with
settings = sublime.load_settings("Preferences.sublime-settings")
settings.set("close_windows_when_empty", False)
def tearDown(self):
if self.view:
self.view.set_scratch(True)
self.view.window().focus_view(self.view)
self.view.window().run_command("close_file")
def setText(self, string, start_point=0):
self.view.run_command("append", {"characters": wrap_text( string ) })
selections = self.view.sel()
selections.clear()
selections.add( sublime.Region( start_point, start_point ) )
def create_test_text(self, start_point):
"""
1. (0, 4),
2. (5, 9),
3. (10, 14),
4. (16, 20),
5. (21, 25),
6. (26, 30),
"""
self.setText( """\
word
word
word
word
word
word""", start_point ) Then on my test files
import sublime
import sublime_plugin
import os
import sys
import importlib
CURRENT_DIRECTORY = os.path.dirname( os.path.dirname( os.path.realpath( __file__ ) ) )
CURRENT_PACKAGE_NAME = os.path.basename( CURRENT_DIRECTORY ).rsplit('.', 1)[0]
utilities = importlib.import_module( CURRENT_PACKAGE_NAME + ".tests.utilities" )
class ClearCursorsCaretsFirstSelectionUnitTests(utilities.BasicSublimeTextViewTestCase):
def test_1_selections_at_first_word(self):
self.create_test_text(0)
self.view.window().run_command( "find_under_expand" )
self.view.window().run_command( "single_selection_first" )
region = self.view.sel()[0]
self.assertEqual( sublime.Region(0, 4), region )
def test_2_selections_at_first_word(self):
self.create_test_text(0)
self.view.window().run_command( "find_under_expand" )
self.view.window().run_command( "find_under_expand" )
self.view.window().run_command( "single_selection_first" )
region = self.view.sel()[0]
self.assertEqual( sublime.Region(0, 4), region )
def test_3_selections_at_first_word(self):
self.create_test_text(0)
self.view.window().run_command( "find_under_expand" )
self.view.window().run_command( "find_under_expand" )
self.view.window().run_command( "find_under_expand" )
self.view.window().run_command( "single_selection_first" )
region = self.view.sel()[0]
self.assertEqual( sublime.Region(0, 4), region ) This was how I made it work. On the my first attempt, I was just trying to import from .utilities import BasicSublimeTextViewTestCase But this was failing with:
When I called the But it was not falling when I used my own runner I already have setup. In addition to the
import sys
import unittest
def run_unit_tests(unit_tests_to_run=[]):
runner = unittest.TextTestRunner()
classes = \
[
sys.modules["ClearCursorsCarets.tests.clear_cursors_carets_first_selection_unit_tests"].ClearCursorsCaretsFirstSelectionUnitTests,
sys.modules["ClearCursorsCarets.tests.clear_cursors_carets_last_selection_unit_tests"].ClearCursorsCaretsLastSelectionUnitTests,
]
if len( unit_tests_to_run ) < 1:
# Comment all the tests names on this list, to run all Unit Tests
unit_tests_to_run = \
[
# "test_semantic_line_wrap_line_starting_with_comment",
]
runner.run( suite( classes, unit_tests_to_run ) )
def suite(classes, unit_tests_to_run):
"""
Problem with sys.argv[1] when unittest module is in a script
https://stackoverflow.com/questions/2812218/problem-with-sys-argv1-when-unittest-module-is-in-a-script
Is there a way to loop through and execute all of the functions in a Python class?
https://stackoverflow.com/questions/2597827/is-there-a-way-to-loop-through-and-execute-all-of-the-functions
looping over all member variables of a class in python
https://stackoverflow.com/questions/1398022/looping-over-all-member-variables-of-a-class-in-python
"""
suite = unittest.TestSuite()
unit_tests_to_run_count = len( unit_tests_to_run )
for _class in classes:
_object = _class()
for function_name in dir( _object ):
if function_name.lower().startswith( "test" ):
if unit_tests_to_run_count > 0 \
and function_name not in unit_tests_to_run:
continue
suite.addTest( _class( function_name ) )
return suite Then I can call this runner from my main application using this: def run_tests():
"""
How do I unload (reload) a Python module?
https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module
"""
print( "\n\n" )
sublime_plugin.reload_plugin( "ClearCursorsCarets.tests.unit_tests_runner" )
sublime_plugin.reload_plugin( "ClearCursorsCarets.tests.utilities" )
sublime_plugin.reload_plugin( "ClearCursorsCarets.tests.clear_cursors_carets_first_selection_unit_tests" )
sublime_plugin.reload_plugin( "ClearCursorsCarets.tests.clear_cursors_carets_last_selection_unit_tests" )
from .tests import unit_tests_runner
# Comment all the tests names on this list, to run all Unit Tests
unit_tests_to_run = \
[
# "test_2_selections_at_last_word",
# "test_last_selection_with_6_selections_plus_redundant_expand_at_last_word",
]
unit_tests_runner.run_unit_tests( unit_tests_to_run )
def plugin_loaded():
"""
Running single test from unittest.TestCase via command line
https://stackoverflow.com/questions/15971735/running-single-test-from-unittest-testcase-via-command-line
"""
pass
run_tests() On the beginning I was only using my runner to run Unit Tests, but @ehuss asked to use the UnitTesting package to run the tests on a pull request to his repository, then since then I started adding it as compatible with the runners I was already using. |
Are you sure that the test cases are discoverable? AKAIK, unittest discover only search files named as EDIT: I saw that you have changed the pattern for discovery. |
I checked out your master branch: https://github.com/evandrocoan/Sublime-Wrap-Plus/tree/master and UnitTesting was working properly.
EDIT: I just realized that you were talking about a different package. |
I checked out https://github.com/evandrocoan/ClearCursorsCarets
|
I see what's going on now. You were trying to use relative import You could move the files into a folder with |
But I am doing this already have then in a folder with However, If I do:
It does work: This is my
The differences I see from that example is the
And that the tests which does relative import are inside a folder called Am I required to move everything to another folder inside the tests folder with a |
Unittesting only imports directories under tests as modules, so you have to put it under |
I may look into the possibility to import Edit: it seems that it requires quite some work based on the |
issue: SystemError: Parent module '' not loaded, cannot perform relative import https://github.com/randy3k/UnitTesting/issues/67
issue: SystemError: Parent module '' not loaded, cannot perform relative import https://github.com/randy3k/UnitTesting/issues/67
SublimeText/UnitTesting#67 SystemError: Parent module '' not loaded, cannot perform relative import
SublimeText/UnitTesting#67 SystemError: Parent module '' not loaded, cannot perform relative import
Hopefully this can be fixed as part of #155. |
I am not sure how easy or how hard it would be done. It seems that |
Yeah, the default implementation of |
If we specify |
Could we just pass |
It would work only if there is a |
Hopefully the next update will handle it. |
I had imported another test file, in one (main) test file like this:
Using this I can run the unit tests by my loader file inside the file
Wrap Plus.wrap_plus
:And it successfully run I reload the
Wrap Plus.wrap_plus
file:But when I use the
UnitTesting
commandUnitTesting: Test Current Package
, I got this error:The text was updated successfully, but these errors were encountered: