Code testing

I have heard this quite a bit when it comes to TDD: “You should always check before entering the code”, in fact I never did a full TDD, or maybe I didn’t use it, but how can I verify that you even did not do???

Can you give me a clear example of how to do this?

+3
source share
9 answers

Recently, I thought that this aspect of TDD is the same as the concept of “desired design” programming, which I read about in Structure and Interpretation of Computer Programs . For example, in lecture 2A of the MIT course taught by the authors, the following example is given for calculating square roots as a function of fixed points:

(define (sqrt x)
  (fixed-point
      (lambda (y) (average (/ x y) y))
      1))

This is shown before the procedure fixed-point. You do not need a definition to understand that you can use the procedure fixed-pointto calculate the square roots. Once you see how you are going to use it, you can identify it.

(define (fixed-point f start)
  (define tolerance 0.00001)
  (define (close-enuf? u v)
     (< (abs (- u v)) tolerance))
  (define (iter old new)
     (if (close-enuf? old new)
         new
         (iter new (f new))))
  (iter start (f start)))

This is the simplest idea used in TDD. By writing tests for your methods, before writing the methods themselves, you show how these methods should be used.

+4

, , , . , , , , , . , , "test-" ( -, ), .

, :

sqrt

-, , .

", , , , , "

TDD , . TDD'er :

" , , "

:

void testSqrt () {
    if (sqrt(9.0) == 3.0) {
        printf("ALL IS FINE AND DANDY HERE");
    }
    else {
        printf("THE SYSTEM IS DOWN! THE SYSTEM IS DOWN!"); /* untz untz untz utnz */
    }
}

sqrt ... 3 9! ,

float sqrt(float n) {
    long i; float x, y;
    const float f = 1.5;
    x = n/2.0;
    y = n;
    i = *(long *)&y;
    i = 0x5f3759df-(i >> 1);
    y = *(float *) &i;
    y = y*(f-(x*y*y));
    y = y*(f-(x*y*y));
    return n * y;
}

float sqrt(float n) {
    float n_t = n/2.0;
    int i = *(int*)&n;
    i = 0x5f375a86 - (i>>1);
    n = *(float*)&i;
    n = n*(1.5f-n_t*n*n);
    return n;
}

, 3 3, . , .

+4

- , API. unit test API, ( ). , , . API. , , . , . TDD.

+2

Test Driven Development: .

: , ; , , , ( , , ), . , , . ( - ), .

, , , , , . , , , , , ( , ).

+1

, , , . : , , . , .

0
def test_add_numbers
  assert_equal( 42, add(40,2) )
  assert_equal( 42, add(42,0) )
  assert_equal( 42, add(44,-2) )
  assert_equal( 42, add(40,1,1) )
  assert_equal( 42, add(42) )
  assert_raises(ArgumentError,"Add requires one argument"){
    add()
  }
end

add, , , , .

, , , .

0

, :

1) API 2) .

, , . , , :

public void testMyFunc() {
    List arg1 = new List()...
    String arg2 = 'test';
    int returned;
    int expected = 3;

     // ok what do i need to write
     returned = myFunc(arg1, arg2);

     assertEquals(expected, returned);
}

- , myFunc. , .

0

, - http://www.theserverside.net/tt/articles/content/TDD_Chapter/ch2.pdf

" " " " - , , , , API, . . /, . , , , , . , ...

0

, , , :

Assert.Fail("Bootstrapping the test");

.

0

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


All Articles