测试数据结构,用于改善测试可读性。

huangapple go评论64阅读模式
英文:

Test Data structure for Tests to improve test readability

问题

我有一个与测试数据和测试类结构相关的问题。
我有一个测试类里面有几个测试。
现在,给定的预期数据和实际数据是我几乎为每个测试创建的结构。
我编写我的测试看起来像这样:

    private static final List<String> EXPECTED_DATA = List.of("a","b","c","d","e","f");
    @Test
    void shouldReturnAttributes() {
        Service service = new Service();
        List<String> actualData = service.doSomething();

        assertThat(actualData).containsExactlyInAnyOrderElementsOf(TestData.EXPECTED_DATA);
    }

目前,我将我的测试数据设置为测试类开始的常量。
一旦添加了更多的测试,越来越多的常量开始出现在测试类的开头,导致不断向下滚动才能到达实际的测试。
所以,一个朋友提出了这样一个想法,即如果常量不在测试类的顶部,测试会更易读。
从多个测试类中使用的测试数据被移到一个CommonTestData类中,其余仅从特定类中使用的测试数据按如下方式结构化。

我们将它们放在一个私有静态类TestData内,代码如下所示:

class ProductAttributeServiceTest {
@Test
    void shouldReturnAttributes() {
        Service service = new Service();
        List<String> actualData = service.doSomething();

        assertThat(actualData).containsExactlyInAnyOrderElementsOf(EXPECTED_DATA);
    }
    
    private static class TestData {
        private static final List<String> EXPECTED_DATA = List.of("a","b","c","d","e","f");
    }
}

你能提出另一种做法吗?
你如何构造你的测试数据以提高测试的可读性?

英文:

I have one question related to the test data and the test-class structure.
I have a test class with few tests inside.
Now, the given and the expected data are structures that I create for almost every test.
I write my tests to look like this:

    private static final List&lt;String&gt; EXPECTED_DATA = List.of(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;);
    @Test
    void shouldReturnAttributes() {
        Service service = new Service();
        List&lt;String&gt; actualData = service.doSomething();

        assertThat(actualData).containsExactlyInAnyOrderElementsOf(TestData.EXPECTED_DATA);
    }

Currently, I set my test data at the beginning of the test class as constants.
Once more tests are added, more constants start appearing at the beginning of the test class, resulting in a lot of scrolling down to reach the actual tests.
So, a friend came up with an idea that if the constants are not on the top of the test class, the tests will be more readable.
The test data that are used from more that one test class are moved to a CommonTestData class and the rest test data that are used only from a specific class we structured them as follows.

We moved them inside a private static class TestData and the code looks like this:

class ProductAttributeServiceTest {
@Test
    void shouldReturnAttributes() {
        Service service = new Service();
        List&lt;String&gt; actualData = service.doSomething();

        assertThat(actualData).containsExactlyInAnyOrderElementsOf(EXPECTED_DATA);
    }
    
    private static class TestData {
        private static final List&lt;String&gt; EXPECTED_DATA = List.of(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;);
    }
}

Could you propose another way of doing that?
How do you structure your test data to improve test readability?

答案1

得分: 1

一种方法是将测试数据放入文本、CSV类型的文件中。

将测试数据放入文件中将有机会使用特定的测试场景命名文件。这将最终增加测试数据的可读性。
而且这些文件也可以按照测试场景进行文件夹结构的排列。

一旦测试数据被整理到文件中,那么这些文件的所有权和维护工作可以转交给领域专家,测试数据可以根据需要直接进行添加/修改。

可以创建一个测试数据供应器类,它将从文件中读取测试数据并提供给测试。

因此,测试只需通过类似以下API与此供应商类进行通信:

public string getTestData(string 测试场景名称)

如果每个常量的测试数据并不是很大,无法放入单独的文件中,那么一个基于 JSON 或 YAML 的配置文件可以完成任务,其中每个数据常量都有一个字段。

英文:

One approach could be put the test data in text,csv type files..

Putting the test data in file would give chance to name the files with specific test scenarios. Which will eventually add more readability to the test data.
And these files can be arranged in test scenario based folder structures as well.

Once test data been arranged in files then the owner ship and maintenance of these files can be transferred to domain experts and test data can be added/ modified directly on need basis.

One test data supplier class can be created which will do task of reading the test data from the files and provide to test.

So tests would have communication only to this supplier class through an API like,

Public string getTestData(string test scenario name)

And if the test data on each constant is not that much big to put in separate files.. then a single json yml based config file having one field for each data constant would do the job.

huangapple
  • 本文由 发表于 2020年9月24日 21:16:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64047294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定