简体   繁体   English

ScalaTest和Scala Specs单元测试框架之间有什么区别?

[英]What’s the difference between ScalaTest and Scala Specs unit test frameworks?

Both are BDD (Behavior Driven Development) capable unit test frameworks for Scala written in Scala. 两者都是用Scala编写的Scala的BDD(行为驱动开发)单元测试框架。 And Specs is built upon may also involve the ScalaTest framework. Specs 建立在 也可能涉及ScalaTest框架。 But what does Specs offer ScalaTest doesn't? 但规格提供ScalaTest的不是什么? What are the differences? 有什么区别?

Specs and ScalaTest are both good tools with happy users, but they differ in several ways. Specs和ScalaTest都是用户满意的好工具,但它们在几个方面有所不同。 You will probably want to pick one as your main testing tool in Scala, but need not give up the other because you can use pieces of both. 您可能希望在Scala中选择一个作为主要测试工具,但不需要放弃另一个,因为您可以使用两者的一部分。 If you like ScalaTest's FeatureSpec syntax and specs' Mockito syntax, for example, you can put both jar files in your classpath and use both at the same time. 例如,如果您喜欢ScalaTest的FeatureSpec语法和规范'Mockito语法,则可以将两个jar文件放在类路径中并同时使用它们。 Here I'll try and capture the main design philosophy differences I've noticed between specs and ScalaTest. 在这里,我将尝试捕捉我在规格和ScalaTest之间注意到的主要设计理念差异。

Probably the main philosophical difference between the tools is that specs is designed for Behavior-Driven Development (BDD), whereas ScalaTest is more general. 这些工具之间的主要哲学差异可能是规范是为行为驱动开发(BDD)而设计的,而ScalaTest则更为通用。 ScalaTest provides traits that you can mix together to get the behavior you prefer in your test classes, including BDD, and you can also easily define your own behavior if you want something different. ScalaTest提供的特征可以混合在一起,以便在测试类中获得您喜欢的行为,包括BDD,如果您想要不同的东西,也可以轻松定义自己的行为。

ScalaTest supports BDD through its Spec , FeatureSpec , WordSpec , FlatSpec , and GivenWhenThen traits, and also has traits that you can mix in to get a nice matcher syntax. ScalaTest通过其SpecFeatureSpecWordSpecFlatSpecGivenWhenThen特性支持BDD,并且还具有可以混合使用以获得良好匹配器语法的特征。 If you like "should", you mix in ShouldMatchers. 如果你喜欢“应该”,你可以混合使用ShouldMatchers。 If you like "must", you mix in MustMatchers . 如果你喜欢“必须”,你可以混合使用MustMatchers But if you like BDD but don't like matcher syntax, you can just use one of ScalaTest's Spec traits without mixing in a matchers trait. 但是如果您喜欢BDD但不喜欢matcher语法,那么您可以使用ScalaTest的Spec特性之一而不混合使用匹配器特性。 Specs has a Specification class that you extend, and you must use the word "must" in your matcher expressions. Specs有一个您扩展的Specification类,您必须在matcher表达式中使用“must”一词。 A big philosophical difference that is evident here is that ScalaTest gives you a lot more choices. 这里显而易见的一个重大哲学差异是ScalaTest为您提供了更多选择。 To make this space of choice easier to navigate, I provide a decision tree here: 为了使这个选择空间更容易导航,我在这里提供了一个决策树:

http://www.scalatest.org/quick_start http://www.scalatest.org/quick_start

The matcher syntax is also different between ScalaTest and specs. ScalaTest和规范之间的匹配器语法也不同。 In ScalaTest I tried to see how far I could go with operator notation, and ended up with matcher expressions that read very much like English sentences, with spaces between the words. 在ScalaTest中,我试着用运算符表示法来看多远,最后得到的matcher表达式非常像英语句子,单词之间有空格。 Specs matcher syntax runs words together more with camel case. 规范匹配器语法与camel case一起运行更多单词。

Specs has more matchers than ScalaTest, and that I think reflects a difference in design attitude. 规格比ScalaTest更多,我认为这反映了设计态度的差异。 I actually cut probably 2/3 of the matcher syntax I built and considered for release. 我实际上削减了我构建并考虑发布的matcher语法的2/3。 I will add more matchers in future releases, but wanted to be sure I knew users actually wanted something before I added it. 我将在未来的版本中添加更多匹配器,但我想确保在添加之前我知道用户确实想要一些东西。 However ScalaTest's matchers includes a dynamic property matcher syntax takes up some of that slack. 但是ScalaTest的匹配器包含一个动态属性匹配器语法,占用了一些松弛。 For example in Specs you can write on a java.io.File : 例如,在Specs中,您可以在java.io.File上编写:

file must beDirectory

This will invoke the isDirectory and make sure it is true. 这将调用isDirectory并确保它为true。 ScalaTest does not have any special matchers for java.io.Files currently, but in ScalaTest, you could just use a dynamic check like this: ScalaTest目前没有任何针对java.io.Files特殊匹配器,但在ScalaTest中,您可以像这样使用动态检查:

