20
@DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

Embed Size (px)

Citation preview

Page 1: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

Effective Unit Testing for DNNJames McKee

Solutions Developer / Enterprise Architect

@punkcoder

Page 2: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

THANKS TO ALL OF OUR GENEROUS SPONSORS!

Page 3: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• Almost 10 years working .NET• Consultant 7 years

• 7 Companies in the Fortune 100• 10 total in the Fortune 500

• Currently with BlueBolt Solutions• Enterprise Architect for Bravo-Squared

Software• Run Punk-Coder.com• New to the DNN Scene

About Me

Page 4: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

So to gauge the audience:• Know what unit tests are?• Realize that unit tests are something

that you write not something you do?

• Think that they are a great idea but something that you don’t have time for?

Questions

Page 5: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• Unit Tests are hard• Unit Tests are for people who don’t know

how to program.• My project isn’t big enough to need unit

tests.• I’m the only one working on my project.• My client doesn’t pay for unit tests.• We are on a compressed time line, we

don’t have time for unit tests.

Reasons People Don’t Unit Test

Page 6: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

Why Unit Testing is Important

More Infromation: https://blogs.msdn.com/b/dotnet/archive/2011/09/26/compatibility-of-net-framework-4-5.aspx?Redirected=truehttps://blogs.msdn.com/b/dotnet/archive/2012/10/17/net-framework-4-5-off-to-a-great-start.aspx?Redirected=true

Page 7: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• Unit Test as a function of code• Unit Test vs. Functional Test

What is a unit test?

Page 8: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

This is a Unit Test

Page 9: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

DEMO ! (HELLO WORLD!)

Page 10: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

Major implementations should interact through the veil of an interface.• Programming to an interface improves

modularity.• It encourages looser coupling.• Mocking classes can get complicated.

It’s just good design!

Interface based programming

Page 11: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

An artificial object that mimics the behavior of a complicated class for the purpose of simplifying testing.

Example:• You would mock the following in your

tests:• Database Calls• File System Calls• Web Service Calls

Mocks

Page 12: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• Many of the interaction points for modules and scheduled tasks are complicated and do not supply Interfaces.

• Functionality such as database access requires instantiation of classes originating from DNN.

Unit Testing in DNN

Page 13: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• Ninject is a lightweight, full featured IoC container for use with dependency injection.

• Open Source• Available at http://www.ninject.org/

Ninject to the Rescue

Page 14: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• To make the application more testable we will create 2 Standard Kernels that will be responsible for filling dependencies:

• In production code we will use the real kernel

• In testing we will use the testing kernel to return mock objects.

• In scheduled tasks this will be inserted at the beginning of custom code.

• In views the presenter will be instantiated from Kernel

Building Interfaces at Boundaries

Page 15: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• Because of state some objects don’t work well with direct instantiation.

• DataContext is a good example. Reusing a data context for multiple calls can result in a time out, that leads to exception, which leads to GC.

• To avoid this create factory classes that produce instance classes.

Building Factories for Instance Objects

Page 16: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

Demo! The Skeleton

Page 17: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• NUnit• xUnit, spiritual successor to NUnit• MBUnit• Visual Studio Built-In

• Suffers from problems related testing x86/x64 in the same project.

• Integrates well with performance testing and can use unit tests for load testing

Unit Testing Frameworks and Tools

Page 18: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• Build automation• Code Coverage• Performance testing

Where to go from here

Page 19: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

• When it comes to unit testing, there is a very fine line between code reuse and masking issues.

• If possible write your code as test driven, this will produce leaner code.

• Aim for the 90% mark, but 80% is the accepted boundary.

• Use asserts carefully, make sure that what you are asserting is the actual correct behavior.

Best Practices

Page 20: @DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise Architect @punkcoder

@DNNConDon’t forget to include #DNNCon in your tweets!

Twitter: @punkcoderemail: [email protected] work: [email protected]

Contact Me