19

我是 Testacular(现在是 Karma)的新手。但我发现它非常强大,非常适合自动跨浏览器 JS 测试。所以我想知道是否可以将它用作 TFS 构建过程的一部分来进行自动 JS 代码单元测试?如果有人有以前的经验,请告诉我们要注意什么,以免我们走错路。

问候,君

4

2 回答 2

9

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);
            }
        }
    }
}
于 2013-09-03T01:31:39.690 回答
1

与最初的问答相比,发生了很多变化。

然而,我们已经通过运行 Grunt 任务让 Karma 在我们的 TFS 构建中运行(我相信 Gulp/无论你拥有什么任务运行器都可以做到这一点)。我们以前使用 C#,但最近发生了变化。

  1. 运行grunt build任务。
  2. 之后添加一个 Grunt 任务
  3. 将文件路径指向您gruntfile.js并运行您的test任务。此任务将运行karma:single。grunt-cli 的位置可能是node_modules/grunt-cli/bin/grunt.

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

  4. 添加发布测试结果步骤。测试结果文件 =**/*.trx

有关发布业力测试结果的更多信息

于 2017-02-20T21:36:40.567 回答