简体   繁体   English

如何在Visual Studio 2008中对F#代码进行单元测试?

[英]How to unit test F# code in Visual Studio 2008?

I'm new to F# and I intend to create new F# project, and unit test it with F# in Visual Studio 2008, but the Visual Studio 2008 Unit Test Wizard don't have a F# test project option (just have C, C# and VB options). 我是F#的新手,我打算创建新的F#项目,并在Visual Studio 2008中使用F#进行单元测试,但Visual Studio 2008单元测试向导没有F#测试项目选项(只有C,C#和VB选项)。

  1. Is it possible to unit test F#? 是否有可能对F#进行单元测试?
  2. Is it possible to unit test F# with a F# test project? 是否可以使用F#测试项目对F#进行单元测试?
  3. Is it possible to unit test F# with a F# test project in Visual Studio 2008? 是否可以在Visual Studio 2008中使用F#测试项目对F#进行单元测试?
  4. How to do it, if there is no Wizard in Visual Studio 2008? 如果Visual Studio 2008中没有向导,该怎么做?

I found this article on f# testing with nunit (link here ). 我在nunit上找到了关于f#测试的这篇文章(链接在这里 )。 I do remember however that there's another way to verify the validity of functional language code (I 'worked' with Clean, which is similar to Haskell for my bachelor). 我确实记得有另一种方法来验证函数式语言代码的有效性(我'使用Clean工作',类似于我的单身汉的Haskell)。 Just can't remember exactly what they called it, I do remember it's more like a mathematical proof. 只是不记得他们究竟叫什么,我记得它更像是一个数学证明。 Also, I'm not sure that it works for F#, as it's a bit different from Haskell'esque languages if I read that correctly. 此外,我不确定它是否适用于F#,因为如果我正确阅读它,它与Haskell'esque语言有点不同。

  1. If you're generating .NET code, it is possible to unit test F# with nunit, as long as the interface is reasonably CLI compliant. 如果您正在生成.NET代码,则可以使用nunit对F#进行单元测试,只要该接口具有合理的CLI兼容性即可。
  2. Nunit works with .NET assemblies, not with any particular language. Nunit适用于.NET程序集,不使用任何特定语言。 If you can drop the [TestFixture] attribute on a class and can drop the [Test] attribute on a public method in a particular class, which returns void, then NUnit can run it. 如果您可以删除类上的[TestFixture]属性并且可以将[Test]属性放在特定类中的公共方法上,该类返回void,那么NUnit可以运行它。
  3. Don't know. 不知道。
  4. Don't know. 不知道。

This link describes using the VS testing system with F#. 此链接描述了使用带有F#的VS测试系统。 You do it pretty much the same way as with C#. 你这样做与C#几乎一样。

The downside is that apparently the VS IDE won't automatically pick it up -- I believe you need to run from the command line using mstest.exe . 缺点是显然VS IDE不会自动获取它 - 我相信你需要使用mstest.exe从命令行运行。

Edit: Oh, another cool thing with F#, is FsCheck . 编辑:哦,F#的另一个很酷的事情是FsCheck This allows you to write simple tests, and have the FsCheck system try to disprove them via random testing. 这允许您编写简单的测试,并让FsCheck系统尝试通过随机测试来反驳它们。

There's nothing forcing you to use F# to test F#. 没有什么可以强迫你用F#来测试F#。 You could simply create a unit test assembly in C#, VB or another .NET supported language, and simply reference the F# assembly needed to perform the tests. 您可以简单地使用C#,VB或其他.NET支持的语言创建单元测试程序集,并简单地引用执行测试所需的F#程序集。 You would do it just as you would create unit tests for any other .NET assembly. 您可以像为任何其他.NET程序集创建单元测试一样。

I'm not sure whether this is strictly related to your exact question, but there are a whole series of posts on Matthew Podwysocki's blog about unit testing in functional languages - there are some F# bits in there too. 我不确定这是否与您的确切问题严格相关,但Matthew Podwysocki博客上有一系列关于函数式语言单元测试的帖子 - 其中也有一些F#位。

Are you asking if it's possible to unit test in F# or are you asking if there's a wizard for it? 你问是否有可能在F#中进行单元测试,或者你在问它是否有一个向导? It's possible to unit test in any language, in fact, functional languages are a unit testers wet dream (I heard it described that way.) since everything is static and you've verified all the edge cases you've essentially verified every case. 可以用任何语言进行单元测试,事实上,功能语言是单位测试人员的梦想(我听说它是​​这样描述的。)因为一切都是静态的,你已经验证了所有边缘情况,你基本上都验证过每一种情况。

EDIT: To answer the fourth question, write your own test cases: 编辑:要回答第四个问题,编写自己的测试用例:

http://en.wikipedia.org/wiki/Unit_testing http://en.wikipedia.org/wiki/Unit_testing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM