2. What is test?
Test – code that verifies your code.
Test – specification that tells what should be expected.
Expected
result
Coffee beans
Water
Coffee
Input
parameters
Your code,
some logic
X
Y
Z
2
3. Why to write tests?
• Easy project development and maintenance
• High code reliability
• Testing automation
• Safe code refactoring
• High quality code
3
4. What to test?
• Test your code
• Test everything that could possibly break
• Test common and edge cases
• Write test that covers the code with bug before fixing it
4
5. Recommended name conventions
A separate project for tests: [module].Tests.csproj
A class with tests for only one target class: public class [target_class]Tests
Test’s name shows what test verifies and what is expected:
public void [target_method]_When_..._Then_...()
public void [target_method]_Should_..._When_...()
Conditions
Expected result
AAA pattern: Arrange-Act-Assert
5
7. Test should be…
• easy to write
• simple to understand
• small
• easy to run and automate
• independent and all-sufficient
• ready to run in parallel… ideally
7
9. Testable code
9
• SOLID principle
• each unit does one concrete task
• loosely coupled units
• Small understandable units
• Dependency Injection (DI) pattern
• Only few dependencies
• Logic is separated from the data resources
• Interfaces
13. Test coverage
Shows what code is executed in tests.
Popular tools:
• JetBrains dotCover
• Built-in tool in Visual Studio Premium / Ultimate
• NCover
• OpenCover (free)
• NCrunch
13
14. Continuous Integration
CI servers automate tests execution and
notify developers about failures.
Demo on https://teamcity.jetbrains.com/?guest=1
14
15. Behavior Driven Development (BDD)
15
BDD
TDD Acceptance tests
In contrast to TDD, BDD provides a specification what should be tested.
Given-When-Then (GWT) pattern:
Given some context
When some action is carried out
Then a particular set of observable consequences should obtain
Popular tools:
• SpecFlow
• NSpec
16. Homework
16
Repository with lecture presentation and code examples:
https://github.com/BinaryStudioAcademy/NET_CodeTesting
Tasks: Homework.txt in the root of repository.
Read README.txt files in the projects for help.