简体   繁体   中英

when to use Property-Based Testing?

I am trying to learn Propery-Based Testing(PBT)I think I know how to implement it but when should I apply PBT?

For example in this case I am trying to compare if the function getCurrentName() returns the expected name. Should I randomize this test?

@Test
public void getNameTest() {
    assertEquals(nameProxy, proxyFoto.getCurrentName());
}

Your question is so generic that it cannot have a specific answer. I suggest you look at some of the stuff that has been written about how to come up with good properties, eg https://johanneslink.net/how-to-specify-it/

As for your concrete example, the answer if writing a property for the current name makes sense depends on a few things:

  • How does the name get into the proxy object? Is there a reasonable chance that depending on the shape/length/encoding etc of the name the behaviour is different?
  • What is the name being used for? Should it be normalised, formatted, shortened or processed in any specific way?

Properties and PBT are about finding and falsifying assumptions about the behaviour of your code under test. If there is nothing that you might get wrong, any form of automated testing can be considered unnecessary. If there are quite a few edge cases and paths that could show unexpected behaviour, then PBT looks like a worthwhile approach.

As a pragmatic recommendation: Start to translate some of your example tests into properties and see which ones are pulling their weight. Then try to add additional properties, eg by using ideas from the article I linked to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM