Exemple #1
0
        private bool TestWithNUnit(string outputFilePath)
        {
            bool hasErrors = false;

            #region <nunit2>
            var task = new NUnit2Task();
            // this little assignment makes the whole TestTask very difficult to unit test
            // unless maybe we subclass Project for testing?
            task.Project = Project;

            #region <formatter type="Plain" />
            var formatter = new FormatterElement();
            formatter.Type = FormatterType.Plain;
            task.FormatterElements.Add(formatter);
            #endregion

            #region <test assemblyname="outputFilePath" />
            var test = new NUnit2Test();
            test.AssemblyFile = new FileInfo(outputFilePath);
            task.Tests.Add(test);
            #endregion

            try
            {
                task.Execute();
            }
            catch (BuildException be)
            {
                hasErrors = true;
                Log(Level.Error, be.Message);
            }
            #endregion
            return(hasErrors);
        }
        private void CreateSummaryDocument(string resultFile, TextWriter writer, NUnit2Test test)
        {
            XPathDocument originalXPathDocument = new XPathDocument(resultFile);
            XslTransform  summaryXslTransform   = new XslTransform();
            XmlTextReader transformReader       = GetTransformReader(test);

            summaryXslTransform.Load(transformReader);
            summaryXslTransform.Transform(originalXPathDocument, null, writer);
        }
Exemple #3
0
        private void CreateSummaryDocument(string resultFile, TextWriter writer, NUnit2Test test)
        {
            XPathDocument originalXPathDocument = new XPathDocument(resultFile);
            // Using XslTransform instead of XslCompiledTransform because the latter
            // does not display nunit output for unknown reasons.
            XslTransform  summaryXslTransform = new XslTransform();
            XmlTextReader transformReader     = GetTransformReader(test);

            summaryXslTransform.Load(transformReader);
            summaryXslTransform.Transform(originalXPathDocument, null, writer);
        }
Exemple #4
0
        private static StringCollection GetTestAssemblies(NUnit2Test test)
        {
            var files = new StringCollection();

            if (test.AssemblyFile.FullName != null)
            {
                files.Add(test.AssemblyFile.FullName);
            }
            else
            {
                files = test.Assemblies.FileNames;
            }

            return(files);
        }
        private XmlTextReader GetTransformReader(NUnit2Test test)
        {
            XmlTextReader transformReader;

            if (test.XsltFile == null)
            {
                Assembly        assembly        = Assembly.GetAssembly(typeof(XmlResultVisitor));
                ResourceManager resourceManager = new ResourceManager("NUnit.Util.Transform", assembly);
                string          xmlData         = (string)resourceManager.GetObject("Summary.xslt", CultureInfo.InvariantCulture);
                transformReader = new XmlTextReader(new StringReader(xmlData));
            }
            else
            {
                if (!test.XsltFile.Exists)
                {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           "Transform file '{0}' does not exist.", test.XsltFile.FullName),
                                             Location);
                }
                transformReader = new XmlTextReader(test.XsltFile.FullName);
            }

            return(transformReader);
        }
        private void FormatResult(NUnit2Test testElement, TestResult result)
        {
            // temp file for storing test results
            string xmlResultFile = Path.GetTempFileName();

            // permanent file for storing test results
            string outputFile = null;

            try {
                XmlResultVisitor resultVisitor = new XmlResultVisitor(xmlResultFile, result);
                result.Accept(resultVisitor);
                resultVisitor.Write();

                foreach (FormatterElement formatter in FormatterElements)
                {
                    if (formatter.Type == FormatterType.Xml)
                    {
                        if (formatter.UseFile)
                        {
                            // determine file name for output file
                            outputFile = result.Name + "-results" + formatter.Extension;

                            if (formatter.OutputDirectory != null)
                            {
                                // ensure output directory exists
                                if (!formatter.OutputDirectory.Exists)
                                {
                                    formatter.OutputDirectory.Create();
                                }

                                // combine output directory and result filename
                                outputFile = Path.Combine(formatter.OutputDirectory.FullName,
                                                          Path.GetFileName(outputFile));
                            }

                            // copy the temp result file to permanent location
                            File.Copy(xmlResultFile, outputFile, true);
                        }
                        else
                        {
                            using (StreamReader reader = new StreamReader(xmlResultFile)) {
                                // strip off the xml header
                                reader.ReadLine();
                                StringBuilder builder = new StringBuilder();
                                while (reader.Peek() > -1)
                                {
                                    builder.Append(reader.ReadLine().Trim()).Append(
                                        Environment.NewLine);
                                }
                                Log(Level.Info, builder.ToString());
                            }
                        }
                    }
                    else if (formatter.Type == FormatterType.Plain)
                    {
                        TextWriter writer;
                        if (formatter.UseFile)
                        {
                            // determine file name for output file
                            outputFile = result.Name + "-results" + formatter.Extension;

                            if (formatter.OutputDirectory != null)
                            {
                                // ensure output directory exists
                                if (!formatter.OutputDirectory.Exists)
                                {
                                    formatter.OutputDirectory.Create();
                                }

                                // combine output directory and result filename
                                outputFile = Path.Combine(formatter.OutputDirectory.FullName,
                                                          Path.GetFileName(outputFile));
                            }

                            writer = new StreamWriter(outputFile);
                        }
                        else
                        {
                            writer = new LogWriter(this, Level.Info, CultureInfo.InvariantCulture);
                        }
                        CreateSummaryDocument(xmlResultFile, writer, testElement);
                        writer.Close();
                    }
                }
            } catch (Exception ex) {
                throw new BuildException("Test results could not be"
                                         + " formatted.", Location, ex);
            } finally {
                // make sure temp file with test results is removed
                File.Delete(xmlResultFile);
            }
        }