u n i t t e 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 !
Automated
Unit Test
Fundamentals
Hard to Reach
Places
https://twitter.com/DisneyPixar/status/682970293523079170
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
13
what are the 4 big parts of unit tests?
Test Framework Test Runner
Code Tests
14
Test Framework Test Runner
Code Tests
15
your application
what you want to test
your test code
the code that tests the
code that you wrote or are
going to write
attributes and asserts
the framework provides
the attributes and asserts
so we know what the tests
are doing.
Examples: nUnit jUnit
cppUnit
runs the tests
often associated with the
test framework; is
distinctly separate.
sometime integrated in
IDE, CI Server or stand
alone exe
Test Runner
16
Code
Test
Framework
Tests
Tests
Tests
Tests
Tests
Start
Test Runner
Test Code
Test Framework
18
Test Framework
19
Test Framework
github.com/tpierrain/NFluent
github.com/fluentassertions
github.com/erichexter/Should
Visual Studio
(VS Test Adapters: xUnit ,nUnit, VS Test etc)
> dotnet test (cli)
Continuous Integration (Team City)
Continuous Integration (GitHub Actions)
Test Runners
Test Artifacts
22
BDD Artifacts
Code Coverage / Complexity
3 A’s
23
– Set up the scenario and the initial input values.
Often in a common [TestFixtureSetup] or [Setup] method
- Action that creates the outcome that is being tests,
usually calling some method in your code to test the result.
– Is a boolean statement to your testing framework to
declare the expected outcome.
Results in Pass or Fail
Arrange Act Assert
Test Code
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
Red
Green
Refactor
Test Runner
return values
no dependencies…
App Domain Domain Validation
UI Logic
Test Code
Unit Tests focus on a Unit
Test a unit in isolation from other units
Control input => Testable output
Simple Tests
VSTest
Start
Test Runner
Test Code
Test Framework
Set the Scene
Defining Behavior
Discussion
what if our “tests”
matched our
language?
Discussion
what if our “tests”
matched our
language?
Discussion
Discussion
BDD
Discussion
BDD
Resources & Frameworks
BDD
http://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, Cucumber
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”
- Real World .NET, C# and Silverlight
Wrox Press 2012
Caleb Jenkins
Data Access
Data Logic
Integration Service Proxy
App Domain Domain Validation
UI Logic
UI
How do you test this
with these
dependencies
Harder to Test
Data Access
Data Logic
Integration Service Proxy
App Domain Domain Validation
UI Logic
UI
Test Runner
Test Code
Integration Service Proxy
App Domain Domain Validation
UI Logic
Dependency Injection + Interfaces
Faked dependencies to increase unit isolation
Leverage mocking frameworks makes life better
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
http://developingUX.com/DI/
http://developingUX.com/DI/
- Real World .NET, C# and Silverlight
Wrox 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 in .NET
Microsoft.Fakes
Bringing 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();
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();
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();
Set the Scene
Handle your
Dependencies (DI + Mocks)
the problem with edges
UI Data
Business
Edges are
Hard to Test
Testing edges
can be like
testing to see
if you’re good
at cliff jumping
That’s not me
..or you’re
stuff on a rock.
You’re either an
expert and it works…
UI Data
Data
Logic
UI
Logic
Business
Edges are
Hard to Test
Edges are still
Hard to Test
by separating UI/Data edges from
UI/Data logic we’re increasing the testable area
we’ve also made it easier to implement
various UI and Data platforms
without affecting the application logic
Edges are all around us
Data
UI
Network
Disk
IO
other
Edges are all around us
Network
Edges are all around us
INetworkProxy
Retry
Logic
/
Formatting
Thin-Edges-Principle
Thin-Edges-Principle
github.com/calebjenkins/Acme.CodingNaked
These examples are for the expressed purpose of
demonstrating unit testing scenarios.. none of this code
(especially the supporting non-testing code) is
intended to be deployed in production scenarios. This
is just some code from a talk at some conference.
There's no warranty, express or implied. Works on my
machines. I don't know you and I don't know how you
got here. Stop calling. Caleb Who?! 👀
Examples: https://github.com/calebjenkins/Acme.CodingNaked/blob/main/src/BDD/BDD_ExampleLib/AccountService.cs
Strategy
Examples: https://github.com/calebjenkins/Acme.CodingNaked/blob/main/src/BDD/BDD_ExampleLib/AccountService.cs
“Don’t be silly”
- Roy Osherove
The Art of Unit Testing
pg. 77
the problem with statics
shared state be like..
also applies to statics, hard-coded
singletons, globally scoped variables
DI Lifecycles
IDependency
Idependency
Extension
Method
Class to Test
Idependency
Method
Idependency Method
(sometimes, not even)
Idependency
Method
Example:
https://github.com/calebjenkins/Acme.CodingNaked/blob/main/src/WithExtensionsLibTests/BusinessProcessStartTests_Reset_Failure.cs#L37
// Clean up after test
https://github.com/calebjenkins/Acme.CodingNaked/blob/main/src/WithExtensionsLibTests/MoqLoggingExtensions.cs
Strategy
Special Thanks: https://adamstorr.azurewebsites.net/blog/mocking-ilogger-with-moq
the challenges with Microsoft Fakes
Find the edges
Keep the edges thin
Stop coding naked
developingUX.com
@calebjenkins
.com/in/calebjenkins
#thatconference
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
.com/in/calebjenkins
#ThatConf2023
github.com/calebjenkins/Acme.CodingNaked
slideshare.net/calebjenkins/coding-naked

Coding Naked 2023

Editor's Notes

  • #35 Ubiquitous language between Business <> Technology
  • #36 Ubiquitous language between Business <> Technology
  • #37 Nothing worse than seeing/…. Test 1, Test 2, Test 3.. What do those even mean?!
  • #38 Nothing worse than seeing/…. Test 1, Test 2, Test 3.. What do those even mean?!
  • #39 SpecFlow is very much Cucumber for .NET
  • #52 Build out slide.. Before you click through: take a minute to have student “read” test and talk out what it’s doing.
  • #53 Build out slide.. Before you click through: take a minute to have student “read” test and talk out what it’s doing.
  • #54 Build out slide.. Before you click through: take a minute to have student “read” test and talk out what it’s doing.
  • #62 Other edges.. Active Director / Oauth Claims / User Credentials Logging, Configuration, Property Files, many many more.
  • #79 I ❤Extension Methods.. But I have to be careful, and tread carefully.
  • #88 https://github.com/calebjenkins/Acme.CodingNaked/blob/main/src/WithExtensionsLibTests/BusinessProcessStartTests_Reset_Failure.cs#L37