public static IUnitTestClassGenerator GetUnitTestGenerator(TestFrameworks testFramework, IMockGenerator mockGenerator) { switch (testFramework) { case TestFrameworks.MsTest: return(new MsTestClassGenerator(mockGenerator)); case TestFrameworks.NUnit: return(new NunitTestClassGenerator(mockGenerator)); default: throw new ArgumentOutOfRangeException(nameof(testFramework), testFramework, null); } }
/// <summary> /// Generate unit tests by looking at exported types from an assembly /// </summary> /// <param name="assemblyPath">Path to the assembly</param> /// <param name="testFramework">Test framework to use when generating code</param> /// <param name="mockFramework">Mock framework to use when generating code</param> /// <param name="outputDirectory">The output path for the generated dll file</param> public void GenerateUnitTests(string assemblyPath, TestFrameworks testFramework, MockFrameworks mockFramework, string outputDirectory) { var mockGenerator = MockGeneratorFactory.GetMockGenerator(mockFramework); var unitTestGenerator = UnitTestGeneratorFactory.GetUnitTestGenerator(testFramework, mockGenerator); var assembly = Assembly.LoadFrom(assemblyPath); var assemblyName = assembly.GetName().Name; var assemblyTestName = $"{assemblyName}.Tests"; var exportedTypes = assembly.ExportedTypes.Where(t => t.IsPublic && !t.IsAbstract && !t.IsInterface); _fileService.CreateDirectory(Path.Combine(outputDirectory, assemblyTestName)); foreach (var typeUnderTest in exportedTypes) { GenerateClass(typeUnderTest, assemblyName, assemblyTestName, unitTestGenerator, outputDirectory); } }
public BoilerplateSettings() { SettingsManager settingsManager = new ShellSettingsManager(ServiceProvider.GlobalProvider); this.store = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings); this.store.CreateCollection(CollectionPath); // Setting upgrade if needed if (this.store.PropertyExists(CollectionPath, VersionKey)) { int storedVersion = this.store.GetInt32(CollectionPath, VersionKey); if (storedVersion < LatestVersion) { this.SetVersionToLatest(); } } else { if (this.store.PropertyExists(CollectionPath, TestProjectsKey)) { // We are upgrading from an old version (v0), as we didn't have version tracked, but had a test projects dictionary var mockFrameworks = new List <string> { "Moq", "AutoMoq", "SimpleStubs", "NSubstitute" }; var templateTypes = new List <TemplateType> { TemplateType.File, TemplateType.MockFieldDeclaration, TemplateType.MockFieldInitialization, TemplateType.MockObjectReference }; foreach (string mockFrameworkName in mockFrameworks) { MockFramework mockFramework = MockFrameworks.Get(mockFrameworkName); foreach (TemplateType templateType in templateTypes) { string templateKey = $"Template_{mockFrameworkName}_{templateType}"; if (this.store.PropertyExists(CollectionPath, templateKey)) { string oldTemplate = this.store.GetString(CollectionPath, templateKey); this.CreateEntryForTestFramework(oldTemplate, templateType, TestFrameworks.Get("VisualStudio"), mockFramework); this.CreateEntryForTestFramework(oldTemplate, templateType, TestFrameworks.Get("NUnit"), mockFramework); this.store.DeleteProperty(CollectionPath, templateKey); } } } } this.SetVersionToLatest(); } if (this.store.PropertyExists(CollectionPath, TestProjectsKey)) { string dictionaryString = this.store.GetString(CollectionPath, TestProjectsKey); if (string.IsNullOrEmpty(dictionaryString)) { this.testProjectsDictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); } else { this.testProjectsDictionary = new Dictionary <string, string>(JsonConvert.DeserializeObject <Dictionary <string, string> >(dictionaryString), StringComparer.OrdinalIgnoreCase); } } else { this.testProjectsDictionary = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); } }
public async System.Threading.Tasks.Task GenerateTestFilesAsync(Project classesProject) { ProjectItem casesFolder = classesProject.ProjectItems.Item("Cases"); var dte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); string selfTestFilesDirectory = SolutionUtilities.GetSelfTestDirectoryFromSandbox(dte); string targetFolder = Path.Combine(selfTestFilesDirectory, "Actual"); if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } var classNames = new List <string>(); foreach (ProjectItem classItem in casesFolder.ProjectItems) { classNames.Add(Path.GetFileNameWithoutExtension(classItem.Name)); } // testFW|mockFW|className var testDefinitions = new HashSet <string>(); // First, iterate over all test frameworks with Moq and choose all classes MockFramework moqFramework = MockFrameworks.Get(MockFrameworks.MoqName); foreach (TestFramework testFramework in TestFrameworks.List) { foreach (string className in classNames) { TryAddTestDefinition(testDefinitions, testFramework, moqFramework, className); } } // Next, iterate over all mock frameworks with VS framework and choose all classes TestFramework vsFramework = TestFrameworks.Get(TestFrameworks.VisualStudioName); foreach (MockFramework mockFramework in MockFrameworks.List) { foreach (string className in classNames) { TryAddTestDefinition(testDefinitions, vsFramework, mockFramework, className); } } // Last, choose a single file and iterate over all mock frameworks and test frameworks string classWithGenericInterface = "ClassWithGenericInterface"; foreach (TestFramework testFramework in TestFrameworks.List) { foreach (MockFramework mockFramework in MockFrameworks.List) { TryAddTestDefinition(testDefinitions, testFramework, mockFramework, classWithGenericInterface); } } foreach (string testDefinition in testDefinitions) { string[] parts = testDefinition.Split('|'); string testFrameworkName = parts[0]; string mockFrameworkName = parts[1]; string className = parts[2]; await this.GenerateTestFileAsync(classesProject, TestFrameworks.Get(testFrameworkName), MockFrameworks.Get(mockFrameworkName), className, targetFolder); } }
public SelfTestDetectionsResult RunDetectionTests(IList <Project> targetProjects) { var dte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); var result = new SelfTestDetectionsResult(); var frameworkPickerService = new FrameworkPickerService(); var defaultSettings = new MockBoilerplateSettings { PreferredTestFramework = null, PreferredMockFramework = null }; var vsMoqSettings = new MockBoilerplateSettings { PreferredTestFramework = TestFrameworks.Get(TestFrameworks.VisualStudioName), PreferredMockFramework = MockFrameworks.Get(MockFrameworks.MoqName) }; var nunitNSubSettings = new MockBoilerplateSettings { PreferredTestFramework = TestFrameworks.Get(TestFrameworks.NUnitName), PreferredMockFramework = MockFrameworks.Get(MockFrameworks.NSubstituteName) }; var testList = new List <SelfTestDetectionTest> { new SelfTestDetectionTest("AutoMoqTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.AutoMoqName), new SelfTestDetectionTest("NetCoreMoqTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.MoqName), new SelfTestDetectionTest("NetCoreNSubstituteTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.NSubstituteName), new SelfTestDetectionTest("NetCoreNUnitTestCases", defaultSettings, TestFrameworks.NUnitName, MockFrameworks.MoqName), new SelfTestDetectionTest("NetCoreSimpleStubsTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.SimpleStubsName), new SelfTestDetectionTest("NetCoreVSRhinoMocksTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.RhinoMocksName), new SelfTestDetectionTest("NoFrameworkTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.MoqName), new SelfTestDetectionTest("NoFrameworkTestCases", nunitNSubSettings, TestFrameworks.NUnitName, MockFrameworks.NSubstituteName), new SelfTestDetectionTest("NSubstituteTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.NSubstituteName), new SelfTestDetectionTest("NUnitTestCases", defaultSettings, TestFrameworks.NUnitName, MockFrameworks.MoqName), new SelfTestDetectionTest("NUnitUwpTestCases", defaultSettings, TestFrameworks.NUnitName, MockFrameworks.SimpleStubsName), new SelfTestDetectionTest("SimpleStubsTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.SimpleStubsName), new SelfTestDetectionTest("VSRhinoMocksTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.RhinoMocksName), new SelfTestDetectionTest("VSTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.MoqName), new SelfTestDetectionTest("XUnitMoqTestCases", defaultSettings, TestFrameworks.XUnitName, MockFrameworks.MoqName), new SelfTestDetectionTest("MultipleFrameworkTestCases", defaultSettings, TestFrameworks.NUnitName, MockFrameworks.NSubstituteName), new SelfTestDetectionTest("MultipleFrameworkTestCases", vsMoqSettings, TestFrameworks.VisualStudioName, MockFrameworks.MoqName), }; var failures = new List <string>(); foreach (SelfTestDetectionTest test in testList) { result.TotalCount++; string projectFileName = GetFileNameFromSandboxProjectName(test.ProjectName, dte); frameworkPickerService.Settings = test.Settings; TestFramework actualTestFramework = frameworkPickerService.FindTestFramework(projectFileName); MockFramework actualMockFramework = frameworkPickerService.FindMockFramework(projectFileName); if (test.ExpectedTestFramework != actualTestFramework.Name) { failures.Add($"Expected {test.ExpectedTestFramework} test framework for {test.ProjectName} but got {actualTestFramework.Name}. (Preferred Framework: {test.Settings.PreferredTestFramework})"); } else if (test.ExpectedMockFramework != actualMockFramework.Name) { failures.Add($"Expected {test.ExpectedMockFramework} mock framework for {test.ProjectName} but got {actualMockFramework.Name}. (Preferred Framework: {test.Settings.PreferredMockFramework})"); } else { result.SucceededCount++; } } //foreach (Project testProject in targetProjects) //{ // result.TotalCount++; // TestFramework actualTestFramework = frameworkPickerService.FindTestFramework(testProject.FileName); // MockFramework actualMockFramework = frameworkPickerService.FindMockFramework(testProject.FileName); // SelfTestDetectionResult expectedDetectionResult; // if (!ExpectedDetectionResults.TryGetValue(testProject.Name, out expectedDetectionResult)) // { // failures.Add("Could not find expected detection results for project " + testProject.Name); // } // else if (expectedDetectionResult.TestFramework != actualTestFramework.Name) // { // failures.Add($"Expected {expectedDetectionResult.TestFramework} test framework for {testProject.Name} but got {actualTestFramework.Name}"); // } // else if (expectedDetectionResult.MockFramework != actualMockFramework.Name) // { // failures.Add($"Expected {expectedDetectionResult.MockFramework} mock framework for {testProject.Name} but got {actualMockFramework.Name}"); // } // else // { // result.SucceededCount++; // } //} result.Failures = failures; return(result); }
public SelfTestDetectionsResult RunDetectionTests(IList <Project> targetProjects) { var dte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); var result = new SelfTestDetectionsResult(); var frameworkPickerService = new FrameworkPickerService(); var defaultSettings = new MockBoilerplateSettings { PreferredTestFramework = null, PreferredMockFramework = null }; var noMockFrameworkSettings = new MockBoilerplateSettings { PreferredTestFramework = null, PreferredMockFramework = MockFrameworks.Get(MockFrameworks.NoneName) }; var vsMoqSettings = new MockBoilerplateSettings { PreferredTestFramework = TestFrameworks.Get(TestFrameworks.VisualStudioName), PreferredMockFramework = MockFrameworks.Get(MockFrameworks.MoqName) }; var nunitNSubSettings = new MockBoilerplateSettings { PreferredTestFramework = TestFrameworks.Get(TestFrameworks.NUnitName), PreferredMockFramework = MockFrameworks.Get(MockFrameworks.NSubstituteName) }; var testList = new List <SelfTestDetectionTest> { new SelfTestDetectionTest("AutoMoqTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.AutoMoqName), new SelfTestDetectionTest("FakeItEasyTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.FakeItEasyName), new SelfTestDetectionTest("JustMockTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.JustMockName), new SelfTestDetectionTest("MoqTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.MoqName), new SelfTestDetectionTest("NetFrameworkPackagesConfigTestCases", defaultSettings, TestFrameworks.NUnitName, MockFrameworks.NSubstituteName), new SelfTestDetectionTest("NetStandardPropsTestCases", defaultSettings, TestFrameworks.NUnitName, MockFrameworks.NSubstituteName), new SelfTestDetectionTest("NoFrameworkTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.NoneName), new SelfTestDetectionTest("NoFrameworkTestCases", nunitNSubSettings, TestFrameworks.NUnitName, MockFrameworks.NSubstituteName), new SelfTestDetectionTest("NSubstituteTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.NSubstituteName), new SelfTestDetectionTest("NUnitTestCases", defaultSettings, TestFrameworks.NUnitName, MockFrameworks.MoqName), new SelfTestDetectionTest("RhinoMocksTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.RhinoMocksName), new SelfTestDetectionTest("VSTestCases", defaultSettings, TestFrameworks.VisualStudioName, MockFrameworks.MoqName), new SelfTestDetectionTest("VSTestCases", noMockFrameworkSettings, TestFrameworks.VisualStudioName, MockFrameworks.NoneName), new SelfTestDetectionTest("XUnitTestCases", defaultSettings, TestFrameworks.XUnitName, MockFrameworks.MoqName), new SelfTestDetectionTest("MultipleFrameworkTestCases", defaultSettings, TestFrameworks.NUnitName, MockFrameworks.NSubstituteName), new SelfTestDetectionTest("MultipleFrameworkTestCases", vsMoqSettings, TestFrameworks.VisualStudioName, MockFrameworks.MoqName), }; var failures = new List <string>(); foreach (SelfTestDetectionTest test in testList) { result.TotalCount++; Project project = GetProjectFromName(test.ProjectName, dte); if (project == null) { failures.Add($"Cannot find project '{test.ProjectName}'"); } else { frameworkPickerService.SettingsFactory = new MockBoilerplateSettingsFactory(test.Settings); TestFramework actualTestFramework = frameworkPickerService.FindTestFramework(project, test.Settings); MockFramework actualMockFramework = frameworkPickerService.FindMockFramework(project, test.Settings); if (test.ExpectedTestFramework != actualTestFramework.Name) { failures.Add($"Expected {test.ExpectedTestFramework} test framework for {test.ProjectName} but got {actualTestFramework.Name}. (Preferred Framework: {test.Settings.PreferredTestFramework})"); } else if (test.ExpectedMockFramework != actualMockFramework.Name) { failures.Add($"Expected {test.ExpectedMockFramework} mock framework for {test.ProjectName} but got {actualMockFramework.Name}. (Preferred Framework: {test.Settings.PreferredMockFramework})"); } else { result.SucceededCount++; } } } result.Failures = failures; return(result); }
/// <summary> /// Generate unit tests asynchronous by looking at exported types from an assembly /// </summary> /// <param name="assemblyPath">Path to the assembly</param> /// <param name="testFramework">Test framework to use when generating code</param> /// <param name="mockFramework">Mock framework to use when generating code</param> /// <param name="outputDirectory">The output path for the generated dll file</param> public Task GenerateUnitTestsAsync(string assemblyPath, TestFrameworks testFramework, MockFrameworks mockFramework, string outputDirectory) { return(Task.Run(() => GenerateUnitTests(assemblyPath, testFramework, mockFramework, outputDirectory))); }