CSCE 747 Software Testing and Quality Assurance Tools
- Slides: 48
CSCE 747 Software Testing and Quality Assurance Tools 12 – Hamcrest 10/02/2013 Tools - Hamcrest 1 CSCE 747 Fall 2013 1
What is Hamcrest? § http: //hamcrest. org/ § http: //code. google. com/p/hamcrest/wiki/Tutorial § http: //junit. sourceforge. net/doc/cookbook/cookbo ok. htm § http: //hamcrest. org/Java. Hamcrest/javadoc/1. 3/ Tools - Hamcrest 2 http: //en. wikipedia. org/wiki/Mockito CSCE 747 Fall 2013
Tools - Hamcrest 3 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
§ Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. § There a number of situations where matchers are invaluble, such as UI validation, or data filtering, § but it is in the area of writing flexible tests that matchers are most commonly used. Tools - Hamcrest 4 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
My first Hamcrest test import static org. hamcrest. Matcher. Assert. assert. That; import static org. hamcrest. Matchers. *; import junit. framework. Test. Case; public class Biscuit. Test extends Test. Case { public void test. Equals() { Biscuit the. Biscuit = new Biscuit("Ginger"); Biscuit my. Biscuit = new Biscuit("Ginger"); assert. That(the. Biscuit, equal. To(my. Biscuit)); } } // Note JUnit 3 Tools - Hamcrest 5 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
§ If you have more than one assertion in your test you can include an identifier for the tested value in the assertion: assert. That("chocolate chips", the. Biscuit. get. Chocolate. Chip. Count(), equal. To(10)); assert. That("hazelnuts", the. Biscuit. get. Hazelnut. Count(), equal. To(3)); Tools - Hamcrest 6 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
Hamcrest Common Matchers § A tour of Hamcrest comes with a library of useful matchers. Here are some of the most important ones. § Core § anything - always matches, useful if you don't care what the object under test is § described. As - decorator to adding custom failure description § is - decorator to improve readability - see "Sugar", below § Logical § all. Of - matches if all matchers match, short circuits (like Java &&) § any. Of - matches if any matchers match, short circuits (like Java ||) § not - matches if the wrapped matcher doesn't match and vice versa Tools - Hamcrest 7 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
§ Object § § § equal. To - test object equality using Object. equals has. To. String - test Object. to. String instance. Of, is. Compatible. Type - test type not. Null. Value, null. Value - test for null same. Instance - test object identity § Beans § has. Property - test Java. Beans properties Tools - Hamcrest 8 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
§ Collections § array - test an array's elements against an array of matchers § has. Entry, has. Key, has. Value - test a map contains an entry, key or value § has. Item, has. Items - test a collection contains elements § has. Item. In. Array - test an array contains an element § Number § close. To - test floating point values are close to a given value § greater. Than, greater. Than. Or. Equal. To, less. Than, less. Than. Or. Equal. To - test ordering Tools - Hamcrest 9 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
§ Text § equal. To. Ignoring. Case - test string equality ignoring case § equal. To. Ignoring. White. Space - test string equality ignoring differences in runs of whitespace § contains. String, ends. With, starts. With - test string matching Tools - Hamcrest 10 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
Syntactic Sugar § Hamcrest strives to make your tests as readable as possible. § For example, the “is” matcher is a wrapper that doesn't add any extra behavior to the underlying matcher. , but increases readability. § The following assertions are all equivalent: § assert. That(the. Biscuit, equal. To(my. Biscuit)); § assert. That(the. Biscuit, is(equal. To(my. Biscuit))); § assert. That(the. Biscuit, is(my. Biscuit)); § The last form is allowed since is(T value) is overloaded to return is(equal. To(value)). Tools - Hamcrest 11 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
Writing custom matchers § matcher for testing if a double value has the value Na. N (not a number). This is the test we want to write: § public void test. Square. Root. Of. Minus. One. Is. Not. ANumber() { assert. That(Math. sqrt(-1), is(not. ANumber())); } Tools - Hamcrest 12 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
is. Not. ANumber Implementation package org. hamcrest. examples. tutorial; import org. hamcrest. Description; import org. hamcrest. Factory; import org. hamcrest. Matcher; import org. hamcrest. Type. Safe. Matcher; Tools - Hamcrest 13 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
public class Is. Not. ANumber extends Type. Safe. Matcher<Double> { @Override public boolean matches. Safely(Double number) { return number. is. Na. N(); } public void describe. To(Description description) { description. append. Text("not a number"); } @Factory public static <T> Matcher<Double> not. ANumber() { return new Is. Not. ANumber(); } } Tools - Hamcrest 14 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
assert. That(1. 0, is(not. ANumber())); § fails with the message java. lang. Assertion. Error: Expected: is not a number got : <1. 0> Tools - Hamcrest 15 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
Sugar generation 1. create an XML configuration file listing all the Matcher classes <matchers> <!-- Hamcrest library --> <factory class="org. hamcrest. core. Is"/> <!-- Custom extension --> <factory class= "org. hamcrest. examples. tutorial. Is. Not. ANumber"/> </matchers> Tools - Hamcrest 16 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
2. Run the org. hamcrest. generator. config. Xml. Configur ator command-line tool that comes with Hamcrest Tools - Hamcrest 17 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
// Generated source package org. hamcrest. examples. tutorial; public class Matchers { public static <T> org. hamcrest. Matcher<T> is(T param 1) { return org. hamcrest. core. Is. is(param 1); } public static <T> org. hamcrest. Matcher<T> is(java. lang. Class<T> param 1) { return org. hamcrest. core. Is. is(param 1); } public static <T> org. hamcrest. Matcher<T> is(org. hamcrest. Matcher<T> param 1) { return org. hamcrest. core. Is. is(param 1); } public static <T> org. hamcrest. Matcher<java. lang. Double> not. ANumber() { return org. hamcrest. examples. tutorial. Is. Not. ANumber. not. ANumber(); } } - Hamcrest 18 Tools http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
update our test to use the new Matchers class import static org. hamcrest. Matcher. Assert. assert. That; import static org. hamcrest. examples. tutorial. Matchers. *; import junit. framework. Test. Case; public class Custom. Sugar. Number. Test extends Test. Case { public void test. Square. Root. Of. Minus. One. Is. Not. ANumber() { assert. That(Math. sqrt(-1), is(not. ANumber())); } } Tools - Hamcrest 19 http: //code. google. com/p/hamcrest/wiki/Tutorial CSCE 747 Fall 2013
Tools - Hamcrest 20 CSCE 747 Fall 2013
Tools - Hamcrest 21 CSCE 747 Fall 2013
import static org. junit. Assert. *; import static org. hamcrest. Core. Matchers. *; public class Junit 4 Features. Test { @org. junit. Test public void test. Arrays() { assert. That(new Integer[] {1, 2, 3, 4, 5}, equal. To(new Integer[]{1, 2, 3, 4, 5})); assert. That(new Integer[] {1, 2, 3, 4, 5}, not(new Integer[] {1, 2})); } } Tools - Hamcrest 22 http: //www. acgavin. com/hamcrest-and-junit 4 -4 CSCE 747 Fall 2013
import static org. junit. Assert. *; import static org. hamcrest. Matchers. *; // provided by additional jar import org. junit. Test; public class Junit 4 Features. Test { @Test public void test. Numeric. Comparison() { assert. That(1, less. Than(2)); assert. That(2, greater. Than(1)); assert. That(2, greater. Than. Or. Equal. To(2)); assert. That(1, less. Than. Or. Equal. To(1)); Tools - Hamcrest 23 http: //www. acgavin. com/hamcrest-and-junit 4 -4 CSCE 747 Fall 2013
Hamcrest API Tools - Hamcrest 24 http: //junit. org/javadoc/4. 10/org/hamcrest/Matcher. html CSCE 747 Fall 2013
http: //hamcrest. org/Java. Hamcrest/javadoc/1. 3/ § org. hamcrest Interface Matcher<T> § All Superinterfaces: Self. Describing § All Known Implementing Classes: All. Of, Any. Of, Base. Matcher, Big. Decimal. Close. To, Combinable. Matcher, Custom. Matcher, Custom. Type. Safe. Matcher, Described. As, Diagnosing. Matcher, Every, Feature. Matcher, Has. Property. With. Value, Has. To. String, Has. XPath, Is, Is. Anything, Is. Array. Containing, Is. Array. Containing. In. Any. Order, Is. Array. Containing. In. Order, Is. Array. With. Size, Is. Close. To, Is. Collection. Containing, Is. Collection. With. Size, Is. Compatible. Type, Is. Empty. Collection, Is. Empty. Iterable, Is. Empty. String, Is. Equal. Ignoring. Case, Is. Equal. Ignoring. White. Space, Is. Event. From, Is. Instance. Of, Is. Iterable. Containing. In. Any. Order, Is. Iterable. Containing. In. Order, Is. Iterable. With. Size, Is. Map. Containing, Is. Not, Is. Null, Is. Same, Ordering. Comparison, Same. Property. Values. As, Same. Property. Values. As. Property. Matcher, String. Contains. In. Order, String. Ends. With, String. Starts. With, Substring. Matcher, Type. Safe. Diagnosing. Matcher, Type. Safe. Matcher Tools - Hamcrest 25 CSCE 747 Fall 2013
Tools - Hamcrest 26 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
The Dilemma § One of the dilemmas posed by the move to shorter and shorter release cycles is how to release more software in less time— and continue releasing indefinitely. § Kent Beck Tools - Hamcrest 27 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
What if? § What if instead, we treated software more like a valuable, productive plant, to be nurtured, pruned, harvested, fertilized, and watered? § Traditional farmers know how to keep plants productive for decades or even centuries. § How would software development be different if we treated our programs the same way? Tools - Hamcrest 28 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
§ “A complex system that works is invariably found to have evolved from a simple system that works. ” § John Gall 2003 Tools - Hamcrest 29 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Growing Objects § Alan Kay’s concept of objects being similar to biological cells that send each other messages. § Alan Kay was one of the authors of Smalltalk and coined the term “object-oriented. ” Tools - Hamcrest 30 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Test Driven Development (TDD) § Test-Driven Development (TDD) is a deceptively simple : § Write the tests for your code before writing the code itself. § it transforms the role testing plays in the development process § Testing is no longer just about keeping defects from the users; § instead, it’s about helping the team to understand the features that the users need § and to deliver those features reliably and predictably. § TDD § radically changes the way we develop software § dramatically improves the quality of the systems Tools - Hamcrest 31 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
What Is the Point of Test-Driven Development? § One must learn by doing the thing; for though you think you know it, you have no certainty, until you try. —Sophocles § Software Development as a Learning Process § developers learn § customers learn - codify business rules/goals § Feedback Is the Fundamental Tools - Hamcrest 32 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Test-Driven Development in a Nutshell Tools - Hamcrest 33 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
§ Writing tests: § makes us clarify the acceptance criteria for the system § encourages us to write loosely coupled components, so they can easily be tested in isolation and, at higher levels, combined together § adds an executable description of what the code does § adds to a complete regression suite § whereas running tests: § detects errors while the context is fresh in our mind § lets us know when we’ve done enough, discouraging “gold plating” and unnecessary features (design). Tools - Hamcrest 34 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Golden Rule of Test-Driven Development § Never write new functionality without a failing test. Tools - Hamcrest 35 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Refactoring. Think Local, Act Local § “Refactoring means changing the internal structure of an existing body of code without changing its behavior. § The point is to improve the code so that it’s a better representation of the features it implements, making it more maintainable. Tools - Hamcrest 36 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Tools - Hamcrest 37 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Coupling and Cohesion § Coupling and cohesion are metrics that (roughly) describe how easy it will be to change the behavior of some code. § They were described by Larry Constantine in [Yourdon 79]. § Elements are coupled if a change in one forces a change in Tools - Hamcrest 38 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Coupling § Elements are coupled if a change in one forces a change in the other § For example, if two classes inherit from a common parent, then a change in one class might require a change in the other. Tools - Hamcrest 39 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Cohesion § Cohesion is a measure of whether its responsibilities form a meaningful unit. § For example, a class that parses both dates and URLs is not coherent, because they’re unrelated concepts. Tools - Hamcrest 40 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Object Oriented § Music is the space between the notes. — Claude Debussy § The big idea is “messaging” [. . . ] The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be. - Alan Kay [1998] Tools - Hamcrest 41 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
§ An object communicates by messages: § An object-oriented system is a web of collaborating objects. § A system is built by creating objects and plugging them together so that they can send messages to one another. Tools - Hamcrest 42 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
§ Man is a tool-using animal. Without tools he is nothing, with tools he is all. —Thomas Carlyle Tools - Hamcrest 43 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
NUnit Behaves Differently from JUnit §. Net should note that NUnit reuses the same instance of the test object for all the test methods, § so any values that might change must either be reset in [Setup] and [Tear. Down] methods (if they’re fields) or made local to the test method. Tools - Hamcrest 44 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Test Fixtures § A test fixture is the fixed state that exists at the start of a test. § A test fixture ensures that a test is repeatable — § every time a test is run it starts in the same state so it should produce the same results. Tools - Hamcrest 45 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Test Fixture Example public class Catalog. Test { final Catalog catalog = new Catalog(); final Entry entry = new Entry(" fish", "chips"); @ Before public void fill. The. Catalog(){ catalog. add( entry); } @ Test public void contains. An. Added. Entry() { assert. True( catalog. contains( entry)); } @ Test public void indexes. Entries. By. Name() { assert. Equals( equal. To( entry), catalog. entry. For(" fish")); Tools - Hamcrest 46 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
assert. Null( catalog. entry. For(" missing name")); } @ Test( expected = Illegal. Argument. Exception. class) public void cannot. Add. Two. Entries. With. The. Same. Name() { catalog. add( new Entry(" fish", "peas"); } } Tools - Hamcrest 47 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
Tools - Hamcrest 48 Freeman, Steve; Pryce, Nat (2009 -10 -12). Growing Object. Oriented Software, Guided by Tests CSCE 747 Fall 2013
- Software testing and quality assurance theory and practice
- The quality revolution
- Theory of goodenough and gerhart
- Software testing and quality assurance theory and practice
- Quality assurance theory
- Software quality assurance tools and techniques
- Quality assurance vs quality control
- Quality assurance vs quality control
- Qat quality assurance testing
- Physics book
- Pmp quality management
- What are quality standards in project management
- Quality assurance model in nursing management
- Compliance vs quality
- Project scheduling and tracking software quality assurance
- Mr brown receives a salary
- 11-747 neural networks for nlp
- Oceanic airlines 777
- Arinc 429 ethernet converter
- Classification of software metrics
- Software quality assurance iso standards
- Quality assurance plan in software engineering
- Software quality assurance challenges
- Maik nogens
- Software quality assurance notes
- Software quality assurance models
- Statistical quality assurance in software engineering
- Statistical software quality assurance
- Software quality assurance is an umbrella activity
- Sqa checklist
- Introduction to software quality assurance
- The isle quality assurance
- Software quality assurance slideshare
- Uniqueness of software quality assurance
- Software quality assurance agency
- Software quality assurance agency uk
- Case tools and their effect on software quality
- Domain closure in software testing
- Logic based testing in software testing
- Du path testing
- Globalization testing example
- Decision table testing in software testing
- Control structure testing in software testing
- Decision table testing in software testing
- Decision table testing
- Decision table testing
- Rigorous testing in software testing
- Testing blindness in software testing
- Types of domain testing