diff --git a/python/helpers/pycharm/pycharm_commands/__init__.py b/python/helpers/pycharm/pycharm_commands/__init__.py new file mode 100644 index 000000000000..5267186f7dd2 --- /dev/null +++ b/python/helpers/pycharm/pycharm_commands/__init__.py @@ -0,0 +1 @@ +# __init__ \ No newline at end of file diff --git a/python/helpers/pycharm/pycharm_commands/pycharm_test.py b/python/helpers/pycharm/pycharm_commands/pycharm_test.py new file mode 100644 index 000000000000..e9a08ffc8716 --- /dev/null +++ b/python/helpers/pycharm/pycharm_commands/pycharm_test.py @@ -0,0 +1,19 @@ +__author__ = 'ktisha' +try: + from pkg_resources import EntryPoint + from setuptools.command import test + from tcunittest import TeamcityTestRunner +except ImportError: + raise NameError("Something went wrong, do you have setuptools installed?") + +class pycharm_test(test.test): + def run_tests(self): + import unittest + + loader_ep = EntryPoint.parse("x=" + self.test_loader) + loader_class = loader_ep.load(require=False) + unittest.main( + None, None, [unittest.__file__] + self.test_args, + testRunner=TeamcityTestRunner, + testLoader=loader_class() + ) diff --git a/python/helpers/pycharm/pycharm_setup_runner.py b/python/helpers/pycharm/pycharm_setup_runner.py new file mode 100644 index 000000000000..84830a17f6eb --- /dev/null +++ b/python/helpers/pycharm/pycharm_setup_runner.py @@ -0,0 +1,23 @@ +__author__ = 'ktisha' + +import sys +from pycharm_run_utils import PYTHON_VERSION_MAJOR, PYTHON_VERSION_MINOR +#noinspection PyUnresolvedReferences +import pycharm_commands # we need pycharm_commands module to be loaded + +if __name__ == "__main__": + test_suite = sys.argv.pop(-1) + + __file__ = test_suite + sys.argv.append("--command-packages") + sys.argv.append("pycharm_commands") + sys.argv.append("pycharm_test") + + + if PYTHON_VERSION_MINOR == 2 and PYTHON_VERSION_MAJOR == 4: + #noinspection PyCompatibility + execfile(test_suite) + else: + #noinspection PyCompatibility + with open(test_suite, "r") as fh: + exec (fh.read(), globals(), locals()) diff --git a/python/psi-api/src/com/jetbrains/python/PyNames.java b/python/psi-api/src/com/jetbrains/python/PyNames.java index 49111f5391db..526a77b57c6c 100644 --- a/python/psi-api/src/com/jetbrains/python/PyNames.java +++ b/python/psi-api/src/com/jetbrains/python/PyNames.java @@ -25,6 +25,8 @@ public class PyNames { public static final String DOT_PY = ".py"; public static final String INIT_DOT_PY = INIT + DOT_PY; + public static final String SETUP_DOT_PY = "setup" + DOT_PY; + public static final String NEW = "__new__"; public static final String GETATTR = "__getattr__"; public static final String GETATTRIBUTE = "__getattribute__"; diff --git a/python/src/com/jetbrains/python/testing/PythonTestConfigurationProducer.java b/python/src/com/jetbrains/python/testing/PythonTestConfigurationProducer.java index 3efe1f8ebbe5..2448bfe8215a 100644 --- a/python/src/com/jetbrains/python/testing/PythonTestConfigurationProducer.java +++ b/python/src/com/jetbrains/python/testing/PythonTestConfigurationProducer.java @@ -140,6 +140,8 @@ abstract public class PythonTestConfigurationProducer extends RuntimeConfigurati protected boolean isTestFile(PsiElement file) { if (file == null || !(file instanceof PyFile)) return false; + final List testCases = getTestCaseClassesFromFile((PyFile)file); + if (testCases.isEmpty()) return false; return true; } @@ -192,8 +194,6 @@ abstract public class PythonTestConfigurationProducer extends RuntimeConfigurati if (!isTestFile(file)) return null; final PyFile pyFile = (PyFile)file; - final List testCases = getTestCaseClassesFromFile(pyFile); - if (testCases.isEmpty()) return null; final RunnerAndConfigurationSettings settings = makeConfigurationSettings(location, "tests from file"); final AbstractPythonTestRunConfiguration configuration = (AbstractPythonTestRunConfiguration)settings.getConfiguration(); diff --git a/python/src/com/jetbrains/python/testing/unittest/PythonUnitTestCommandLineState.java b/python/src/com/jetbrains/python/testing/unittest/PythonUnitTestCommandLineState.java index 4f9d58a3dc4c..11a55713bbbe 100644 --- a/python/src/com/jetbrains/python/testing/unittest/PythonUnitTestCommandLineState.java +++ b/python/src/com/jetbrains/python/testing/unittest/PythonUnitTestCommandLineState.java @@ -5,6 +5,8 @@ import com.intellij.execution.configurations.GeneralCommandLine; import com.intellij.execution.configurations.ParamsGroup; import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.openapi.util.text.StringUtil; +import com.jetbrains.python.PyNames; +import com.jetbrains.python.testing.AbstractPythonTestRunConfiguration; import com.jetbrains.python.testing.PythonTestCommandLineStateBase; import java.util.ArrayList; @@ -17,6 +19,7 @@ public class PythonUnitTestCommandLineState extends PythonTestCommandLineStateBase { private final PythonUnitTestRunConfiguration myConfig; private static final String UTRUNNER_PY = "pycharm/utrunner.py"; + private static final String SETUP_PY_TESTRUNNER = "pycharm/pycharm_setup_runner.py"; public PythonUnitTestCommandLineState(PythonUnitTestRunConfiguration runConfiguration, ExecutionEnvironment env) { super(runConfiguration, env); @@ -25,6 +28,9 @@ public class PythonUnitTestCommandLineState extends @Override protected String getRunner() { + if (myConfig.getTestType() == AbstractPythonTestRunConfiguration.TestType.TEST_SCRIPT && + myConfig.getScriptName().endsWith(PyNames.SETUP_DOT_PY)) + return SETUP_PY_TESTRUNNER; return UTRUNNER_PY; } @@ -64,6 +70,9 @@ public class PythonUnitTestCommandLineState extends assert script_params != null; if (myConfig.useParam() && !StringUtil.isEmptyOrSpaces(myConfig.getParams())) script_params.addParameter(myConfig.getParams()); - script_params.addParameter(String.valueOf(myConfig.isPureUnittest())); + + if (myConfig.getTestType() != AbstractPythonTestRunConfiguration.TestType.TEST_SCRIPT || + !myConfig.getScriptName().endsWith(PyNames.SETUP_DOT_PY)) + script_params.addParameter(String.valueOf(myConfig.isPureUnittest())); } } diff --git a/python/src/com/jetbrains/python/testing/unittest/PythonUnitTestConfigurationProducer.java b/python/src/com/jetbrains/python/testing/unittest/PythonUnitTestConfigurationProducer.java index 7505dbe9ff06..db643c06284c 100644 --- a/python/src/com/jetbrains/python/testing/unittest/PythonUnitTestConfigurationProducer.java +++ b/python/src/com/jetbrains/python/testing/unittest/PythonUnitTestConfigurationProducer.java @@ -10,12 +10,13 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtilCore; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; -import com.jetbrains.python.psi.PyClass; -import com.jetbrains.python.psi.PyElement; -import com.jetbrains.python.psi.PyFunction; +import com.jetbrains.python.PyNames; +import com.jetbrains.python.psi.*; import com.jetbrains.python.testing.*; import org.jetbrains.annotations.Nullable; +import java.util.List; + public class PythonUnitTestConfigurationProducer extends PythonTestConfigurationProducer { public PythonUnitTestConfigurationProducer() { super(PythonTestConfigurationType.getInstance().PY_UNITTEST_FACTORY); @@ -63,4 +64,13 @@ public class PythonUnitTestConfigurationProducer extends PythonTestConfiguration if (pyClass == null || !PythonUnitTestUtil.isUnitTestCaseClass(pyClass)) return false; return true; } + + @Override + protected boolean isTestFile(PsiElement file) { + if (file == null || !(file instanceof PyFile)) return false; + if (PyNames.SETUP_DOT_PY.equals(((PyFile)file).getName())) return true; + final List testCases = getTestCaseClassesFromFile((PyFile)file); + if (testCases.isEmpty()) return false; + return true; + } } \ No newline at end of file