How to write unit tests in specification form?

In many situations, it’s hard for me to come up with good unit test names for classes and methods. I usually try to follow the form:

public class TestContext
{
    [Fact]
    public void WhenThis_DoThat()
    {
    }
}

Some put the words Given, When, and Then on parts that need to be explicit. I like it because it seems that unit test makes it clearer what it is testing. Besides looking at BDD toolkits, I need to know how this can work with the usual old xUnit tools.

It’s especially hard for me to work with scripts like this:

When the application starts, the main form loads, and the user sees a list of links that the user can click.

or perhaps it is better to use a use case:

The user can select a link from the list of links.

, , . unit test.

, , ?

+3
5

BDD: http://github.com/orfjackal/tdd-tetris-tutorial

, . , "Given/When", - "Then". JUnit , :

@RunWith(NestedJUnit4.class)
public class WerewolfTest extends Assert {
    public class Given_the_moon_is_full {
        @Before public void When_you_walk_in_the_woods() {
            ...
        }
        @Test public void Then_you_can_hear_werewolves_howling() {
            ...
        }
        @Test public void Then_you_wish_you_had_a_silver_bullet() {
            ...
        }
    }
    public class Given_the_moon_is_not_full {
        @Before public void When_you_walk_in_the_woods() {
            ...
        }
        @Test public void Then_you_do_not_hear_any_werewolves() {
            ...
        }
        @Test public void Then_you_are_not_afraid() {
            ...
        }
    }
}
+6

BDD Dan North:

"!" , agiledox, , . JUnit , :

public class CustomerLookupTest extends TestCase { testFindsCustomerById() { ... } testFailsForDuplicateCustomers() { ... } ... }

- :

CustomerLookup – finds customer by id – fails for duplicate customers – ...

"" , . , .

, , , , . , , -, , .

. !

+4

, ? A unit test - , .

FormHasLinks?

LoadForm?//

, , , , , .

0

, . , : , .

, Backspace :

  • Backspace after space
  • textrun
  • Backspace
  • Backspace
  • .

That way, you can see more or less which scenarios are being tested (but he is also not trying to tell you what the expected behavior in each scenario is).

0
source

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


All Articles