BDD env tests added
authorIlya.Kazakevich <Ilya.Kazakevich@jetbrains.com>
Wed, 10 Sep 2014 21:41:25 +0000 (01:41 +0400)
committerIlya.Kazakevich <Ilya.Kazakevich@jetbrains.com>
Wed, 10 Sep 2014 21:41:25 +0000 (01:41 +0400)
python/testSrc/com/jetbrains/env/ut/PyUnitTestTask.java
python/testSrc/com/jetbrains/python/fixtures/PyTestCase.java

index 46b60bc9917f8bec109b97df5d08c71892d6ccd6..550ca6690cfdef7be6b15bbe70a31234ade33eeb 100644 (file)
@@ -1,8 +1,11 @@
 package com.jetbrains.env.ut;
 
 import com.google.common.collect.Lists;
-import com.intellij.execution.*;
+import com.intellij.execution.RunManager;
+import com.intellij.execution.RunManagerEx;
+import com.intellij.execution.RunnerAndConfigurationSettings;
 import com.intellij.execution.configurations.ConfigurationFactory;
+import com.intellij.execution.configurations.RunConfiguration;
 import com.intellij.execution.executors.DefaultRunExecutor;
 import com.intellij.execution.process.ProcessAdapter;
 import com.intellij.execution.process.ProcessEvent;
@@ -29,20 +32,37 @@ import com.jetbrains.python.sdk.flavors.PythonSdkFlavor;
 import com.jetbrains.python.testing.AbstractPythonTestRunConfiguration;
 import com.jetbrains.python.testing.PythonTestConfigurationType;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 import org.junit.Assert;
 
 /**
+ * Tasks to run unit test configurations.
+ * You should extend it either implementing {@link #after()} and {@link #before()} or implement {@link #runTestOn(String)}
+ * yourself and use {@link #runConfiguration(com.intellij.execution.configurations.ConfigurationFactory, String, com.intellij.openapi.project.Project)}
+ * or {@link #runConfiguration(com.intellij.execution.RunnerAndConfigurationSettings, com.intellij.execution.configurations.RunConfiguration)} .
+ * Use {@link #myDescriptor} and {@link #myConsoleView} to check output
+ *
  * @author traff
  */
 public abstract class PyUnitTestTask extends PyExecutionFixtureTestTask {
 
   protected ProcessHandler myProcessHandler;
   private boolean shouldPrintOutput = false;
-  private SMTestProxy.SMRootTestProxy myTestProxy;
-  private boolean mySetUp = false;
-  private SMTRunnerConsoleView myConsoleView;
-  private RunContentDescriptor myDescriptor;
+  /**
+   * Test root node
+   */
+  protected SMTestProxy.SMRootTestProxy myTestProxy;
+  /**
+   * Output test console
+   */
+  protected SMTRunnerConsoleView myConsoleView;
+  /**
+   * Test run descriptor
+   */
+  protected RunContentDescriptor myDescriptor;
+
   private StringBuilder myOutput;
+  private boolean mySetUp = false;
 
   public PyUnitTestTask() {
   }
@@ -83,30 +103,30 @@ public abstract class PyUnitTestTask extends PyExecutionFixtureTestTask {
   @Override
   public void tearDown() throws Exception {
     UIUtil.invokeAndWaitIfNeeded(new Runnable() {
-      @Override
-      public void run() {
-        try {
-          if (mySetUp) {
-            if (myConsoleView != null) {
-              Disposer.dispose(myConsoleView);
-              myConsoleView = null;
-            }
-            if (myDescriptor != null) {
-              Disposer.dispose(myDescriptor);
-              myDescriptor = null;
-            }
-
-
-            PyUnitTestTask.super.tearDown();
-
-            mySetUp = false;
-          }
-        }
-        catch (Exception e) {
-          throw new RuntimeException(e);
-        }
-      }
-    }
+                                   @Override
+                                   public void run() {
+                                     try {
+                                       if (mySetUp) {
+                                         if (myConsoleView != null) {
+                                           Disposer.dispose(myConsoleView);
+                                           myConsoleView = null;
+                                         }
+                                         if (myDescriptor != null) {
+                                           Disposer.dispose(myDescriptor);
+                                           myDescriptor = null;
+                                         }
+
+
+                                         PyUnitTestTask.super.tearDown();
+
+                                         mySetUp = false;
+                                       }
+                                     }
+                                     catch (Exception e) {
+                                       throw new RuntimeException(e);
+                                     }
+                                   }
+                                 }
 
     );
   }
@@ -151,10 +171,30 @@ public abstract class PyUnitTestTask extends PyExecutionFixtureTestTask {
       }
     }.execute();
 
-    final ExecutionEnvironment environment = ExecutionEnvironmentBuilder.create(DefaultRunExecutor.getRunExecutorInstance(), settings).build();
+    runConfiguration(settings, config);
+  }
+
+  /**
+   * Run configuration.
+   *
+   * @param settings settings (if have any, null otherwise)
+   * @param config configuration to run
+   * @throws Exception
+   */
+  protected void runConfiguration(@Nullable final RunnerAndConfigurationSettings settings,
+                                  @NotNull final RunConfiguration config) throws Exception {
+    final ExecutionEnvironment environment;
+    if (settings == null) {
+      environment = ExecutionEnvironmentBuilder.create(DefaultRunExecutor.getRunExecutorInstance(), config).build();
+    }
+    else {
+      environment = ExecutionEnvironmentBuilder.create(DefaultRunExecutor.getRunExecutorInstance(), settings).build();
+    }
     //noinspection ConstantConditions
+
     Assert.assertTrue(environment.getRunner().canRun(DefaultRunExecutor.EXECUTOR_ID, config));
 
+
     before();
 
     final com.intellij.util.concurrency.Semaphore s = new com.intellij.util.concurrency.Semaphore();
@@ -177,7 +217,7 @@ public abstract class PyUnitTestTask extends PyExecutionFixtureTestTask {
                   myOutput.append(event.getText());
                 }
               });
-              myConsoleView = (com.intellij.execution.testframework.sm.runner.ui.SMTRunnerConsoleView)descriptor.getExecutionConsole();
+              myConsoleView = (SMTRunnerConsoleView)descriptor.getExecutionConsole();
               myTestProxy = myConsoleView.getResultsViewer().getTestsRootNode();
               myConsoleView.getResultsViewer().addEventsListener(new TestResultsViewer.SMEventsAdapter() {
                 @Override
index c9740f0b21381a26fc177c18d11d293f6b2918e1..3528d7c6517d16c3faf6d7eb19ba834382807d4a 100644 (file)
@@ -98,12 +98,12 @@ public abstract class PyTestCase extends UsefulTestCase {
     IdeaTestFixtureFactory factory = IdeaTestFixtureFactory.getFixtureFactory();
     TestFixtureBuilder<IdeaProjectTestFixture> fixtureBuilder = factory.createLightFixtureBuilder(getProjectDescriptor());
     final IdeaProjectTestFixture fixture = fixtureBuilder.getFixture();
-    myFixture = IdeaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(fixture,
-                                                                                    new LightTempDirTestFixtureImpl(true));
-    myFixture.setUp();
+      myFixture = IdeaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(fixture,
+                                                                                      new LightTempDirTestFixtureImpl(true));
+      myFixture.setUp();
 
-    myFixture.setTestDataPath(getTestDataPath());
-  }
+      myFixture.setTestDataPath(getTestDataPath());
+    }
 
   protected String getTestDataPath() {
     return PythonTestUtil.getTestDataPath();
@@ -190,16 +190,24 @@ public abstract class PyTestCase extends UsefulTestCase {
     return myFixture.findElementByText("class " + name, PyClass.class);
   }
 
+  /**
+   * @see #moveByText(com.intellij.testFramework.fixtures.CodeInsightTestFixture, String)
+   */
+  protected void moveByText(@NotNull final String testToFind) {
+    moveByText(myFixture, testToFind);
+  }
+
   /**
    * Finds some text and moves cursor to it (if found)
    *
+   * @param fixture    test fixture
    * @param testToFind text to find
    * @throws AssertionError if element not found
    */
-  protected void moveByText(@NotNull final String testToFind) {
-    final PsiElement element = myFixture.findElementByText(testToFind, PsiElement.class);
+  public static void moveByText(@NotNull final CodeInsightTestFixture fixture, @NotNull final String testToFind) {
+    final PsiElement element = fixture.findElementByText(testToFind, PsiElement.class);
     assert element != null : "No element found by text: " + testToFind;
-    myFixture.getEditor().getCaretModel().moveToOffset(element.getTextOffset());
+    fixture.getEditor().getCaretModel().moveToOffset(element.getTextOffset());
   }
 
   /**
@@ -273,13 +281,17 @@ public abstract class PyTestCase extends UsefulTestCase {
 
   /**
    * Creates run configuration from right click menu
+   *
+   * @param fixture       test fixture
    * @param expectedClass expected class of run configuration
-   * @param <C> expected class of run configuration
+   * @param <C>           expected class of run configuration
    * @return configuration (if created) or null (otherwise)
    */
   @Nullable
-  protected <C extends RunConfiguration> C createRunConfigurationFromContext(@NotNull final Class<C> expectedClass) {
-    final DataContext context = DataManager.getInstance().getDataContext(myFixture.getEditor().getComponent());
+  public static <C extends RunConfiguration> C createRunConfigurationFromContext(
+    @NotNull final CodeInsightTestFixture fixture,
+    @NotNull final Class<C> expectedClass) {
+    final DataContext context = DataManager.getInstance().getDataContext(fixture.getEditor().getComponent());
     for (final RunConfigurationProducer<?> producer : RunConfigurationProducer.EP_NAME.getExtensions()) {
       final ConfigurationFromContext fromContext = producer.createConfigurationFromContext(ConfigurationContext.getFromContext(context));
       if (fromContext == null) {