项目作者: ndPPPhz

项目描述 :
Swift protocol which helps both Devs and QAs on writing human-readable and easy editable tests using the logical table of truth
高级语言: Swift
项目地址: git://github.com/ndPPPhz/TruthTableOption.git
创建时间: 2019-08-14T16:46:25Z
项目社区:https://github.com/ndPPPhz/TruthTableOption

开源协议:

下载


" class="reference-link">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 a Table 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

newCustomer isInternationalWomensDay isFemale Result
false false false false
false false true false
false true false false
false true true true
true false false false
true false true true
true true false false
true true true true

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 feature
without 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 a String as RawValue and conforms to TruthTableOption.
  • Add a case for each column of the table.
  • Implement passingScenarios 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 both true or false
  1. enum PromoBanner: String, TruthTableOption {
  2. static var passingScenarios: [Set<TruthTableOptionDescriptor<PromoBanner>>] = [
  3. [.true(.isFemale), .true(.newCustomer)],
  4. [.true(.isFemale), .true(.isInternationalWomensDay)],
  5. [.true(.isFemale), .true(.newCustomer), .true(.isInternationalWomensDay)]
  6. ]
  7. case newCustomer
  8. case isInternationalWomensDay
  9. case isFemale
  10. }

Test file

Let’s assume the following mock object

  1. final class PromoBannerMock: PromoBannerInterface {
  2. var _isNewCustomer: Bool = false
  3. var _isInternationalWomensDay: Bool = false
  4. var _isFemale: Bool = false
  5. var isNewCustomer: Bool {
  6. return _isNewCustomer
  7. }
  8. var isInternationalWomensDay: Bool {
  9. return _isInternationalWomensDay
  10. }
  11. var isFemale: Bool {
  12. return _isFemale
  13. }
  14. }

and the following test

  1. func testPromoBanner() {
  2. // Get the full table of truth and iterate for each row
  3. for optionRow in PromoBanner.fullOptionTable {
  4. // Set up the mock object with the current row
  5. configureState(with: optionRow)
  6. testScenario(with: optionRow)
  7. }
  8. }
  9. private func configureState(with optionRow: PromoBanner.Row) {
  10. promoMock._isNewCustomer = optionRow.contains(.newCustomer)
  11. promoMock._isInternationalWomensDay = optionRow.contains(.isInternationalWomensDay)
  12. promoMock._isFemale = optionRow.contains(.isFemale)
  13. }
  14. private func testScenario(with optionRow: MarioBonusLevel.Row) {
  15. // The result from the manager
  16. let expectedResult = manager.shouldShowBanner
  17. // The result from the table of the truth
  18. let result = optionRow.result
  19. XCTAssertEqual(result, expectedResult, "Expected result \(expectedResult) instead \(result)")
  20. }

Inspiration

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


License

License

MIT license

  • Copyright 2018 © Annino De Petra