diff --git a/python/src/com/jetbrains/python/testing/PythonUnitTestCommandLineState.java b/python/src/com/jetbrains/python/testing/PythonUnitTestCommandLineState.java index 491ab16ec530..57b3c88a064a 100644 --- a/python/src/com/jetbrains/python/testing/PythonUnitTestCommandLineState.java +++ b/python/src/com/jetbrains/python/testing/PythonUnitTestCommandLineState.java @@ -14,8 +14,12 @@ import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.execution.runners.ProgramRunner; import com.intellij.execution.testframework.sm.SMTestRunnerConnectionUtil; import com.intellij.execution.ui.ConsoleView; +import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.containers.HashMap; import com.jetbrains.python.PythonHelpersLocator; import org.jetbrains.annotations.NotNull; @@ -71,12 +75,6 @@ public class PythonUnitTestCommandLineState extends CommandLineState { cmd.setWorkDirectory(myConfig.getWorkingDirectory()); } - cmd.getParametersList().addParametersString(myConfig.getInterpreterOptions()); - cmd.addParameter(new File(helpersRoot, UTRUNNER_PY).getAbsolutePath()); - for (String testSpec : getTestSpecs()) { - cmd.addParameter(testSpec); - } - Map envs = myConfig.getEnvs(); if (envs == null) envs = new HashMap(); @@ -84,8 +82,29 @@ public class PythonUnitTestCommandLineState extends CommandLineState { envs = new HashMap(envs); envs.put(PYTHONUNBUFFERED, "1"); - insertToPythonPath(envs, helpersRoot); - // TODO[yole] use -Dpython.path for jython + + List pythonPathList = new ArrayList(); + pythonPathList.add(helpersRoot.getPath()); + final Module module = myConfig.getModule(); + if (module != null) { + final VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots(); + for (VirtualFile contentRoot : contentRoots) { + pythonPathList.add(FileUtil.toSystemDependentName(contentRoot.getPath())); + } + } + String pythonPath = StringUtil.join(pythonPathList, File.pathSeparator); + if (new File(myConfig.getInterpreterPath()).getName().toLowerCase().startsWith("jython")) { // HACK rewrite with cleaner API + cmd.getParametersList().add("-Dpython.path=" + pythonPath); + } + else { + insertToPythonPath(envs, pythonPath); + } + + cmd.getParametersList().addParametersString(myConfig.getInterpreterOptions()); + cmd.addParameter(new File(helpersRoot, UTRUNNER_PY).getAbsolutePath()); + for (String testSpec : getTestSpecs()) { + cmd.addParameter(testSpec); + } cmd.setEnvParams(envs); cmd.setPassParentEnvs(myConfig.isPassParentEnvs()); @@ -116,11 +135,11 @@ public class PythonUnitTestCommandLineState extends CommandLineState { return specs; } - private static void insertToPythonPath(Map envs, File path) { + private static void insertToPythonPath(Map envs, String path) { if (envs.containsKey(PYTHONPATH)) { - envs.put(PYTHONPATH, path.getAbsolutePath() + ":" + envs.get(PYTHONPATH)); + envs.put(PYTHONPATH, path + File.pathSeparatorChar + envs.get(PYTHONPATH)); } else { - envs.put(PYTHONPATH, path.getAbsolutePath()); + envs.put(PYTHONPATH, path); } } diff --git a/python/testData/testRunner/dependentTests/my_class_test.py b/python/testData/testRunner/dependentTests/my_class_test.py new file mode 100644 index 000000000000..8c6a68e2b69e --- /dev/null +++ b/python/testData/testRunner/dependentTests/my_class_test.py @@ -0,0 +1,7 @@ +import unittest +from testedCode.my_class import * + +class MyClassTest(unittest.TestCase): + def test_foo(self): + c = MyClass() + self.assertEquals("bar", c.foo()) diff --git a/python/testData/testRunner/testedCode/__init__.py b/python/testData/testRunner/testedCode/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/python/testData/testRunner/testedCode/my_class.py b/python/testData/testRunner/testedCode/my_class.py new file mode 100644 index 000000000000..fbf4632343b7 --- /dev/null +++ b/python/testData/testRunner/testedCode/my_class.py @@ -0,0 +1,4 @@ +class MyClass: + def foo(self): + return "bar" + \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/testRunner/PyTestRunnerTest.java b/python/testSrc/com/jetbrains/python/testRunner/PyTestRunnerTest.java index 3befc64ec935..5359d2edd770 100644 --- a/python/testSrc/com/jetbrains/python/testRunner/PyTestRunnerTest.java +++ b/python/testSrc/com/jetbrains/python/testRunner/PyTestRunnerTest.java @@ -68,6 +68,12 @@ public class PyTestRunnerTest extends LightPlatformTestCase { assertEquals(8, result.length); } + public void testDependent() throws ExecutionException { + final File testDir = new File(PathManager.getHomePath(), "plugins/python/testData/testRunner"); + String[] result = runUTRunner(testDir.getPath(), new File(testDir, "dependentTests/my_class_test.py").getPath()); + assertEquals(3, result.length); + } + private static String[] runUTRunner(String workDir, String... args) throws ExecutionException { File helpersDir = new File(PathManager.getHomePath(), "plugins/python/helpers"); File utRunner = new File(helpersDir, "pycharm/utrunner.py"); @@ -89,10 +95,9 @@ public class PyTestRunnerTest extends LightPlatformTestCase { File jythonJar = new File(PathManager.getHomePath(), "plugins/python/lib/jython.jar"); parameters.getClassPath().add(jythonJar.getPath()); + parameters.getProgramParametersList().add("-Dpython.path=" + pythonPath + ";" + workDir); parameters.getProgramParametersList().addAll(args); - parameters.setWorkingDirectory(workDir); - parameters.getVMParametersList().add("-Dpython.path=" + pythonPath); final StringBuilder stdout = new StringBuilder(); final StringBuilder stderr = new StringBuilder();