2015-09-22 107 views
2

我试图在使用tsUnit的Typescript中设置一个单元测试。我创建了一个非常简单的测试,以确保我正确地做事。但是,每次我单击测试资源管理器中的全部运行时,它都不会显示任何结果!看起来好像Visual Studios 2013在我的解决方案中找不到任何有效的测试。以下是我在代码:我的单元测试没有显示在测试资源管理器中

/// <reference path="../../UnitTesting/UnitTesting/Scripts/tsUnit/tsUnit.ts" /> 

module CalculationsTests { 
    export class SimpleMathTests extends tsUnit.TestClass { 

     addTwoNumbersWith1And2Expect3() { 
      var result = 1 + 2; 

      this.areIdentical(3, result); 
     } 

     addTwoNumbersWith3And2Expect5() { 
      var result = 3 + 6; 

      this.areIdentical(4, result); // Deliberate error 
     } 
    } 
} 

window.onload =() => { 
    // new instance of tsUnit - pass in modules that contain test classes 
    var test = new tsUnit.Test(CalculationsTests); 

    // Handle the results yourself... 
    var result = test.run(); 

    var outcome = (result.errors.length === 0) ? 'Test Passed' : 'Test Failed'; 

    alert(JSON.stringify(outcome)); 
}; 

在我的主HTML文件,我有:

<!DOCTYPE html> 

<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>TypeScript HTML App</title> 
    <link rel="stylesheet" href="app.css" type="text/css" /> 
    <script src="/Scripts/tsUnit/tsUnit.js"></script> 
    <script src="app.js"></script> 
</head> 
<body> 
    <h1>TypeScript HTML App</h1> 
    <h2>TS Unit Test</h2> 
    <div id="content"></div> 
</body> 
</html> 

这里的结果:

enter image description here

谁能帮我找到了我需要做些什么才能正确设置单元测试?

+0

您忘记提及您正在使用ES6功能。这可能会导致测试被抛弃。我不使用Visual Studio for Javascript,但是**可能是发生了什么的可能性。尝试将其转换为ES5,使用常规对象而不是模块或类。 –

+1

试用Jasmine单元测试框架,在VS –

+0

中有几个很好的集成工具存在于这个测试框架中@Oleg Dokuka谢谢我会试试 – mmangual83

回答

0

tsUnit有几个输出选项,您有基于浏览器的输出或TAP输出(这对命令行使用更好),或者您可以基于原始数据生成自己的输出。

要与Visual Studio集成,您需要在.NET测试中包装您的tsUnit测试。没有编写Visual Studio扩展来允许tsUnit测试发现。您可以在项目中使用单个测试类来完成此操作,例如在C#项目中,您可以编写一个MSTest or NUnit test that calls the TypeScript tests

相关问题