Writing tests without violating SRP, OCP, DRY

jriver27

I am trying to understand these three principles better.

My question is... How do I write tests without violating SRP, OCP, and DRY?

My current design violates DRY because of the similar code in the test files.

I can't merge the test files together because that will violate the Open/Closed principle. (There is a high probability of adding more modules later)

Is there something I'm missing here? If it helps I'm using Ruby and Minitest for this.

Module files

a.rb:

module A
  # does an algorithm
end

b.rb:

module B
  #does another algorithm
end

Test files

a_test.rb:

class ModuleATest
  # tests the algorithm
end

b_test.rb:

class ModuleBTest
  # tests the algorithm
end
Dave Schweisguth

Test code is quite different from regular code, so although all of those design principles are generally valid in test code, their importance is different there.

Regarding DRY, test code needs to be readable above all else, so it's normal to have a little more duplication in test code than in regular code. It sounds like testing each of your two algorithms requires duplication that you're not showing; address that by extracting the duplication into test helper methods. But do that only if it preserves the clarity of the tests.

Regarding OCP, a test needs to do whatever it needs to do to test the module it's testing. If you got your module design wrong at first and have to split a module into two or something, you'll have to do the same with the test. So don't worry about whether your tests follow OCP, worry about your regular code and the tests will follow.

Regarding SRP, again, a test needs to do whatever it needs to do to test the module it's testing. If a module has too many responsibilities then so will its test. That's a sign that the module needs refactoring to fix the issue for both the module and the test.

Also, there are different kinds of tests. Integration tests by their nature have more responsibilities than unit tests.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to implement movable overloads without violating the DRY principle in C++?

From Dev

A DRY way of writing similar unit tests in python

From Dev

A DRY way of writing similar unit tests in python

From Dev

Impacts of violating SRP, Law of Demeter, etc

From Dev

Impacts of violating SRP, Law of Demeter, etc

From Dev

Writing DRY Code in Python

From Dev

C++ mostly redundant default and parameterized constructors violating DRY

From Dev

C++ mostly redundant default and parameterized constructors violating DRY

From Dev

Merge Upsert without violating unique constraint

From Dev

Recording time stamp without violating the user input

From Dev

Browser-Based SRP without Java

From Dev

Writing tests before writing code

From Dev

Zephyr writing tests

From Dev

Writing tests for AngularJS with UnderscoreJS

From Dev

Writing Unit Tests with ODataQueryOptions

From Dev

Writing tests for mailer rails

From Dev

Writing additional tests

From Java

How to avoid violating the DRY principle when you have to have both async and sync versions of code?

From Dev

Writing JavaScript tests that test other functions are called, without actually calling them

From Dev

Adding a conditional logic without violating SOLID principles C#

From Dev

Fastest way to insert rows without violating the primary key

From Dev

How to implement logging properly without violating solid principals?

From Dev

PHP: How to use extended interfaces without violating SOLID principles in this case?

From Dev

Swap values of two rows in MySQL without violating unique constraint?

From Dev

Creating covariant generic types without violating empty interface rules

From Dev

What is the standard way to configure an ItemTemplate without violating the Item separation?

From Dev

Communicate between two windows without violating class encapsulation

From Dev

What does "without violating encapsulation" mean in Memento pattern

From Dev

How to allow this code within a Chrome Packaged App without violating CSP?

Related Related

  1. 1

    how to implement movable overloads without violating the DRY principle in C++?

  2. 2

    A DRY way of writing similar unit tests in python

  3. 3

    A DRY way of writing similar unit tests in python

  4. 4

    Impacts of violating SRP, Law of Demeter, etc

  5. 5

    Impacts of violating SRP, Law of Demeter, etc

  6. 6

    Writing DRY Code in Python

  7. 7

    C++ mostly redundant default and parameterized constructors violating DRY

  8. 8

    C++ mostly redundant default and parameterized constructors violating DRY

  9. 9

    Merge Upsert without violating unique constraint

  10. 10

    Recording time stamp without violating the user input

  11. 11

    Browser-Based SRP without Java

  12. 12

    Writing tests before writing code

  13. 13

    Zephyr writing tests

  14. 14

    Writing tests for AngularJS with UnderscoreJS

  15. 15

    Writing Unit Tests with ODataQueryOptions

  16. 16

    Writing tests for mailer rails

  17. 17

    Writing additional tests

  18. 18

    How to avoid violating the DRY principle when you have to have both async and sync versions of code?

  19. 19

    Writing JavaScript tests that test other functions are called, without actually calling them

  20. 20

    Adding a conditional logic without violating SOLID principles C#

  21. 21

    Fastest way to insert rows without violating the primary key

  22. 22

    How to implement logging properly without violating solid principals?

  23. 23

    PHP: How to use extended interfaces without violating SOLID principles in this case?

  24. 24

    Swap values of two rows in MySQL without violating unique constraint?

  25. 25

    Creating covariant generic types without violating empty interface rules

  26. 26

    What is the standard way to configure an ItemTemplate without violating the Item separation?

  27. 27

    Communicate between two windows without violating class encapsulation

  28. 28

    What does "without violating encapsulation" mean in Memento pattern

  29. 29

    How to allow this code within a Chrome Packaged App without violating CSP?

HotTag

Archive