Dan Solovay - Test Driven Sitecore - SUGCON
- 1. Organized by the Community, for the Community.
TEST DRIVEN
SITECORE
Dan Solovay
Sitecore MVP
Lead Architect at XCentium
- 2. Organized by the Community, for the Community.
OVERVIEW
• What isTDD?
• Unit or Integration tests?
• Tools
• Demo of Sitecore IntegrationTesting
• Demo of Sitecore.FakeDB
• Demo of Glass.Mapper + Autofac
• How to get started withTDD?
SUGCON NORTH AMERICA
2015
2
- 3. Organized by the Community, for the Community.
MORE ISOLATION
Sitecore
Integration
Testing
Sitecore.FakeDB
Glass.Mapper +
Autofac
SUGCON NORTH AMERICA
2015
3
- 4. Organized by the Community, for the Community.
MORE FLEXIBILITY
SUGCON NORTH AMERICA
2015
4
- 5. Organized by the Community, for the Community.
SUGCON NORTH AMERICA
2015
5
TEST DRIVEN
DEVELOPMENT
- 6. Organized by the Community, for the Community.
AGILE ORIGINS OF TDD
• Rediscovered by Kent Beck
• Extreme Programming
– Embrace change through
continuous feedback
• Popularized by Robert
Martin
– Author of SOLID principles
– “What if your code always
worked?”
SUGCON NORTH AMERICA
2015
6
- 7. Organized by the Community, for the Community.
THE TDD CYCLE
• RED
– Right just enough code to fail
• GREEN
– Right just enough code to pass
• REFACTOR
– Improve design when everything is working
SUGCON NORTH AMERICA
2015
7
- 8. Organized by the Community, for the Community.
SUGCON NORTH AMERICA
2015
8
UNIT AND
INTEGRATION
TESTING
- 9. Organized by the Community, for the Community.
WHAT IS A UNIT TEST?
• Automated
• Full control / runs in
memory
• Consistent & fast
• Tests a single logical
concept
• Readable &
maintainable
• Trustworthy
SUGCON NORTH AMERICA
2015
9
Roy Osherove,
Art of UnitTesting
- 10. Organized by the Community, for the Community.
INTEGRATION TESTS
• More fragile
• Provide full system confidence
• Shows different systems working together
SUGCON NORTH AMERICA
2015
10
- 11. Organized by the Community, for the Community.
SUGCON NORTH AMERICA
2015
11
TOOLS
- 12. Organized by the Community, for the Community.
MYTOOLBOX
• XUNIT
– Better separation of tests than NUNIT.
• NCRUNCH
– Runs tests as you type.
• NSUBSTITUTE
– Creates fake versions of interfaces in tests.
• AUTOFAC
– Creates real versions in production.
SUGCON NORTH AMERICA
2015
12
- 13. Organized by the Community, for the Community.
SUGCON NORTH AMERICA
2015
13
TESTING SITECORE
- 14. Organized by the Community, for the Community.
SITECORE INTEGRATION TESTING
• Browser based
– More robust
– Http Context available
• Class Library based
– Works withTest Runners
– Easier to automate
SUGCON NORTH AMERICA
2015
14
- 15. Organized by the Community, for the Community.
SITECORE FAKE DB
• Developed by Sergey Shusliapin of Sitecore
Ukraine
• Uses Sitecore DLLs, replaces data layer
• Tests Sitecore Items directly
• No code changes required!
SUGCON NORTH AMERICA
2015
15
- 16. Organized by the Community, for the Community.
MAP SITECORE ITEMS
• Use object mapper like Glass
• Use Dependency Injector like Autofac
• Your code does not talk directly to Sitecore
SUGCON NORTH AMERICA
2015
16
- 17. Organized by the Community, for the Community.
SUGCON NORTH AMERICA
2015
17
NEXT STEPS
- 18. Organized by the Community, for the Community.
BENEFITS
• Encourages focus
• Creates confidence
• Drives good design
SUGCON NORTH AMERICA
2015
18
- 19. Organized by the Community, for the Community.
CHALLENGES
• Hard to learn new habits
• Team buy in
• Finding time
SUGCON NORTH AMERICA
2015
19
- 20. Organized by the Community, for the Community.
SOLUTIONS
• Avoid all-or-nothing thinking
– One, two hours ofTDD per week
• Get involvement
– Pair up, ask for input
– Run aTDD Kata session
• Sell the value
– Fast feedback
– Flexible design
SUGCON NORTH AMERICA
2015
20
- 21. Organized by the Community, for the Community.
FEARLESS
If we have the tests we become fearless about
making changes. If we see messy code, or an
unclean structure, we can clean it without fear.
Because of the tests, the code becomes
malleable again. Because of the tests, software
becomes soft again.
– Robert Martin, bit.ly/tdd3rules
SUGCON NORTH AMERICA
2015
21
- 22. Organized by the Community, for the Community.
SUGCON NORTH AMERICA
2015
Thanks!
@DanSolovay
bit.ly/tddlinks
Editor's Notes
- Okay, let’s see how these concepts apply to Sitecore. First, let’s look at tests that actually execute logic against the Sitecore database, so Sitecore integration tests.