file must be a ('directory)

Anytime you pass a symbol in after be , it will use reflection to look for (in this case) a method or field named directory or a method named isDirectory . 无论何时在be之后传递符号,它都将使用反射来查找(在本例中)名为directory的方法或字段或名为isDirectory There's also a way to make this static, by defining a BePropertyMatcher (which requires only 2 or 3 lines of code usually). 通过定义BePropertyMatcher (通常只需要2或3行代码),还有一种方法可以使其静态化。 So basically in ScalaTest I try to provide more functionality with less API. 因此,基本上在ScalaTest中,我尝试使用更少的API提供更多功能。

Another general design attitude difference between specs and ScalaTest involves implicit conversions. 规范和ScalaTest之间的另一个一般设计态度差异涉及隐式转换。 By default you get only one implicit conversion when you use ScalaTest, which is the one that puts the === operator on everything. 默认情况下,当您使用ScalaTest时,您只获得一次隐式转换,这是将===运算符放在所有内容上的转换。 (If you need to, you can "turn off" this implicit conversion with one line of code. The only reason you would need to do that is if you were trying to test something that has its own === operator, and you get a conflict.) ScalaTest defines many other implicit conversions, but to use them you need to explicitly "invite" them into your code by mixing in a trait or doing an import. (如果需要,你可以用一行代码“关闭”这个隐式转换。你需要做的唯一原因就是你试图测试有自己的===运算符的东西,然后你得到冲突。)ScalaTest定义了许多其他隐式转换,但要使用它们,您需要通过混合特征或执行导入将它们显式“邀请”到您的代码中。 When you extend class Specification in specs I think you pretty much get dozens of implicit conversions by default. 当您在规范中扩展类Specification ,我认为默认情况下您几乎可以获得数十个隐式转换。 I'm not sure how much that will matter in practice, but I figure people will want to test code that uses their own implicits, and sometimes there may be a conflict between the test framework's implicits and those of the production code. 我不确定在实践中有多重要,但我认为人们会想要测试使用自己的含义的代码,有时候测试框架的含义与生产代码的含义之间可能存在冲突。 When that happens I think it may be easier to work around the problem in ScalaTest than specs. 当发生这种情况时,我认为在ScalaTest中解决问题可能比规范更容易。

Another difference in design attitude that I've noticed is comfort with operators. 我注意到设计态度的另一个不同之处在于操作员的舒适度。 One goal I had was that any programmer looking at someone else's test code that uses ScalaTest would be able to guess what the meaning was without looking anything up in the ScalaTest documentation. 我的一个目标是,任何使用ScalaTest查看其他人的测试代码的程序员都能够在ScalaTest文档中查看其中的含义而无需查看。 I wanted ScalaTest client code to be drop dead obvious. 我希望ScalaTest客户端代码显而易见。 One way that goal manifested itself is that ScalaTest is very conservative about operators. 目标表现的一种方式是ScalaTest对运营商非常保守。 I only define five operators in ScalaTest: 我只在ScalaTest中定义了五个运算符:

  • === , which means equals === ,这意味着平等
  • > , which means greater than > ,这意味着大于
  • < , less than < ,不到
  • >= , greater than or equal >= ,大于或等于
  • <= , less than or equal. <= ,小于或等于。

That's it. 而已。 So these things pretty much look like what mean. 所以这些东西看起来很像是什么意思。 If you see in someone else's code: 如果你看到别人的代码:

result should be <= 7

My hope is that you won't need to run to the API documentation to guess what that <= means. 我希望您不需要运行API文档来猜测<=意思。 By contrast, specs is much freer with operators. 相比之下,运营商的规格更加自由。 Nothing wrong with that, but it is a difference. 没错,但这是一个区别。 Operators can make code more concise, but the tradeoff is you may have to run to the documentation when you find things like ->- , >> , | 操作员可以使代码更简洁,但是当您找到诸如->->>|等的内容时,可能需要权衡这些代码| , |> , ! |> , ! , or ^^^ (which all have special meanings in Specs) in your colleague's test code. ,或者^^^ (在规范中都有特殊含义)在你同事的测试代码中。

One other philosophical difference is that I do try and make it just slightly easier in ScalaTest to use a functional style when you need to share a fixture, whereas Specs by default continues the tradition of the setUp and tearDown approach popularized by JUnit, in which you reassign vars before each test. 另一个哲学上的区别是,我确实尝试在ScalaTest中稍微更轻松地在需要共享夹具时使用函数样式,而默认情况下Specs继续传统的由JUnit推广的setUptearDown方法,其中你在每次测试之前重新分配变量。 However if you want to test that way, it is also very easy in ScalaTest. 但是,如果您想以这种方式进行测试,那么在ScalaTest中也很容易。 You just need to mix in the BeforeAndAfter trait. 你只需要混合BeforeAndAfter特性。

For more insight into ScalaTest, you can watch the "Get Higher with ScalaTest" presentation I gave at the 2009 Devoxx conference here: 有关ScalaTest的更多信息,您可以在2009年Devoxx会议上观看“ScalaTest获得更高”的演示:

http://parleys.com/play/514892260364bc17fc56bde3/chapter0/about http://parleys.com/play/514892260364bc17fc56bde3/chapter0/about

The main differences are (mostly from a specs point of view :-) ): 主要区别(主要来自规格观点:-)):

  • ScalaTest provides more "testing styles" than specs (you can visit each bullet point on the quick start page to get a detailed view on each style) ScalaTest提供了比规范更多的“测试样式”(您可以访问快速入门页面上的每个项目符号点以获得每种样式的详细视图)

  • ScalaTest and specs have a different set of matchers. ScalaTest和规范有一组不同的匹配器。 You can compare them here for ScalaTest and here for specs. 你可以在这里比较它们的ScalaTest和这里的规格。 On that side of things, specs has a lot of small features that you may like when writing your specification: xml matchers, matchers composition (an easy way to reuse matchers by transforming them), precise failures, detailed differences for long strings, ... 在这方面,规范在编写规范时有许多小功能:xml匹配器,匹配器组合(通过转换它们来重用匹配器的简单方法),精确故障,长字符串的详细差异,... 。

  • Mockito has been given a nice BDD support in specs: Mockito Mockito在规格方面得到了很好的BDD支持: Mockito

  • specs has DataTables which allow to group a lot of small example in a sort of table (if you can stand operators being used as the table delimiters) specs有DataTables允许在一个表中组合很多小例子(如果你可以将运算符用作表分隔符)

  • In specs, you can define examples which are nested as libidum and automatically cleaned-up at every level 在规范中,您可以定义嵌套为libidum并在每个级别自动清理的示例

