Introduction
In my current role at an agile company we work in pairs and following XP principles, one of which is TDD. I’ve been following TDD for a few years now and one thing I think that helps me when I come back to old tests and helps other developers who are new to a project is when a test has a "context".
Example of a test with context
class When correcting a customers address with no postcode Then ???
Then what? Well that’s the great thing about working in a pair you get to discuss what should happen in the above "context". Perhaps it should log an error, perhaps it should throw an exception, perhaps it should just do nothing? Who knows but at least the context is in the test.
Another example of a test with context
class When correcting a customers address with a valid address Then ???
Again the name of the class should help kick start the discussion between the pair.
If you compare that to a class without a context:Example of test without context
class CustomerFixtures Then ???
It’s not quite the same and doesn’t really lend it’s self to discussion about behaviour, what do you think?
I don't want to pretend I invented any of these concepts. They've been around for a while now, it's called BDD or Behaviour driven design. For me though it's more about making your tests readable and more importantly understandable from just looking at the class and method names. Do a search for "Context Specification" on your favourite search engine and you'll find lots more examples.
Code
Enough talking get with the code. Here are 2 small helper classes that have been adapted from a “Specification Context” example I found on the interwebs. This works with NUnit. I hope you’ll find it useful:
public abstract class Specification { [SetUp] public void BaseSetUp() { SetupParameters(); SetupDependencies(); InitializeClassUnderTest(); Because(); } [TearDown] public void BaseTearDown() { DisposeContext(); } protected virtual void SetupParameters() { } protected virtual void SetupDependencies() { } protected virtual void InitializeClassUnderTest() { } protected virtual void Because() { } protected virtual void DisposeContext() { } }
public abstract class SpecificationFor<TClassUnderTest> : Specification { protected override void InitializeClassUnderTest() { ClassUnderTest = CreateClassUnderTest(); } protected abstract TClassUnderTest CreateClassUnderTest(); protected TClassUnderTest ClassUnderTest { get; private set; } }
To show how you can use these helper classes I'm using 3 classes:
- SpecificationForCorrectCustomerAddress - base class which sets up the class under tests and any dependecies
- When_correcting_a_customers_address_with_a_valid_address - First context (Happy path)
- When_correcting_a_customers_address_with_no_postcode - Second context (Unhappy path)
public abstract class SpecificationForCorrectCustomerAddress: SpecificationFor<Customer> { protected Customer.AddressDetails AddressDetails; protected ILogger Logger; protected override void SetupParameters() { AddressDetails = new Customer.AddressDetails { Postcode = "AB1 2CD" }; } protected override void SetupDependencies() { Logger = Substitute.For<ILogger>(); } protected override Customer CreateClassUnderTest() { return new Customer(Logger); } protected override void Because() { ClassUnderTest.CorrectCustomerAddress(AddressDetails); } }
[TestFixture] public class When_correcting_a_customers_address_with_a_valid_address : SpecificationForCorrectCustomerAddress { [Test] public void Then_correction_is_logged() { Logger.Received().Info(Arg.Any<string>()); } }
[TestFixture] public class When_correcting_a_customers_address_with_no_postcode : SpecificationForCorrectCustomerAddress { private Action action; protected override void SetupParameters() { AddressDetails = new Customer.AddressDetails(); } protected override void Because() { action = () => ClassUnderTest.CorrectCustomerAddress(AddressDetails); } [Test] public void Then_unknown_postcode_exception_is_thrown() { action.ShouldThrow(); } }
As you can see all you do is inherit from the SpecificationFor<T> abstract class and implement the CreateClassUnderTest method. The other methods are optional. I like to split up my setup process into the following parts:
- SetupParameters - an area to setup any parameters required by method/property being called
- SetupDependencies- an area to setup any dependencies required by ClassUnderTest
- CreateClassUnderTest - This is where you create the ClassUnderTest, passing it any dependencies it might require.
- Because- This is where you call the method/property on the ClassUnderTest
- DisposeContext- Any Tidying up code you might require
This pattern is classic TDD, I.e. Arrange, Act, Assert. Where SetupParameters, SetupDependencies and CreateClassUnderTest are part of the Arrange. Because is the Act and the [Test] methods are just the Assert. This results in a very readable output in your favourite test runner.
Good article. I find this approach very comfortable to use for tests. Thanks!
ReplyDeleteP.S. Looks very nice in a ReSharper test session :)
The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Final Year Project Centers in Chennai.
DeleteSoftware management seeks for decision support to identify technologies like JavaScript that meet best the goals and characteristics of a software project or improvement programme. JavaScript Training in Chennai Accessible experiences and repositories that effectively guide that technology selection are still lacking.
Aim of technology domain analysis is to describe the class of context situations (e.g., kinds of JavaScript software projects) in which a software engineering technology JavaScript Training in Chennai can be applied successfully
Thanks Siarhei,
ReplyDeleteReally chuffed that you took the time to download the example and check it out.. happy testing.
Muy bien! Found it tricky at first because the inheritance hides stuff whereas in the old flat style of writing tests the code was all in the same place. I see that starting from the Because() is the best place to start when debugging your tests or getting a little more understanding of the code under test.
ReplyDeleteI believe that your blog will surely help the readers who are really in need of this vital piece of information. Waiting for your updates.
ReplyDeleteSpoken English Classes in Chennai
Spoken English in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
Hal yang paling membuat para penjudi kalah adalah emosi yang buruk saat bermain. Dengan begitu maka para penjudi jangan sampai untuk bermain dalam emosi yang buruk.
ReplyDeleteasikqq
http://dewaqqq.club/
http://sumoqq.today/
interqq
pionpoker
bandar ceme terpercaya
freebet tanpa deposit
paito warna
syair sgp
ReplyDeletefantastic post from you guys that amazed me as I get lot to learn from your article. It is Really very informative and creative contents for all the person who is looking to gain knowledge from the article. Thanks for sharing in information
Thanks.
DedicatedHosting4u.com
Nonton Film Bokep
ReplyDeleteDOMINOQQ
SASAQQ
BANDARPOKER
I am overwhelmed by your post with such a nice topic. Usually, I visit your blogs and get updated with the information you include but today’s blog would be the most appreciable...
ReplyDeleteThanks
Cpa offers
Nice Post. If you wish to get study abroad in any course. The below information will suit you.
ReplyDeleteSkolarrssolutions
Mbbs in Russia
Mbbs in UK
Mbbs in Malaysia
Nice Post. If you wish to get study abroad in any course. The below information will suit you.
ReplyDeleteSkolarrssolutions
Mbbs in Russia
Mbbs in UK
Mbbs in Malaysia
Rice Bags Manufacturers
ReplyDeletePouch Manufacturers
wall putty bag manufacturers
fertilizer bag manufacturers
Lyrics with music
we have provide the best ppc service.
ReplyDeleteppc company in gurgaon
website designing company in Gurgaon
PPC company in Noida
seo company in gurgaon
PPC company in Mumbai
PPC company in Chandigarh
Digital Marketing Company
we have provide the best fridge repair service.
ReplyDeletefridge repair in faridabad
Videocon Fridge Repair in Faridabad
Whirlpool Fridge Repair in Faridabad
Hitachi Fridge Repair In Faridabad
Washing Machine Repair in Noida
godrej washing machine repair in noida
whirlpool Washing Machine Repair in Noida
IFB washing Machine Repair in Noida
LG Washing Machine Repair in Noida
iso certification in noida
ReplyDeleteiso certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification in delhi
iso consultants in noida
iso registration in delhi
ReplyDeleteiso certification in faridabad
ISO 9001 Certification in Noida
website designing services
SEO Service Consultant
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteSelenium online training
Selenium certification training
Selenium online course
Selenium training course
Mobile app development company in gurgaon
ReplyDeleteIt is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteDocker online training
Docker certification training
Docker online course
Docker training course
Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
ReplyDeleteSoftware testing online training
Software testing certification training
Software testing online course
Software testing training course
Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
ReplyDeleteSelenium online training
Selenium certification training
Selenium online course
Selenium training course
data sgp
ReplyDeletedata sydney
datahk
syair sydney
syairsgp
datasgp
paito warna
http://warungsgp.com/
live hk 6d
live sydney
Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful. Blockchain online training
ReplyDeleteBlockchain certification training
Blockchain online course
Blockchain training course
Shweta gaur is one of the famous makeup artist in all over India. We are providing the best makeup artist courses and more other courses in over branches in Delhi.
ReplyDeleteMakeup Artist in Delhi
Makeup Artist
Best Makeup Artist in Delhi
Best Makeup Artist in East Delhi
Top Makeup Artist in Delhi
Top Makeup Artist in India
Bridal Makeup
Best Bridal Makeup Artist in Delhi
Bridal Makeup Artist in Delhi with Price
Bridal Makeup Artist in Delhi NCR
Top Bridal Makeup Artist in Delhi
Bridal Makeup in Delhi
Bridal Makeup Charges in Delhi
Bridal Makeup Packages Prices
Freelance Makeup Artist
Bridal Makeup Charges in Delhi
Freelance Bridal Makeup Artist in Delhi
Reception Makeup
Party makeup
Wedding makeup artist
Makeup Artist in Noida
Mobile app development company in mumbai
ReplyDeleteapp development company in delhi
ReplyDeletegreat tips At SynergisticIT we offer the best java training california
ReplyDeleteGood blog, it's really very informative, do more blogs under good concepts.
ReplyDeleteartificial intelligence and machine learning
how to learn react js
services provided by amazon
angularjs web application
aws interview questions and answers for experienced
Good blog, it's really very informative, Mustache Transplant
ReplyDeleteMua vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ tháng nào rẻ nhất
vé máy bay pleiku đi hồ chí minh
vé máy bay nha trang - hà nội
vé máy bay đi nha trang
giá vé máy bay đi đà lạt khứ hồi
taxi sân bay hà nội nội bài
ยูฟ่าเบท หรือ UFABET คือ เว็บให้บริการเดิมพันออนไลน์แบบครบวงจรโดยไม่ผ่านเอเย่นต์หรือตัวแทนใดๆ ซึ่งมีเกมและกีฬาให้เลือกเดิมพันมากมายหลากหลายประเภท รวมไปถึงคาสิโนออนไลน์หลายชนิด UFABET ถือได้ว่าเป็นเว็บไซต์ให้บริการเดิมพันออนไลน์ที่ผู้คนส่วนใหญ่นิยมใช้บริการ เพราะด้วยรูปแบบเว็บไซต์ที่ใช้งานง่ายและไม่จำเป็นต้องทำความเข้าใจอะไรเยอะ ประกอบกับเว็บนี้ยังรองรับการใช้งานในหลายๆภาษา ทั้ง จีน ฮ่องกง ไทย อังกฤษฯ จึงทำให้ผู้ใช้งานส่วนใหญ่ทั้งหน้าใหม่หรือหน้าเก่าต่างเลือกที่จะหันมาใช้บริการกับทาง ยูฟ่าเบท มากขึ้น จึงเป็นเว็บเดิมพันออนไลน์ที่ดีที่สุดในภูมิภาคเอเชีย ufa
ReplyDeleteAppslure is an award-winning mobile app development agency building feature-packed and interactive mobile applications for startups, medium and large enterprises.
ReplyDeleteMale fertility doctor in chennai
ReplyDeleteStd clinic in chennai
Erectile dysfunction treatment in chennai
Premature ejaculation treatment in chennai
Small penis size treatment in chennai
Ivf clinic in chennai
Every call and enquiry bodyguard servicesthat we receive is dealt with the utmost discretion, respect, carefulness and understanding.
ReplyDeleteYou have posted such a fabulous post; I am looking for such type of helpful posts. Thanks for sharing it. Kinemaster Gold
ReplyDeleteI am very impressed with the article. The content of information is very informative. We are also providing the best services click on below links to visit our website.
ReplyDeleteSnowflake Training
Workday Training
Okta Training
AEM Training
CyberArk Training
Nice blog post so thanks a lot for sharing this great blog post.. keep more post for sharing.. have a nice day.
ReplyDeleteNotary Public Lawyer in Cambridge
Thank you very much for this great post.. also check
ReplyDeleteounce to cup
밀양콜걸
ReplyDelete사천콜걸
구미콜걸
강진콜걸
구미콜걸
사천콜걸
영주콜걸
대전콜걸
ReplyDelete세종콜걸
광주콜걸
서울콜걸
울산콜걸
인천콜걸
부산콜걸
대구콜걸
Null’s Brawl APK is a modified version which have unlimited money and unlocked all skins for free.
ReplyDelete