-
Notifications
You must be signed in to change notification settings - Fork 31
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
Ability to run single test methods or classes #209
Comments
UnitTesting uses TestLoader.discover to discover tests. I beleive you might be also to switch to use |
Thanks for your response. Perhaps UnitTesting could accept a new I put this together: diff --git a/unittesting/package.py b/unittesting/package.py
index 6259772..cffda42 100644
--- a/unittesting/package.py
+++ b/unittesting/package.py
@@ -82,10 +82,14 @@ class UnitTestingCommand(sublime_plugin.ApplicationCommand, UnitTestingMixin):
# use custom loader which supports reloading modules
self.remove_test_modules(package, settings["tests_dir"])
loader = TestLoader(settings["deferred"])
- if os.path.exists(os.path.join(start_dir, "__init__.py")):
- tests = loader.discover(start_dir, settings["pattern"], top_level_dir=package_dir)
+ if "dot_pattern" in settings:
+ tests = loader.loadTestsFromName(settings["dot_pattern"])
else:
- tests = loader.discover(start_dir, settings["pattern"])
+ if os.path.exists(os.path.join(start_dir, "__init__.py")):
+ tests = loader.discover(start_dir, settings["pattern"], top_level_dir=package_dir)
+ else:
+ tests = loader.discover(start_dir, settings["pattern"])
+
# use deferred test runner or default test runner
if settings["deferred"]:
if settings["legacy_runner"]: With the above change I was able to run a single test method with the following build config: "build_systems": [
{
"name": "Sublime tests:",
"target": "unit_testing",
"package": "User",
"variants": [
{"name": "test_copy_pyobj_path", "dot_pattern": "User.tests.test_pypaths.TestCopyPathsCommand.test_copy_pyobj_path"},
]
}
] If you think this is a reasonable approach I can make a PR which also updates the README. |
I think the current setup only works for plugins under |
Mmm... unittest's dotted notation should represent the absolute path of the python object, so as long as "build_systems": [
{
"name": "Sublime tests:",
"target": "unit_testing",
"package": "User",
"variants": [
{"name": "single UnitTesting test", "dot_pattern": "UnitTesting.tests.test_await_worker.TestAwaitingWorkerInDeferredTestCase.test_await_worker"}
]
},
] Panel output:
Basically this is the same dotted notation you can also use via the
|
Thanks for the investigation. I will take a look soon. |
Thank you Randy. For the record, I wanted a way to quickly run a single test method while I am writing it, and ended up with a different (more dynamic) solution by monkey patching the TestLoader, which is a solution customized for my specific setup (so not really generic): from unittest.mock import patch
import sublime
from unittesting.core import TestLoader
from unittesting.package import UnitTestingCommand
from .pypaths import pyobj_dotted_path
class CursorTestLoader(TestLoader):
def discover(self, *args, **kwargs):
pypath = pyobj_dotted_path(sublime.active_window().active_view())
pypath = pypath.lstrip("home..config.sublime-text.Packages.")
return self.loadTestsFromName(pypath)
class UnitTestingAtCursorCommand(UnitTestingCommand):
"""A variant which runs a test method/class/module given the current
cursor position."""
def unit_testing(self, stream, package, settings, cleanup_hooks=[]):
with patch(
"unittesting.package.TestLoader",
side_effect=CursorTestLoader,
create=True,
):
return super().unit_testing(
stream=stream,
package=package,
settings=settings,
cleanup_hooks=cleanup_hooks,
) ...so personally I'm "covered" for this specific problem. =) |
I'm glad that you have found a way to get what you need. def unit_testing(self, *args, **kwargs):
with patch(
"unittesting.package.TestLoader",
side_effect=CursorTestLoader,
create=True,
):
return super().unit_testing(*args, **kwargs) |
Definitively. Thanks for the suggestion. |
This is a common use case, supported both by unittest and pytest. E.g. in unittest I can run an individual test method with:
With pytest:
Apparently with UnitTesting it's possible to specify a single test module to run, but not a test class or a test method.
Right now I am running individual test modules by using this build config:
I would like to be able to specify something like (mimicking pytest style):
To give even more context about my specific use case: I have a dynamic build system able to run individual test methods based on the cursor position (see ST forum post). I did this for both pytest and unittest. I would like to do the same by using UnitTesting.
@randy3k if you like the idea I can try working on a PR.
The text was updated successfully, but these errors were encountered: