private void checkConfigurationCreatedFrom(@NotNull final PsiElement element) {

    final PythonTestConfigurationProducer producer = createProducer();
    final ConfigurationFromContext context =
        producer.createConfigurationFromContext(new ConfigurationContext(element));
    Assert.assertNotNull(
        String.format("Failed to create context for %s", myTestRunnerName), context);
    Assert.assertNotNull(
        String.format("Configuration %s has not name", myTestRunnerName),
        context.getConfiguration().getName());
  }
  @Nullable
  @Override
  public RunnerAndConfigurationSettings findExistingConfiguration(ConfigurationContext context) {
    final RunnerAndConfigurationSettings existingConfiguration =
        super.findExistingConfiguration(context);
    if (existingConfiguration == null
        && GradleSystemRunningSettings.getInstance().getPreferredTestRunner()
            == GRADLE_TEST_RUNNER) {
      final ConfigurationFromContext createdContext = createConfigurationFromContext(context);
      if (createdContext != null) {
        final RunnerAndConfigurationSettings settings = createdContext.getConfigurationSettings();
        final RunManagerEx manager = RunManagerEx.getInstanceEx(context.getProject());
        manager.setTemporaryConfiguration(settings);
        return settings;
      } else {
        return null;
      }
    }

    return existingConfiguration;
  }
  protected RunnerAndConfigurationSettings createConfigurationByElement(
      final Location location, final ConfigurationContext context) {
    final PsiElement element = location.getPsiElement();
    final PsiFile file = element.getContainingFile();
    if (!(file instanceof GroovyFile)) {
      return null;
    }

    GroovyFile groovyFile = (GroovyFile) file;
    final PsiClass aClass = GroovyRunnerUtil.getRunningClass(location.getPsiElement());
    if (aClass instanceof GroovyScriptClass || GroovyRunnerUtil.isRunnable(aClass)) {
      final RunnerAndConfigurationSettings settings = createConfiguration(aClass);
      if (settings != null) {
        mySourceElement = element;
        final GroovyScriptRunConfiguration configuration =
            (GroovyScriptRunConfiguration) settings.getConfiguration();
        GroovyScriptTypeDetector.getScriptType(groovyFile)
            .tuneConfiguration(groovyFile, configuration, location);
        return settings;
      }
    }

    if (file.getText().contains("@Grab")) {
      ApplicationConfigurationProducer producer = new ApplicationConfigurationProducer();
      ConfigurationFromContext settings = producer.createConfigurationFromContext(context);
      if (settings != null) {
        PsiElement src = settings.getSourceElement();
        mySourceElement = src;
        return createConfiguration(
            src instanceof PsiMethod ? ((PsiMethod) src).getContainingClass() : (PsiClass) src);
      }

      return null;
    } else {
      return null;
    }
  }
 @Override
 public boolean isPreferredConfiguration(
     ConfigurationFromContext self, ConfigurationFromContext other) {
   return !other.isProducedBy(AbstractAllInDirectoryConfigurationProducer.class)
       && !other.isProducedBy(PatternConfigurationProducer.class);
 }
  private void doTest(String filename) throws Exception {
    configureByFile(BASE_PATH + filename);

    final Map<Integer, String> markers =
        JSTestUtils.extractPositionMarkers(getProject(), getEditor().getDocument());
    assertFalse("No caret markers found", markers.isEmpty());
    assertFalse("No 'expected' value", markers.containsValue(null));

    final DataContext dataContext =
        DataManager.getInstance().getDataContext(getEditor().getComponent());
    int i = 1;
    for (Map.Entry<Integer, String> marker : markers.entrySet()) {
      String place = filename + ": marker " + i++ + ": ";
      getEditor().getCaretModel().moveToOffset(marker.getKey());

      final ConfigurationFromContext configurationFromContext =
          new FlexUnitRuntimeConfigurationProducer()
              .createConfigurationFromContext(ConfigurationContext.getFromContext(dataContext));
      final RunConfiguration configuration =
          configurationFromContext == null ? null : configurationFromContext.getConfiguration();

      if ("null".equals(marker.getValue())) {
        assertNull(place + "Null configuration expected", configuration);
      } else {
        assertNotNull(place + "Not null configuration expected", configuration);
        assertTrue(
            place + "Invalid configuration", configuration instanceof FlexUnitRunConfiguration);

        final String[] expected;
        if ("null".equals(marker.getValue())) {
          expected = null;
        } else {
          expected = marker.getValue().split(" ");
          assertEquals(
              place
                  + "Expected should be in the form: \"Class com.test.Foo\" or \"Method com.test.Foo.testBar()\" or \"Package com.test\"",
              2,
              expected.length);
        }
        final FlexUnitRunnerParameters params =
            ((FlexUnitRunConfiguration) configuration).getRunnerParameters();
        assertEquals(place + "Invalid scope", expected[0], params.getScope().name());

        final String definition;
        switch (params.getScope()) {
          case Class:
            definition = params.getClassName();
            break;
          case Method:
            definition = params.getClassName() + "." + params.getMethodName() + "()";
            break;
          case Package:
            definition = params.getPackageName();
            break;

          default:
            fail(place + "Unknown scope: " + params.getScope());
            definition = null;
        }

        assertEquals(place + "Invalid definition", expected[1], definition);
      }
    }
  }
  @Override
  public void onFirstRun(
      ConfigurationFromContext configuration,
      ConfigurationContext context,
      Runnable startRunnable) {
    KtNamedDeclaration declarationToRun = getDeclarationToRun(configuration.getSourceElement());
    final PsiNamedElement lightElement =
        CollectionsKt.firstOrNull(LightClassUtilsKt.toLightElements(declarationToRun));

    // Copied from TestNGInClassConfigurationProducer.onFirstRun()
    if (lightElement instanceof PsiMethod || lightElement instanceof PsiClass) {
      PsiMethod psiMethod;
      PsiClass containingClass;

      if (lightElement instanceof PsiMethod) {
        psiMethod = (PsiMethod) lightElement;
        containingClass = psiMethod.getContainingClass();
      } else {
        psiMethod = null;
        containingClass = (PsiClass) lightElement;
      }

      InheritorChooser inheritorChooser =
          new InheritorChooser() {
            @Override
            protected void runForClasses(
                List<PsiClass> classes,
                PsiMethod method,
                ConfigurationContext context,
                Runnable performRunnable) {
              ((TestNGConfiguration) context.getConfiguration().getConfiguration())
                  .bePatternConfiguration(classes, method);
              super.runForClasses(classes, method, context, performRunnable);
            }

            @Override
            protected void runForClass(
                PsiClass aClass,
                PsiMethod psiMethod,
                ConfigurationContext context,
                Runnable performRunnable) {
              if (lightElement instanceof PsiMethod) {
                Project project = psiMethod.getProject();
                MethodLocation methodLocation =
                    new MethodLocation(project, psiMethod, PsiLocation.fromPsiElement(aClass));
                ((TestNGConfiguration) context.getConfiguration().getConfiguration())
                    .setMethodConfiguration(methodLocation);
              } else {
                ((TestNGConfiguration) context.getConfiguration().getConfiguration())
                    .setClassConfiguration(aClass);
              }
              super.runForClass(aClass, psiMethod, context, performRunnable);
            }
          };
      if (inheritorChooser.runMethodInAbstractClass(
          context,
          startRunnable,
          psiMethod,
          containingClass,
          new Condition<PsiClass>() {
            @Override
            public boolean value(PsiClass aClass) {
              return aClass.hasModifierProperty(PsiModifier.ABSTRACT) && TestNGUtil.hasTest(aClass);
            }
          })) return;
    }

    super.onFirstRun(configuration, context, startRunnable);
  }
 @Override
 public boolean isPreferredConfiguration(
     ConfigurationFromContext self, ConfigurationFromContext other) {
   return !other.isProducedBy(AbstractTestMethodConfigurationProducer.class);
 }