自动单元测试例code [英] Automatically Unit Test Example Code

查看:118
本文介绍了自动单元测试例code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队负责一个API开发的大型系统,我们也写了。我们需要提供例如code,这样使用我们的API等开发人员可以学习如何使用它。我们一直在记录使用XML文档注释code。 如:

My team is responsible for the development of an API for a large system that we also write. We need to provide example code so that other developers using our API can learn how to use it. We have been documenting the code using the xml document comments. eg.

/// <summary>Summary here</summary>
/// <example>Here is an example  <code>example code here</code> </example>
public void SomeFunction()

我们再使用沙堡,并建立我们所需要的帮助文件(CHM和在线网站)。

We then use Sandcastle and build the help files we need (chm and an online website).

这是很尴尬的时候,例如code不工作,而这通常是因为某些功能已经改变,或一个简单的错误。

It is quite embarrassing when the example code doesnt work, and this is usually because some functionality has changed or a simple error.

有没有人做过这样的事情,而且还配置单元测试的例子code运行,这样它们已知的生成过程中的工作?

Has anyone ever done something like this, but also configured unit tests to run on the example code so that they are known to work during the build?

推荐答案

是的,沙塔支持这一点,它是伟大的保持的例子的正确性。您可以指向这样一个code区:

Yes, sandcastle supports this and it's great to maintain the correctness of examples. You can point to a code region like this:

   /// <summary>
   /// Gizmo which can act as client or server.
   /// </summary>
   /// <example>
   /// The following example shows how to use the gizmo as a client:
   /// <code lang="cs"
   ///    source="..\gizmo.unittests\TestGizmo.cs"
   ///    region="GizmoClientSample"/>
   /// </example>
   public class Gizmo

您可以再使用一些测试code在TestGizmo.cs,例如,通过封闭在一个区域:

You can then use some test code in TestGizmo.cs as an example by enclosing it in a region:

[Test]
public GizmoCanActAsClient()
{
   #region GizmoClientSample
   Gizmo gizmo = new Gizmo();
   gizmo.ActAsClient();
   #endregion
}

警告:如果您移动或重命名测试文件,你将只有当你试图重新与沙堡的文档获取有关这个错误

Caveat: If you move or rename the test file, you will only get an error about this when you try to regenerate the documentation with sandcastle.

这篇关于自动单元测试例code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