tarantool / cartridge-springdata

Spring Data Tarantool
Other
18 stars 7 forks source link

Add tests name convention and change tests to this convention #47

Closed wey1and closed 3 years ago

wey1and commented 3 years ago

I suggest the following convention: Tests names should be correspond the next pattern:

test_[methodName/scenarioName]_should[Action]_if[Condition]

[methodName/scenarioName]_ - is optional. if[Condition] - is optional. You can omit this if the execution is straightforward.

Also, inside the body of the test there should be comments of the following format:

    //given
    [some code that defines conditions]
    //when
    [some code that executes the test case]
    //then
    [some code with assertions]

Example: If we have method like this:

    public String hello(String name) {
        return "Hello, " + name;
    }

Test for this method:

    @Test
    public void test_hello_shouldReturnHelloWithName() {
        //given
        String name = "John";

        //when
        String welcome = hello(name);

        //then
        assertThat(welcome).isEqualTo("Hello, John");
    }

If we have method like this:

    public String helloWithCondition(@Nullable String name) {
        if (name == null) {
            return "Hello Stranger";
        }
        return "Hello, " + name;
    }

Test for this method can be like this:

    @Test
    public void test_helloWithCondition_shouldReturnHelloStranger_ifNameIsNull() {
        //given
        String name = null;

        //when
        String welcome = helloWithCondition(name);

        //then
        assertThat(welcome).isEqualTo("Hello, Stranger");
    }

It's good when this pattern can be included into test method name but it's not always possible. When the description is too large then test case can contain additional info in comment. Also consider this points:

akudiyar commented 3 years ago

Thank you for the proposal!

I suggest using only test_ for method prefixes instead of some names, the test method name already includes the "shouldSomething" part which describes it.

wey1and commented 3 years ago

Thank you for the proposal!

I suggest using only test_ for method prefixes instead of some names, the test method name already includes the "shouldSomething" part which describes it.

Done

vrogach2020 commented 3 years ago

I'll try to finalize the agreement:

1 Name of test method must not violate linter rules and generate compiler warnings (must not contain special characters, be longer than 120 chars etc).

2 These points of testcase must be readable and simple:

It's good when they can be included into test method name but it's not always possible. When the description is too large then testcase should contain a comment on points mentioned above.

3 Do not perform a total refactoring of existing codebase. Just leave the code better than it was after making changes.

vrogach2020 commented 3 years ago

Also maybe use this pattern ?

@akudiyar @wey1and

test_[method]_when[condition]_then[reaction]
For example:
test_helloWorld_whenCalled_ThenPrintHelloWorld()

instead of this

test_[methodName/scenarioName]_should[Action]_if[Condition]

[methodName/scenarioName]_ - is optional.
if[Condition] - is optional. You can omit this if the execution is straightforward.
wey1and commented 3 years ago

Thanks to all! Based on the discussion, I brought the initial version to the final one.