Prove correct unit test

I am creating a graph structure for training purposes. I use the TDD approach, so I write a lot of unit tests. However, I am still figuring out how to prove the correctness of my unit tests.

For example, I have this class (not including the implementation, and I simplified it)

public class SimpleGraph(){
 //Returns true on success
 public boolean addEdge(Vertex v1, Vertex v2) { ... }

 //Returns true on sucess
 public boolean addVertex(Vertex v1) { ... }
}

I also created these unit tests.

@Test
public void SimpleGraph_addVertex_noSelfLoopsAllowed(){
 SimpleGraph g = new SimpleGraph();
 Vertex v1 = new Vertex('Vertex 1');
 actual = g.addVertex(v1);
 boolean expected = false;
 boolean actual = g.addEdge(v1,v1);
 Assert.assertEquals(expected,actual);
}

Ok, this is awesome. There is only one question here, I proved that functions only work for this case. However, in my courses on graph theory, everything I do theoretically proves theorems (induction, contradiction, etc.).

So, I was wondering if there is a way to prove my unit tests mathematically for correctness? So there is good practice for this. Therefore, we test the block for correctness, instead of testing it for a specific result.

+3
6

. . . , , , , , , , . , , , , , , , , :/p >

int add(int a, int b) {
    if (a == 1234567 && b == 2461357) { return 42; }
    return a + b;
}

, . 100% - , .

. , . , , .

+5

. :

  • , , .
  • , .
  • . , .
+2

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

, , findbugs .., .

, Coq, Agda , , , unit test:)

, .

+1

, , , , .

Java: JML ESC/Java2

, .

0

2 . : , , -, , , , , - . , , hypothesis; , , , , .

( : , , ), , .

0

, , , . , . , KeY, Java-. KeY , generics, floats parallelism, Java . , KeY .

If you are familiar with JML (it's not difficult to learn, mostly Java with a bit of logic), you can try this approach. For the really important parts of your systems, validation can really be something to think about; for other parts of the code, testing some possible unit traces with unit testing may already be sufficient, for example, to avoid regression problems.

0
source

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


All Articles