coding naked-2018 devconnectionsfiles.informatandm.com/uploads/2018/10/coding_naked_unit...microsoft...
TRANSCRIPT
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Coding NakedUnit Testing Those Hard to Reach Places
Caleb JenkinsTechnology LeaderQuicken Loans
U n i t Te s t i n g t h o s e h a r d t o r e a c h p l a c e s !
Coding Naked!
developingUX.comspeakerpedia.com/speakers/caleb-jenkins
@calebjenkins#CodeMash
of our clients would recommend us to others. *
* industry average is 11%
on refi or closing fees, plus work with our exclusive internal team!
rockframework.github.iogithub.com/quickenloans
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Tuesday
Wednesday
Wednesday
Automated Unit Test
Fundamentals
Hard to Reach Places
Coding Naked
back to…
Is TDD only for coding elites?
or something for every developer?
How do we make Automated Tests the norm?
make coding without tests as
uncomfortable as coding naked
* this is not me
15
what are the 4 big parts of unit tests?what are the 4 big parts of unit tests?
Test Framework Test Runner
Code Tests
16
your applicationwhat you want to test
your test codethe code that tests the
code that you wrote or are going to write
attributes and assertsthe framework provides
the attributes and asserts so we know what the tests
are doing. Examples: nUnit jUnit
cppUnit
runs the testsoften associated with the
test framework; is distinctly separate.
sometime integrated in IDE, CI Server or stand
alone exe
Test Runner
17
Code
Test
Fra
me
wo
rk
Tests
Tests
Tests
Tests
Tests
Start
ATest RunnerB
C Test Code
Test Framework
19
Test Framework
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
github.com/tpierrain/NFluent
github.com/erichexter/Should
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
nUnit Test Runner
Test Runners
21
Visual Studio (VS Test)
CodeRush / ReSharperContinuous Integration (Team City)
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
3 A’s
22
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
Arrange Act Assert
Test Code
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Data Access
Data Logic
Integration Service Proxy
App Domain Domain Validation
UI Logic
UI
Building better Lego’s
Tests are small
Tests are fast
Tests focus on one thing
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Red
GreenRefactor
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Test Runner
return valuesno dependencies…
App Domain Domain Validation
UI Logic
Test Code
Unit Tests focus on a UnitTest a unit in isolation from other unitsControl input => Testable output
Simple Tests
VSTest
Start
Test Runner
Test Code
Test Framework
Set the Scene
as a <role>,I will <function>so that <value>
Defining BehaviorDiscussion
what if our “tests”
given <class> to testwhen <setup> scenariothen <test> outcome
matched ourlanguage?
Discussion
Discussion
BDD
Resources & Frameworks
BDDhttp://neelnarayan.blogspot.com/2010/07/bdd-is-more-than-tdd-done-right.html
more than TDD done right
http://dannorth.net/introducing-bdd/
introducing BDD
http://lucisferre.net/2011/02/05/behavior-driven-test-driven-domain-driven-design/
behavior driven, test driven, domain driven
nBehave, nSpec, SpecFlow, StoryQ, mSpec, StorEvil
Handle your dependencies
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”
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 SilverlightWrox Press 2012
Caleb Jenkins
- Real World .NET, C# and SilverlightWrox Press 2012
Caleb Jenkins
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Data Access
Data Logic
Integration Service Proxy
App Domain Domain Validation
UI Logic
UI
How do you test this
with thesedependencies
Harder to Test
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Data Access
Data Logic
Integration Service Proxy
App Domain Domain Validation
UI Logic
UI
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Test Runner
Test Code
Integration Service Proxy
App Domain Domain Validation
UI Logic
Dependency Injection + InterfacesFaked dependencies to increase unit isolationLeverage mocking frameworks makes life better
Note:Dependency InjectionNote: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
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
http://developingUX.com/DI/
http://developingUX.com/DI/
- Real World .NET, C# and SilverlightWrox Press 2012
Caleb Jenkins
- Real World .NET, C# and SilverlightWrox Press 2012
Caleb Jenkins
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!”
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!”
Microsoft.Fakes
Bringing DI togetherBringing DI together
IData mockData = MockRepository.GenerateMock<IData>();
mockData.Expect(x => x.getAll<account>()).Return(sampleAccounts).Repeat.Once();
IAccountServices accountService= new AcmeAccountService(mockData);
var act = accountService.GetAccount(known_account_id);
mockData.VerifyAllExpectations();
Start(Framework, Runner, Test Code)
Set the Scene
Handle your Dependencies
the problem with edges
UI DataBusiness
Edges areHard to Test
Testing edges can be like
testing to see if you’re good
at cliff jumping
That’s not me
..or you’restuff on a rock.
You’re either an expert and it works…
UI DataDataLogic
UILogicBusiness
Edges areHard to Test
Edges are stillHard to Test
by separating UI/Data edges fromUI/Data logic we’re increasing the testable area
we’ve also made it easier to implementvarious UI and Data platforms
without affecting the application logic
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Edges are all around us
Data
UI
Net
wor
k Disk IO
Edges are all around us
Net
wor
k
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
Edges are all around us
INet
wor
kPro
xyRe
try L
ogic
/ For
mat
ting
Data
UI
Net
wor
k Disk IO
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM
https://github.com/calebjenkins/Acme.CodingNaked
These examples are for the expressed purpose ofdemonstrating unit testing scenarios.. none of this code(especially the supporting non-testing code) isintended to be deployed in production scenarios. Thisis just some code from a talk at some conference oruser group. There's no warranty, express or implied.Works on my machines. I don't know you and I don'tknow how you got here. Stop calling. I no live here,you no call back!
“Don’t be silly”- Roy Osherove
The Art of Unit Testingpg. 77
the problem with statics
shared state be like..
IDependency
IdependencyExtensionMethod
Class to Test
IdependencyMethod
Idependency Method
IdependencyMethod
the challenges with Microsoft Fakes
Identify your edges
Keep the edges dumb
Stop coding naked
StartFramework, Runner, Test Code
Set the Scene
Handle your Dependencies
no, seriously
http://www.flickr.com/photos/jforth/5768064504/
http://www.flickr.com/photos/laughingsquid/255915238/
http://www.flickr.com/photos/dieselbug2007/370557683/
http://www.flickr.com/photos/m0php/530526644/
http://www.flickr.com/photos/lowfatbrains/80542761/
http://www.flickr.com/photos/georgivar/4974112941/
http://www.flickr.com/photos/redbettyblack/395899686/sizes/
http://www.flickr.com/photos/goldberg/815408116/
http://www.flickr.com/photos/fudj/122371431/
http://www.flickr.com/photos/yardsale/4524101944/
http://www.flickr.com/photos/38738277@N04/3652658961/
http://www.flickr.com/photos/utslibrary/6776175796/
http://www.flickr.com/photos/48725518@N03/4478990651/Copyright © Merriswheel – Used without permission
developingUX.com@calebjenkins#ITDevConnections
#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM