示例#1
0
        public void Test(IEnumerable <SolutionProject> testProjects = null)
        {
            if (testProjects == null)
            {
                testProjects = Projects.Where(p => p.Name.EndsWith(".Tests"));
            }

            foreach (SolutionProject project in testProjects)
            {
                NormalizedPath projectPath = project.Path.GetDirectory().FullPath;
                NormalizedPath binDir      = projectPath.AppendPart("bin").AppendPart(_globalInfo.BuildConfiguration);
                NormalizedPath objDir      = projectPath.AppendPart("obj");
                string         assetsJson  = File.ReadAllText(objDir.AppendPart("project.assets.json"));
                bool           isNunitLite = assetsJson.Contains("NUnitLite");
                bool           isVSTest    = assetsJson.Contains("Microsoft.NET.Test.Sdk");
                foreach (NormalizedPath buildDir in Directory.GetDirectories(binDir))
                {
                    string framework            = buildDir.LastPart;
                    string fileWithoutExtension = buildDir.AppendPart(project.Name);
                    string testBinariesPath     = "";
                    if (isNunitLite)
                    {
                        // Using NUnitLite.
                        testBinariesPath = fileWithoutExtension + ".exe";
                        if (File.Exists(testBinariesPath))
                        {
                            _globalInfo.Cake.Information($"Testing via NUnitLite ({framework}): {testBinariesPath}");
                            if (!_globalInfo.CheckCommitMemoryKey(testBinariesPath))
                            {
                                _globalInfo.Cake.NUnit3(new[] { testBinariesPath }, new NUnit3Settings
                                {
                                    Results = new[] { new NUnit3Result()
                                                      {
                                                          FileName = FilePath.FromString(projectPath.AppendPart("TestResult.Net461.xml"))
                                                      } }
                                });
                            }
                        }
                        else
                        {
                            testBinariesPath = fileWithoutExtension + ".dll";
                            _globalInfo.Cake.Information($"Testing via NUnitLite ({framework}): {testBinariesPath}");
                            if (!_globalInfo.CheckCommitMemoryKey(testBinariesPath))
                            {
                                _globalInfo.Cake.DotNetCoreExecute(testBinariesPath);
                            }
                        }
                    }
                    if (isVSTest)
                    {
                        testBinariesPath = fileWithoutExtension + ".dll";
                        // VS Tests
                        _globalInfo.Cake.Information($"Testing via VSTest ({framework}): {testBinariesPath}");
                        if (!_globalInfo.CheckCommitMemoryKey(testBinariesPath))
                        {
                            var options = new DotNetCoreTestSettings()
                            {
                                Configuration = _globalInfo.BuildConfiguration,
                                Framework     = framework,
                                NoRestore     = true,
                                NoBuild       = true,
                                Logger        = "trx"
                            };
                            if (_globalInfo.Cake.Environment.GetEnvironmentVariable("DisableNodeReUse") != null)
                            {
                                options.ArgumentCustomization = args => args.Append("/nodeReuse:false");
                            }
                            _globalInfo.Cake.DotNetCoreTest(projectPath, options);
                        }
                    }

                    if (!isVSTest && !isNunitLite)
                    {
                        testBinariesPath = fileWithoutExtension + ".dll";
                        _globalInfo.Cake.Information("Testing via NUnit: {0}", testBinariesPath);
                        if (!_globalInfo.CheckCommitMemoryKey(testBinariesPath))
                        {
                            _globalInfo.Cake.NUnit(new[] { testBinariesPath }, new NUnitSettings()
                            {
                                Framework   = "v4.5",
                                ResultsFile = FilePath.FromString(projectPath.AppendPart("TestResult.Net461.xml"))
                            });
                        }
                    }
                    _globalInfo.WriteCommitMemoryKey(testBinariesPath);
                }
            }
        }