TDD Example for Business Applications

I understand the general idea of ​​unit testing and used it in scenarios where complex processes took place on the system, but I still have a question about all of these principles that come together.

We are warned not to test the framework or database. Good user interface design is not amenable to testing other than human. The user interface is generally excluded as part of MVC. In many applications, what remains ?. 37signals talks about extensive device testing, but in an application like Basecamp or Backpack, which types are tested using the appropriate unit tests? What does 100% code coverage mean in this scenario?

EDIT: I don't tear off applications like Backpack - they are awesome, but the work seems to be more about design and interaction, rather than complicated logic (in fact, they support this idea). In areas of this application where CRUD and the hierarchy of an object are related to the fact that they cover it to a large extent, does the number of units of unit tests correspond to zero? Is the test point in this case another copy of the verification code (required, regular expression, etc.)?

+4
source share
2 answers

TDD for business applications works as follows.

  • Record the business requirement.

  • Record a test for this requirement.

  • Enter the code that passes the test.

The trick is that there are many dissimilar non-standard requirements that do not require rigorous testing.

  • Saving a database is not a requirement for a business. This is technical.

  • “activating a button in the GUI for any situation” is not a business requirement, it is part of the interface.

  • “backup” is not a business requirement; is it security or business continuity or something like that.

Consider a specific example.

The requirement is "You cannot borrow books until you pay your fines."

tests.

  • Trying to borrow a book with dignity.

  • Trying to borrow a book without penalty.

The code.

class FinesNotPaid( unittest.TestCase ): def setUp( self ): # load some fixtures with users, books and fines outstanding. def test_should_not_checkout_book( self ): x = TheCheckoutClass() x.checkoutBook( someBook ) self.fail( "Should have thrown error" ) class FinesPaid( unittest.TestCase ): def setUp( self ): # load some fixtures with users, books and fines paid. def test_should_not_checkout_book( self ): x = TheCheckoutClass() x.checkoutBook( someBook ) self.success( ) class NoFines( unittest.TestCase ): etc. 

These are business rules implemented by classes that are separate from your database and your GUI. These are application domain level classes.

Your CRUD rules are business rules. You need to test them. However, you do not need to check every database-related function. You need a few "can I create and save an object?" tests. You must believe that ORM, the data access layer, and the database really work. You do not write tests for exhaustive testing of built-in ORM functions.

Code coverage (100% or 80% or 10%) is pretty much pointless. Is software with tests with 80% code coverage actually 20% more likely to fail? This does not work. Testing every line of code does not cover every logical path, so stop worrying and start testing.

Check out business use cases. If they pass and there is unverified code, then - perhaps - you wrote too much code.

+8
source

You are testing to see if the business logic (in many applications this is the “service” or “logical” level) matches the description of business users about how the business works. You are testing to make sure that, for example, just because you add a 6% sales tax on all purchases in Pennsylvania, you also do not receive a 6% bonus when you give someone a gift card.

There are (or should be) a lot of brain juice in the application layers that are between the user interface and the database. This is what you need to verify in order to verify.

+2
source

Source: https://habr.com/ru/post/1309804/


All Articles