Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possibility to use Karma with TFS builds

I'm new to the Testacular(now Karma). But I found it is really powerful and great for automatic cross-browser JS testing. So I want to know if it is possible to use it as part of TFS building procedure to conduct automatic JS code unit testing? If anyone has previous experience, could you please let us know what to notice so that we are not going to take the wrong way.

Regards, Jun

like image 692
bigbearzhu Avatar asked Apr 16 '13 00:04

bigbearzhu


2 Answers

Here is my pseudo code to run the karma in TFS using C# helper class. The basic idea is:

  1. Use C# unit test to test your js files using Karma.
  2. Capture the output of Karma to show that in your build log.
  3. Use separate process to run Karma.
  4. Pack all Karma files into a zip file, extract that into temporary folder for each build, so that builds with different version of karma wouldn't conflict with each other.
  5. Clean the temp folder after build.

-

namespace Test.Javascript.CrossBrowserTests
{
    public class KarmaTestRunner : IDisposable
    {
        private const string KarmaPath = @".\node_modules\karma\bin\karma";

        private string NodeBasePath { get; set; }
        private string NodeFullPath { get { return NodeBasePath + @"\node\node.exe"; } }
        private string NpmFullPath { get { return NodeBasePath + @"\node\npm.cmd"; } }

        public KarmaTestRunner()
        {
            ExtractKarmaZip();
            LinkGlobalKarma();
        }

        public int Execute(params string[] arguments)
        {
            Process consoleProcess = RunKarma(arguments);
            return consoleProcess.ExitCode;
        }

        public void Dispose()
        {
            UnlinkGlobalKarma();
            RemoveTempKarmaFiles();
        }

        private void ExtractKarmaZip()
        {
            NodeBasePath = Path.GetTempPath() + Path.GetRandomFileName();
            byte[] resourceBytes = Assembly.GetExecutingAssembly().GetEmbeddedResourceBytes(typeof(KarmaTestRunner).Namespace + "." + "karma0.9.4.zip");

            ZipFile file = ZipFile.Read(resourceBytes);
            file.ExtractAll(NodeBasePath);
        }

        private void LinkGlobalKarma()
        {
            ExecuteConsoleProcess(NpmFullPath, "link", "karma");
        }

        private Process RunKarma(IEnumerable<string> arguments)
        {
            return ExecuteConsoleProcess(NodeFullPath, new[] { KarmaPath }.Concat(arguments).ToArray());
        }

        private static Process ExecuteConsoleProcess(string path, params string[] arguments)
        {
            //Create a process to run karma with arguments
            //Hook up the OutputDataReceived envent handler on the process
        }

        static void OnOutputLineReceived(string message)
        {
            if (message != null)
                Console.WriteLine(message);
        }

        private void UnlinkGlobalKarma()
        {
            ExecuteConsoleProcess(NpmFullPath, "uninstall", "karma");
        }

        private void RemoveTempKarmaFiles()
        {
            Directory.Delete(NodeBasePath, true);
        }
    }
}

Then use it like this:

namespace Test.Javascript.CrossBrowserTests
{
    [TestClass]
    public class CrossBrowserJSUnitTests
    {
        [TestMethod]
        public void JavascriptTestsPassForAllBrowsers()
        {
            using (KarmaTestRunner karmaRunner = new KarmaTestRunner())
            {
                int exitCode = karmaRunner.Execute("start", @".\Test.Project\Javascript\Karma\karma.conf.js");
                exitCode.ShouldBe(0);
            }
        }
    }
}
like image 179
bigbearzhu Avatar answered Nov 11 '22 17:11

bigbearzhu


A lot has changed since the original question and answer.

However, we've gotten Karma to run in our TFS build by running a Grunt task (I'm sure the same is possible with Gulp/whatever task runner you have). We were using C# before, but recently changed.

  1. Have a grunt build task run.
  2. Add a Grunt task after that
  3. point the file path to your gruntfile.js and run your test task. This task will run karma:single. The grunt-cli location may be node_modules/grunt-cli/bin/grunt.

    grunt.registerTask('test', [ 'karma:single' ]);

  4. Add a Publish Test Results step. Test Results Files = **/*.trx

More information about publishing Karma Test Results

like image 1
AlignedDev Avatar answered Nov 11 '22 16:11

AlignedDev