简体   繁体   English

JUnit可以对所有字符串进行断言吗?

[英]Can JUnit make assertions about all Strings?

Is there any way in JUnit (or any other testing framework) to make assertions about all possible inputs to a method? JUnit(或任何其他测试框架)中是否有任何方法可以对方法的所有可能输入进行断言?

Something like: 就像是:

assertTrue(myClass.myMethod(anyString))

EDIT: I realize that there are an infinite number of possible inputs but I guess I was wondering if there were some frameworks that could statically analyze a method and detect that in all possible cases a certain result would be reached. 编辑:我意识到有无限数量的可能输入,但是我想我想知道是否有一些框架可以静态地分析一种方法并检测在所有可能的情况下都会达到一定的结果。 Eg public static boolean myMethod(String input) { return true; } 例如public static boolean myMethod(String input) { return true; } public static boolean myMethod(String input) { return true; } will always return true? public static boolean myMethod(String input) { return true; }将始终返回true?

No, there is practically an unlimited number of possible inputs. 不,实际上输入的数量是无限的。
Its your job to separate them into test cases with (expected) equivalent behaviour. 将它们分成具有(预期)行为的测试用例是您的工作。 If such an artificial intelligence would exist, then it could also write the code to be tested. 如果存在这样的人工智能,那么它也可以编写要测试的代码。

There exist test case generators, that auto create test cases, but they are mostly useless. 有一些测试用例生成器,它们可以自动创建测试用例,但是它们几乎没有用。 They produce a huge amount of test cases, and mainly only touch the code, instead of testing an expected result. 他们产生了大量的测试用例,并且主要只接触代码,而不是测试预期的结果。

Such tools raise test coverage percentage, but in a very dubious way. 此类工具可提高测试覆盖率,但以一种非常可疑的方式。 (I would call that an illegal raise of test coverage: you should test, not touch!) (我认为这是非法提高考试覆盖率的方法:您应该进行测试,而不是碰碰!)

Such a tool is CodePro from Google. 这种工具是Google的CodePro Use CodePro->Test Case generation (eg within Eclipse) On first you will be a bit suprised, it's not to bad to try it out. 使用CodePro-> Test Case生成(例如在Eclipse中)首先,您会有点惊讶,尝试一下也不错。 Then you will know the limits of auto test case generation. 然后,您将知道自动测试用例生成的限制。

You cannot do this with JUnit. 您无法使用JUnit执行此操作。 The only way I think you could do such a thing would be using Formal Logic Verification 我认为您可以执行此操作的唯一方法是使用形式逻辑验证

As said before, it's not possible. 如前所述,这是不可能的。 However, there's the approach of automated property based testing. 但是,存在基于属性的自动化测试方法。 This comes somehow as close as possible to your idea. 这以某种方式尽可能接近您的想法。 Well, still far tough... 好吧,仍然很艰难...

For instance, hava a look at scalacheck : 例如,看看scalacheck

ScalaCheck is a library written in Scala and used for automated property-based testing of Scala or Java programs. ScalaCheck是一个用Scala编写的库,用于对Scala或Java程序进行基于属性的自动化测试。 ScalaCheck was originally inspired by the Haskell library QuickCheck, but has also ventured into its own. ScalaCheck最初是受Haskell库QuickCheck的启发,但也已经涉足了它。

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

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