Karteikarten
Chapter 5
Karten in diesem Stapel (48)
- 1idea of unit testingto test a small part of the code in isolation. if a test fails, it is easy to know where
to look for problems. - 2unit test definition, UUT, oracleA unit test consists of one or more executions of a unit under test (UUT) with
some input data and the comparison of the result of the execution against some
oracle. A UUT is whatever piece of the code we wish to test in isolation. UUTs
are often methods, but in some cases they can also be entire classes, initialization
statements, or certain paths through the code. The term oracle designates the correct
or expected result of the execution of a UUT. - 3The comparison of the result of executing the UUT with the oracle is also called...an assertion
- 4When testing instance methods, it is important to remember...that the input data
includes the implicit argument (the instance that receives the method call). - 5regression testing definitionRunning tests to ensure that whatwas tested as correct still (or for detecting new bugs caused by changes)
- 6what is JUnit?a unit testing framework.
unit testing frameworks are collecting tests, running them, and reporting the results. In addition to tools to collect and run tests and display the results, frameworks also include a set of constructs to
allow developers to write tests in a structured way and (if they so choose) efficiently. - 7what are the major constructs supported by testing frameworkstest cases, test suites, test
fixtures and assertions. - 8what does @Test do?The @Test annotation instance indicates that the annotated method should be run
as a unit test. - 9what are assert methods?They are declared as static methods
of the class org.junit.jupiter.api.Assertions and all they do is verify
a predicate and, if the predicate is false, report a test failure - 10test suite definitionA collection of tests for a project is known as a test suite. By default, a project’s test
suite consists of all the unit tests for the production code in the project.
JUnit provides a @Suite construct that allows a developer to list a number of test classes to be
executed together. - 11how to organise tests?have one test class per project class, where the test class collects all the
tests that test methods or other usage scenarios that involve the class.
locate all the testing code in a different source folder with
a package structure that mirrors the package structure of the production code. - 12idea of metaprogrammingto write code that operates on a
- 13in Java, metaprogramming...In Java, metaprogramming is called reflection and library support for metaprogramming features is available through the class
java.lang.Class and the package java.lang.reflect. - 14what is the most basic metaprogramming task?to obtain a reference to an object that
represents a piece of code to learn about it, a procedure called introspection. In Java,
the class Class is the main access point for metaprogramming. - 15how do you use type wildcard and forName library method?public static void main(String[] args) {
try {
Class theClass = Class.forName(args[0]);
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
} - 16Besides the forName library method, Java offers two other ways to obtain a reference to an instance of class Class that are less brittle:class literals, and through
an instance of the class of interest. - 17what is a class literal?a class literal is a literal expression
that consists of the name of a class followed by the suffix .class, and that refers to a reference to an instance of class Class that represents the class named before the suffix. So, Card.class refers to the instance of class
that represents class Card.
Class cardClass1 = Card.class; - 18how do you obtain an instance of class Class is through an instance of the class?Card card = Card.get(Rank.ACE, Suit.CLUBS);
Class cardClass2 = card.getClass(); - 19what does this show? System.out.println(cardClass1 == cardClass2);a very important property of class
Class: its instances are unique. If executed, the code should always
print true on the console. Indeed, class Class has no accessible constructor, and its
instances can be considered to be unique flyweight objects. - 20what is the next step after obtaining an instance of class Class?The
interface to class Class provides numerous methods that can be called to obtain objects
that represent the members of the class, its superclass, etc - 21examples of library methods of interface Class:- This example makes use of class Method, a library
class intended to represent methods in Java code. Similar classes exist to represent
constructors (Constructor) and fields (Field).
for( Method method : String.class.getDeclaredMethods() ) {
System.out.println(method.getName());
} - 22motivation for annotationsit is possible to attach additional information (i.e., meta-information) to code elements in the form of annotations
- 23how to declare annotation type- In Java, an **annotation type** is declared similarly to an interface, for example:
public @interface Test {} - 24how to declare annotation instancesin the form @Test
- 25main advantage of annotations in Java:they are typed and checked by the compiler.
The @Test annotation used to flag unit tests in JUnit is thus a type annotation
provided by the JUnit library, and its use is checked by the compiler. - 265 things a unit test should be:fast, independent, repeatable, focused, and readable
- 27fast:whatever test suite is executed should be able to complete in the order of a few seconds.
- 28independent:Each unit test should be able to execute in isolation. This means
that, for example, one test should not depend on the fact that another test executes
before to leave an input object in a certain state. (can execute a single test, faciltiates test suite evolution, order of execution is not predictable) - 29repeatable:The execution of unit tests should produce the same result in different
environments (for example, when executed on different operating systems). - 30focused:Tests should exercise and verify a slice of code execution behavior that
is as narrow as reasonably possible.(helps developpers identify faults). Tests should ideally focus on testing only one aspect of
one unit under test. If that unit under test is a method, we can refer to it as the
focal method for the test. - 31readable:The structure and coding style of the test should make it easy to identify
all the components of the test (unit under test, input data, oracle), as well as
the rationale for the test. Choosing an appropriate name for the test can often help in clarifying its rationale. - 32what is a test fixture?Baseline objects used for testing and declared as fields of a test class
By default, JUnit 5 will instantiate a fresh version of the test
class before running every test method in the class. For this reason, the values of the fields of the test class will contain their initial value when any test executes. - 33when using design by contract, it does not make sense to...test code with input that does not respect the
method’s preconditions, because the resulting behavior is unspecified. - 34With JUnit 5 the standard way to check that a method call raises the expected exception is to use...the assertThrows assert method. This method takes as argument
an instance of class Class that represents the expected exception type, and
an instance of a subtype of the library interface Executable that executes the code
expected to cause the exception. - 35A typical solution when an interface does not include a method that would be convenient for testing is to...provide the desired functionality in the form of a
helper method in the testing class instead. - 36How can we test private methods?Private methods are internal elements of other, accessible methods, and therefore
are not really units that should be tested. Following this logic, the code in private
methods should be tested indirectly through the execution of the accessible
methods that call them; - 37stub motivationThe key to unit testing is to test small parts of the code in isolation. In some cases,
however, factors can make it difficult to test a piece of code in isolation - 38stub definitionA stub is a greatly simplified version of an object that mimics its behavior sufficiently to support the testing of a UUT that uses this object.
- 39test coverage motivationClearly we need to select some input to provide to our test out of all the possibilities. This is a problem
known as test case selection, where test case can be considered to be a set of input
values for an assumed UUT - 40The basic challenge of the test case selection problem is to...test efficiently, meaning to find a minimal set of test cases that provides us a maximal amount of testing for our code
- 41There are two basic ways to approach the selection of test cases:Functional (or black-box) testing tries to cover as much of the specified behavior
of a UUT as possible, based on some external specification of what the UUT
should do.
Structural (or white-box) testing tries to cover as much of the implemented
behavior of the UUT as possible, based on an analysis of the source code of the UUT. The main advantage of white-box testing is that it can reveal problems caused by low-level implementation details that are
invisible at the level of the specification. - 42test coverage metric definitionA test coverage metric is a number (typically a percentage) that determines
how much of the code executes when we run our tests. Test coverage metrics can
be computed by code coverage tools that keep track of the code that gets executed
when we run unit tests. - 43The following are three well-known coverage metrics:statement coverage, branch coverage, and path coverage
- 44explain statement coveragethe simplest coverage metric, but a poor one... the number of statements executed by a test or test suite, divided by the
number of statements in the code of interest. - 45explain branch coveragethe number of program branches (decision points) executed by
the test(s) divided by the total number of branches in the code of interest. In this
context, a branch is one outcome of a condition. stronger metric than statement coverage in the sense that for the same coverage result, more of
the possible program executions will have been tested. - 46bad side of branch coverage:the concept of branch coverage is ambiguous, due to the different possible interpretations of the
term branch. if, boolean expr, return, comparison. - 47explain path coveragethe number of execution
paths actually executed over all possible execution paths in the code of interest.
Path coverage subsumes almost all other coverage metrics, and is a very close approximation
of the entire behavior that is possible to test. - 48bad side of path coveragen many
cases, the number of paths through a piece of code will be unbounded, so it will not
be possible to compute this metric. For this reason, path coverage is considered a
theoretical metric
Informationen
- Beschreibung
- idk Software design Karteikarten am Chapter 5, erstellt von Virginie Gagné am 16/10/2022.
- Erstellt von
- Virginie Gagné
- Aufrufe
- 0
- Bewertung