a piece of code that test a piece of production code.
characteristics:
- are stored on disk along with a version control system.
- should be easy and fast to run.
- should test software components in isolation.
- should also run isolated - no test should depend on another test
- should run simultaneously and in any order.
- when the implementation (feature) is complete, to verify its correct behavior.
- when the implementation changes, to verify its behavior is intact.
- when new units are added to the system, to verify it still fulfills its intended purpose.
is a term to refer to test frameworks that are based on ideas and concepts or derive their structure on the Smalltalk's SUnit, designed by Kent Beck in 1988.
is a predicate that states the programmer's intended state of a system.
Red is used for "failure"
Green is used for "success"
are used to organize the tests to get a more fine-grained feedback
should exercise only one unit, but it may do so using one or more assertions
are required only to test one specific behavior of a single unit
a set of related test functions/methods
test cases are usually organized in test suites in more complex systems
test suites
>test cases
>test functions
centralized setup of test data
are handled by the following methods:
setUp
akabefore
orbeforeEach
tearDown
akaafter
orafterEach
test the sum of its parts (unit tests, two or more individual components)
writing tests is an investment
testing applications takes time
automated testing allows us to write a test once and run it as many times as we wish
by "trapping" a bug in a test, our test suite will notify us if the bug ever makes a reappearance.
we can run all our tests prior to pushing code into production to make sure that past mistakes stay in the past.
is vital to growing your application while preserving a good design.
you've done it by:
- Reusing code/methods. DRY (Don't Repeat Yourself)
- Renaming objects or functions
first step while refactoring:
“Build a solid set of tests for the section of code to be changed.” - Martin Fowler
without tests you have no reliable metric that can tell you whether or not the refactoring was successful, and that new bugs weren’t introduced:
“don’t touch anything that doesn’t have coverage. Otherwise, you’re not refactoring; you’re just changing shit.” - Hamlet D’Arcy
make use of a
test runner
to automate tests across multiple user agents aka browsers.
serve as good documentation of the underlying interfaces.
new developers can get to know the system being developed better
writing tests is NOT always easy
in order to write truly great unit tests, the code you’re testing needs to be testable.