This is certainly a very partial and biased comparison and many other differences exist (and the libraries are still evolving, ...). 这当然是一种非常偏袒和偏见的比较,并且存在许多其他差异(并且图书馆仍在不断发展,......)。

At the end of the day I think that it really depends on your testing/specifying style. 在一天结束时,我认为这实际上取决于您的测试/指定样式。 If it's simple (simple specification structure, setups, expectations, ...) then both libraries will appear very similar. 如果它很简单(简单的规范结构,设置,期望......),那么两个库看起来都非常相似。 Otherwise, both have their take on how things should be done. 否则,两者都要考虑应该如何做。 As a last example of this you can have a look at tagging: in ScalaTest and in specs . 作为最后一个示例,您可以查看标记:在ScalaTest规范中

I hope this helps. 我希望这有帮助。

据我所知,除了一些高度专业化的功能外,它还取决于个人喜好。

IDE support may be another point IDE支持可能是另一个观点

I've been trying to get Specs to work with Eclipse through JUnit, and I found the official solution to be a bit "hacky". 我一直试图通过JUnit让Specs与Eclipse一起工作,我发现官方解决方案有点“hacky”。 Specs setup: http://code.google.com/p/specs/wiki/RunningSpecs#Run_your_specification_with_JUnit4_in_Eclipse 规格设置: http//code.google.com/p/specs/wiki/RunningSpecs#Run_your_specification_with_JUnit4_in_Eclipse

ScalaTest's integration (also through JUnit) with seems a bit less hacky. ScalaTest的集成(也通过JUnit)似乎有点不那么hacky。 Still, I haven't got any of them to work as well as JUnit and Java. 尽管如此,我还没有像JUnit和Java一样工作。

ScalaTest setup: http://groups.google.com/group/scalatest-users/web/running-scalatest-from-eclipse ScalaTest设置: http//groups.google.com/group/scalatest-users/web/running-scalatest-from-eclipse

If one decision factor is the compile time, scalatest seems to perform better. 如果一个决策因素是编译时间,则scalatest似乎表现更好。

We're currently using specs2 in our project, but suffer from slow compile times in tests. 我们目前在项目中使用specs2,但在测试中遇到编译时间慢的问题。 I just finished a POC on moving to scalatest and saw compile times drop by a factor of about 0.82 just by switching the 2 frameworks in some of our sources. 我刚刚完成了一个关于移动到scalatest的POC,并且通过在我们的一些源中切换2个框架,看到编译时间下降了大约0.82。

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

相关问题 测试和规范之间有什么区别? - What's the difference between tests and specs? Scala测试:SUnit,ScalaTest,ScalaCheck,Specs和ParTest的状态和关系是什么? - Scala testing: What's the status and relationship of SUnit, ScalaTest, ScalaCheck, Specs and ParTest? 这两个单元测试断言有什么区别? - What is the difference between these two Unit Test Assertions? 关键字“ test”和“ it”有什么区别 - What's the difference between keywords 'test' and 'it' 单元测试框架有外观吗? - Is there a facade for Unit Test Frameworks? 那里的.NET单元测试框架有什么区别? - What's the difference between the .NET Unit Testing framework out there? 测试套件和测试组之间有什么区别? - What's the difference between test suite and test group? 如何使用Specs2对该Play Scala控制器进行单元测试? - How can I unit test this Play Scala controller using Specs2? Play Framework 2 scala specs2 mockito,我该如何编写一个模拟单元测试 - Play Framework 2 scala specs2 mockito, how do I write a mocking unit test 使用“Run As”运行 JUnit 测试和 Maven 的测试有什么区别? - What is the difference between running JUnit tests with "Run As" and Maven's test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM