Considerations Before Testing

I am starting to use test development for JavaScript, but I would like to start using it in my various projects.

I would like to know what are common mistakes and how to avoid them?

In addition, I would like to know what I should read in order to simplify the testing process and apply it to my code.

Thanks in advance.

+4
source share
3 answers

The biggest problem I encountered while using TDD is developers who are not sure about unit testing. Poor unit testing spends more time than it saves. Baffled, unreliable, inconspicuous, unreadable tests fall on the sidelines very quickly, and foggy bug developers take time to want a unit test again.

Per Fagrell makes some good points, especially about testing after each change; it should be second nature for testing before and after any change to the test.


Frames:

Consider QUnit as your framework for testing JS: http://docs.jquery.com/Qunit

I have a dependent-labeled test harness page, and the tests run very well when the page loads.

You can follow

  • To arrange
  • Law
  • Approve

flow for unit tests using QUnit.

However, you will have to manually use the test setup and deletion methods and name them in your test methods. This will help isolate test cases by preserving indentical conditions for all tests and not allowing tests to be dependent on their order of operation.

Look for useful frameworks in other languages ​​that you will use. NUnit is very popular for .NET.


Insulation:

Per Fagrell also draws a good conclusion about isolation. The difference between unit testing (testing one aspect of an atom of functionality) and integration (testing the interaction of several atoms) must be carefully understood before testing. If you have several statements in the test method, you are not unit testing and you need to change your test method.


Legend:

Good naming convention with excellent The Art Of Unit Testing for your MethodUnderTest_Condition_ExpectedBehaviour tests, e.g.

Expand_TextVariable_ExpandsText

Save your tests from the same book :

  • Authentic
  • served by
  • Read

Otherwise, you and other developers will not want to run tests.


Fakes:

A common misunderstanding is the difference between the two types of fakes: stubs and mocks .

A seam is created in code by abstracting the functionality that the code in the interface depends on. For instance. The controller is independent of a specific repository, it will depend on an IRepository.

A stub , then implements this IRepository and returns fake values; it is used to isolate controller code that runs in isolation. for example GetCustomer() will create a new customer and return it, without GetCustomer() calls to the real repository or to any store. Workpieces are never checked for .
mock is like a stub except that it may contain values ​​that can be tested. for example AddCustomer(Customer customerToBeAdded) , your layout will accept this value and may be opposed. Cats can be tested against .

Look at the test isolation structure (or Mocking Framework), which can automatically create fakes for this interface.

Failure to understand the purpose of bullying led to the fact that more than one developer, whom I saw, create a layout for testing functionality, and then record tests against the layouts themselves.


Resources

I mentioned The Art Of Unit Testing and I recommend it completely. This is one of the books along with Code Complete that I would grab if the office caught fire.

Hope this helps.

+10
source

Make sure your tests only test one functionality. Name and affirmations should be fully aligned with this. For instance. if you add the expand () function to the variable handler, then the test should be called (roughly) test_expands_variables or should_expand_defined_variable or something that matches your naming convention, and the statements should only be on the return value or side effects of the function call. A common mistake is the approval of the setup steps, but any functionality in the setup should have its own test with what is approved on the spot. If you say the same thing everywhere, you suddenly find it difficult to figure out which test you should correct.

A good starting place to feel the whole TDD cycle is to try to use some code-kata. The Roman Digital Converter is a general first introduction to writing tests. First, be REALLY anal about the tests after every small change. Once you get this, you will feel how intrusive you need / should be, but getting a rhythm for a beginner usually requires being really pedantic.

+3
source

I started entering TDD in Javascript using Christian Johansen's book Test-Driven JavaScript Development. It works great and covers almost every aspect of TDD and applies it to JS: http://tddjs.com/

0
source

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


All Articles