请输入您要查询的百科知识:

 

词条 Hamcrest
释义

  1. Rationale

  2. References

  3. External links

Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively.[1] These matchers have uses in unit testing frameworks such as JUnit and jMock. Hamcrest has been included in JUnit 4 since 2012[2],

but was omitted from Junit 5 in 2017.[3]

Hamcrest has been ported to C++, C#,[4] Objective-C, Python,[5] ActionScript 3,[6] PHP,[7] JavaScript,[8] Erlang,[9], R[10], Rust,[11]. and Swift[12]

Rationale

"First generation" unit test frameworks provide an 'assert' statement, allowing one to assert during a test that a particular condition must be true. If the condition is false, the test fails. For example:

assert(x == y);

But this syntax fails to produce a sufficiently good error message if 'x' and 'y' are not equal. It would be better if the error message displayed the value of 'x' and 'y'. To solve this problem, "second generation" unit test frameworks provide a family of assertion statements, which produce better error messages. For example,

assert_equal(x, y);

assert_not_equal(x, y);

But this leads to an explosion in the number of assertion macros, as the above set is expanded to support comparisons different from simple equality. So "third generation" unit test frameworks use a library such as Hamcrest to support an 'assert_that' operator that can be combined with 'matcher' objects, leading to syntax like this:

assert_that(x, equal_to(y))

assert_that(x, is_not(equal_to(y)))

The benefit is that there are still fluent error messages when the assertion fails, but with greater extensibility. It is now possible to define operations that take matchers as arguments and return them as results, leading to a grammar that can generate a huge number of possible matcher expressions from a small number of primitive matchers.

These higher-order matcher operations include logical connectives (and, or and not), and operations for iterating over collections. This results in a rich matcher language which allows complex assertions over collections to be written in a declarative style rather than a procedural style.{{citation needed|date=April 2017}}

References

1. ^{{cite web|url=http://code.google.com/p/hamcrest/wiki/Tutorial|title=The Hamcrest Tutorial}}
2. ^{{cite web |title=Summary of Changes in version 4.4 |author=Marc Philipp |date=21 Oct 2012 |work=JUnit documentation |url=https://github.com/junit-team/junit4/blob/master/doc/ReleaseNotes4.4.md#summary-of-changes-in-version-44 |accessdate=20 Sep 2016}}
3. ^{{cite web |title=JUnit 5 User Guide - Third-party Assertion Libraries|url=https://junit.org/junit5/docs/5.0.0/user-guide/#writing-tests-assertions-third-party |accessdate=11 May 2018}}
4. ^{{cite web|url=https://github.com/nhamcrest/NHamcrest|title=NHamcrest}}
5. ^{{cite web|url=https://pypi.python.org/pypi/PyHamcrest|title=PyHamcrest}}
6. ^{{cite web|url=https://github.com/drewbourne/hamcrest-as3/tree/master|title=Hamcrest-AS3 }}
7. ^{{cite web|url=http://code.google.com/p/hamcrest/|title=Hamcrest - Google Code}}
8. ^{{cite web|url=https://github.com/abe33/hamcrest4qunit|title=Hamcrest4Qunit}}
9. ^{{cite web|url=https://github.com/hyperthunk/hamcrest-erlang#readme|title=Hamcrest Erlang}}
10. ^{{cite web|url=http://docs.renjin.org/en/latest/writing-renjin-extensions.html#using-the-hamcrest-package-to-write-unit-tests|title=Hamcrest for Renjin}}
11. ^{{cite web|url=https://crates.io/crates/hamcrest/|title=Hamcrest Cargo package}}
12. ^{{Cite web|url=https://github.com/nschum/SwiftHamcrest|title=Hamcrest for Swift|last=|first=|date=|website=|archive-url=|archive-date=|dead-url=|access-date=}}

External links

  • {{Official website|http://hamcrest.org/}}
{{programming-software-stub}}

2 : Java platform software|Unit testing frameworks

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/11 16:38:55