Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Swift protocol which helps both Devs and QAs on writing human-readable and easy editable tests using the logical table of truth

NotificationsYou must be signed in to change notification settings

ndPPPhz/TruthTableOption

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

TruthTableOption

The TruthTableOption is a Swift protocol which helps both Devs and QAs on writing human-readable and easy editable tests.It's applicable anywhere you can synthesize your logic with aTable Of The Truth.

Example and applicable scenario

Imagine a scenario like the following one:You have to write and test a new feature which tells you whether you need to display a new banner containing a discount code to the users.You have been told from your business analyst that you only have to show the new banner in your app when:

  1. The customer gender is female
  2. Is a new customer or is the international women's day

After discussing with your QAs you end up with the following ACs

newCustomerisInternationalWomensDayisFemaleResult
falsefalsefalsefalse
falsefalsetruefalse
falsetruefalsefalse
falsetruetruetrue
truefalsefalsefalse
truefalsetruetrue
truetruefalsefalse
truetruetruetrue

Developing a feature like that, requires different layers of logic which have to be fully tested.Pretty sure you'd like to avoid writing single tests for each scenario. Moreover, in future, you should be able to extend this featurewithout trying to understand how the logic works.Debugging gets harder as big as the table and the requirement grows

This can be reduced to something like this:Screenshot 2020-03-10 at 20 04 03

and here's where the TruthTableOption comes to rescue.

Usage

  • Create an enum which has aString as RawValue and conforms toTruthTableOption.
  • Add a case for each column of the table.
  • ImplementpassingScenarios in the following manner:
    • For each row where the result is true
      • .true when the condition should be true
      • .false when the condition should be false
      • .dontCare when the condition could be bothtrue orfalse
enumPromoBanner:String,TruthTableOption{staticvarpassingScenarios:[Set<TruthTableOptionDescriptor<PromoBanner>>]=[[.true(.isFemale),.true(.newCustomer)],[.true(.isFemale),.true(.isInternationalWomensDay)],[.true(.isFemale),.true(.newCustomer),.true(.isInternationalWomensDay)]]case newCustomercase isInternationalWomensDaycase isFemale}

Test file

Let's assume the following mock object

finalclassPromoBannerMock:PromoBannerInterface{var_isNewCustomer:Bool=falsevar_isInternationalWomensDay:Bool=falsevar_isFemale:Bool=falsevarisNewCustomer:Bool{return _isNewCustomer}varisInternationalWomensDay:Bool{return _isInternationalWomensDay}varisFemale:Bool{return _isFemale}}

and the following test

func testPromoBanner(){// Get the full table of truth and iterate for each rowforoptionRowinPromoBanner.fullOptionTable{// Set up the mock object with the current rowconfigureState(with: optionRow)testScenario(with: optionRow)}}privatefunc configureState(with optionRow:PromoBanner.Row){promoMock._isNewCustomer= optionRow.contains(.newCustomer)promoMock._isInternationalWomensDay= optionRow.contains(.isInternationalWomensDay)promoMock._isFemale= optionRow.contains(.isFemale)}privatefunc testScenario(with optionRow:MarioBonusLevel.Row){// The result from the managerletexpectedResult= manager.shouldShowBanner// The result from the table of the truthletresult= optionRow.resultXCTAssertEqual(result, expectedResult,"Expected result\(expectedResult) instead\(result)")}

Inspiration

If you are interested in the followed approach, take a look at


License

License

MIT license

  • Copyright 2018 © Annino De Petra

About

Swift protocol which helps both Devs and QAs on writing human-readable and easy editable tests using the logical table of truth

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp