PyElement (@NotNull final PsiElement anchor, @NotNull final PyTestCreationModel model) { final Project project = anchor.getProject(); IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace(); String fileName = model.getFileName(); if (!fileName.endsWith(".py")) { fileName = fileName + "." + PythonFileType.INSTANCE.getDefaultExtension(); } final PyFile psiFile = PyClassRefactoringUtil.getOrCreateFile(model.getTargetDir() + "/" + fileName, project); final String className = model.getClassName(); final List<String> methods = model.getMethods(); final boolean classBased = !StringUtil.isEmptyOrSpaces(className); final LanguageLevel level = LanguageLevel.forElement(psiFile); final PyElementGenerator generator = PyElementGenerator.getInstance(project); PyElement result = psiFile; if (classBased) { final boolean unitTestClassRequired = PythonUnitTestDetectorsBasedOnSettings.isTestCaseClassRequired(anchor); final StringBuilder fileText = new StringBuilder(); fileText.append("class ").append(className); if (unitTestClassRequired) { fileText.append("(TestCase)"); } else if (LanguageLevel.forElement(anchor).isPython2()) { fileText.append("(object)"); } fileText.append(":\n\t"); if (methods.isEmpty()) { fileText.append("pass\n"); } for (final String method : methods) { fileText.append("def ").append(method).append("(self):\n\t"); if (unitTestClassRequired) { fileText.append("self.fail()"); } else { fileText.append("assert False"); } fileText.append("\n\n\t"); } if (unitTestClassRequired) { AddImportHelper.addOrUpdateFromImportStatement(psiFile, "unittest", "TestCase", null, AddImportHelper.ImportPriority.BUILTIN, null); } result = (PyElement)psiFile.addAfter(generator.createFromText(level, PyClass.class, fileText.toString()), psiFile.getLastChild()); } else { for (final String method : methods) { final PyFunction fun = generator.createFromText(level, PyFunction.class, String.format("def %s():\n\tassert False\n\n", method)); psiFile.addAfter(fun, psiFile.getLastChild()); } } PostprocessReformattingAspect.getInstance(project).doPostponedFormatting(psiFile.getViewProvider()); CodeStyleManager.getInstance(project).reformat(psiFile); return result; }