Without writing any automated tests, how do I start behavior-based development?

I have programmed in many languages ​​for many years and would like to think that I am generally good at this. However, I never wrote automatic testing: no unit tests, no TDD, no BDD, nothing.

I tried to start writing suitable test suites for my projects. I see the theoretical significance of the fact that you can fully test all the code in a project after making any changes. I see how test environments such as RSpec and Mocha should make setting up and running these tests simple enough, and I like the DSLs that they provide for writing tests.

But I never managed to write the actual unit test for any part of my code. The material I write never seems very tested, which is really useful.

  • Functions do not seem very callable outside the context in which they are used. Many of the functions that I write cause HTTP request requests or database requests or some other call that cannot be easily tested.
  • Some functions return HTML strings. I can compare an HTML string with a hard-coded version of the same string, but this only limits my ability to modify this part of the code. Plus, loading HTML in my test code is a mess.
  • I can pass the mock / spy objects to the method and make sure that they get certain method calls, but as far as I can tell, there is only testing the details of the implementation of the method that I am "testing".

How do I get started with proper BDD testing? (I would like to do this with Mocha and Node.js, but the general recommendations for BDD are fine too.)

+6
source share
2 answers

It seems that the main question you are asking is “how do I write test code”?

Being a fan of object-oriented programming, I know that I am biased, but in my experience it is much easier to check code written in OO style. The reason for this is that unit tests are designed to test small, isolated components of the system, and well-designed object-oriented code (mostly) provides this.

I agree that functions are often related to the context in which they are located, which makes testing them difficult. I don’t have much experience working with functional programming, but I know that this context is often passed in some kind of variable, which makes it difficult to separate the problems of functions.

With OO programming, I successfully tested objects that wrap HTTP requests, database queries, etc., making fun of an object that executes an actual network request to return a known dataset. You then verify that your wrapper is processing this data correctly. You can also check for crashes and unexpected data. Another way to do this is to configure the local server, which you use instead of the usual endpoint, but this gives your test suite an external dependency, which should be avoided when possible.

When testing HTML, many people don’t do this at all because of the very variable nature of the view layer. However, there are some things that really deserve to be tested, but never a complete line of HTML - as you discover, just a tiny change will mean that the whole test breaks. What do you really test in this case that the two lines in separate parts of your code base are the same?

The best thing is to load an HTML string from your function / object into the HTML parser library, and you can usually use Xpath or CSS selectors to check tags with specific classes, identifiers, or other attributes and check the number of elements matching certain requirements. Rspec has a built-in method ( have_tag() ), like many testing libraries.

Something else you might want to pay attention to is integration testing (e.g. Capybara, Selenium). This will load your web application using the JavaScript engine, so you can check the HTML elements as well as the JavaScript events.

Generally mocking / sealing, you usually only want to do this with objects that depend on the object you are testing. Otherwise, you can pretty much manipulate everything to claim it is true!

As for resources for testing, I would recommend looking at development test books, even if you do not plan to practice TDD. The main reason is that they first give you a head for testing. here are a few:

+3
source

TDD or BDD does not matter, they are isomorphic, as evidenced by Mocha, where they are just pluggable interfaces.

I can pass the mock / spy objects to the method and make sure that they get certain method calls, but as far as I can tell, there is only testing the details of the implementation of the method that I "tested".

Exactly what the unit testing point is: you are only testing the code you are testing, not more. The problem with testing is more that a failed test usually does not indicate the problem for sure. The promise of unit testing is that you can largely avoid debugging. Too rough tests nullify. Of course, balance is needed, anyone with experience with UT will tell you that it's easy to fall into the trap I -ve-stubbed-the-thing-i-wanted-to-test-and-didn't-notice, your tests may be green and the actual code is not working.

0
source

All Articles