Caleb Jenkins discusses best practices for writing automated unit tests, including having a test runner, setting the test context or scene, and handling dependencies through techniques like dependency injection and mocking. He advocates writing tests first to define requirements and ensure code meets expectations. Jenkins also addresses challenges with testing edges or interfaces and advocates separating UI/data logic from edges to increase testability.
5. Development Engineering Process
Object Orientation Automated Tests Agile, Lean, XP
SOLID Source Control Team Dynamics
Patterns Automated Builds Continuous Learning
Secure Coding
14. Do you write after the fact Tests
or design with executable
specifications?
A test confirms what was done
blue prints or specifications
define what needs to be done.
Automated Unit Tests do both!
15. What was
What
should be
Discuss: done
done
Building Inspector
vs
Architect Designer
Which are you?
Which do you need?
What’s the difference?
Unit Tests
do both
18. your application
Code
what you want to test
Tests
Test Framework Test Runner
18
19. your test code
the code that tests the
Code Tests
code that you wrote or are
going to write
Test Framework Test Runner
19
20. Code Tests
attributes and asserts
the framework provides
the attributes and asserts
Test Framework
so we know what the tests Test Runner
are doing.
Examples: nUnit jUnit
cppUnit
20
21. Code Tests
runs the tests
often associated with the
test framework; is
Test Framework Test Runner
distinctly separate.
sometime integrated in
IDE, CI Server or stand
alone exe
21
33. Test Code … 3 A’s
Arrange Act Assert
Arrange– Set up the scenario and the initial input values.
Often in a common [TestFixtureSetup] or [Setup] method
Act - Action that creates the outcome that is being tests, usually
calling some method in your code to test the result.
Assert – Is a boolean statement to your testing framework to
declare the expected outcome. Results in Pass or Fail
34
34. Building better Lego’s
UI
UI Logic Tests are small
App Domain Domain Validation
Tests are fast
Integration Service Proxy Tests focus on one thing
Data Logic
Data Access
35. Simple Tests (return values) with no dependencies…
Test Runner
Test Code
UI Logic
App Domain Domain Validation
Unit Tests focus on a Unit
Test a unit in isolation from other units
Control input => Testable output
45. Dependencies
“The single greatest thing that you can do
to make your code more testable and
healthy is to start taking a Dependency
Injection approach to writing software”
Real World .NET, C# and Silverlight
Caleb Jenkins (Wrox Press 2012)
46. How do you test this
UI
UI Logic
App Domain Domain Validation
Integration Service Proxy
Data Logic with these
Data Access dependencies
47. UI
UI Logic
App Domain Domain Validation
Integration Service Proxy
Data Logic
Data Access
48. Test Runner
Test Code
UI Logic
App Domain Domain Validation
Integration Service Proxy
Dependency Injection + Interfaces
Faked dependencies to increase unit isolation
Leverage mocking frameworks makes life better
49. Note:
Dependency Injection
will turn you in to a complete
coding Ninja, however the
full scope of DI with any of
the many DI frameworks is
beyond the scope of this talk
51. Mocking Framework
“A mocking framework allows you to create fake classes on
the fly in-line with your test code. That is a bit of a
simplification, mocking frameworks use a combination of
emits, reflection and generics to create run-time instance
implementations of .NET Interfaces – whew, that’s a mouthful
- it’s a whole lot easier to say that they create fake classes on
the fly!”
Real World .NET, C# and Silverlight
Caleb Jenkins (Wrox Press 2012)
65. Edges are
Hard to Test
UI UI Data
Business Data
Logic
Logic
66. Edges are still
Edges are
Hard to Test
Data
UI Data
UI
Business
Logic Logic
by separating UI/Data edges from
UI/Data logic we’re increasing the testable area
67. Edges are still
Edges are
Hard to Test
Data
UI Data
UI
Business
Logic Logic
we’ve also made it easier to implement
by separating UI/Data edges from
various UI and Data platforms
UI/Data logic we’re increasing the testable area
without affecting the application logic
70. Model View Controller (MVC)
• All input is routed to a controller
• Example Web Scenarios
• ASP.NET MVC Framework
Model View Presenter (MVP)
• View initiates Presenter
• UI Logic is contained in Presenter
• Example WinApp & ASP.NET Webform apps
Model View ViewModel (MVVM)
• ViewModel is a view specific model
• VM is can mash up application models
• UI logic contained in ViewModel
• Example Rich Data binding Scenarios
(WPF / Silverlight)
74
71. Application
UI Layer
Business Layer
Data Layer (IRepository)
Data Logic
IRepository
Repository handles the CRUD and “bare metal” Data Interactions.
Consider ADO.NET, nHibernate, EF, LinqToSQL
Data Base 76
74. 1 Have a runner
2 Set the Scene
3 Handle your
Dependencies
76. Development Team Work Process
Object Orientation Automated Tests Agile, Lean, XP
SOLID Source Control Team Dynamics
Patterns Automated Builds Continuous Learning
Secure Coding