36

开发者测试; Story 场景树、Property-Based Testing、Contract Testing - Cloud-Nativ...

 5 years ago
source link: https://www.deva9.com/cloud-native/%e5%bc%80%e5%8f%91%e8%80%85%e6%b5%8b%e8%af%95-story-%e5%9c%ba%e6%99%af%e6%a0%91%e3%80%81property-based-testing%e3%80%81contract-testing/?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

开发者测试; Story 场景树、Property-Based Testing、Contract Testing

colton-sturgeon-1052645-unsplash.jpg

过去我们一直的都在倡导著开发者需进行所谓的单元测试。

单元测试是个相当有效的工程实践, 这是无庸置疑的。但是, 全世界大部分的开发人员是都没有在写单元测试的。

主要的原因如下:

  1. 粒度过小的单元测试用例是很难进行维护与扩展的:

针对这个问题, 我们必需要有个轻量级、可视化的方法, 可协助开发人员找到真正值得进行测试的关键路径。

只针对关键路径进行测试用例的开发, 才不会形成粒度过小的测试用例;  粒度过小的测试用例将会难以的进行维护, 最终只好宣布放弃。

Story 场景树是个相当好的方法,可协助开发人员找到真正值得进行测试的关键路径。

20170705000736940.png
Story 场景树

2. 单元测试的写法尚不够简洁:

单元测试往往都会写得过于的片断, 无形中就会很容易的遗漏掉应该测试的场景。

针对这个问题, Property-Based Testing 是个相当好的解决方案。

Property-Based Testing 能以 @given 的方式生成测试数据; 而能以测试数据驱动的方式, 在单一的测试用例中完成多个不同的场景的测试; 使得开发人员能以少量的测试代码, 完成更有效、更完整的测试场景的测试。

@given(st.lists(st.integers())) 
def test_sort(XS):
    sorted_xs = quicksort(XS)
    assert isinstance(sorted_xs, list)
    assert Counter(XS) == Counter(sorted_xs) 
    assert all(
        x <= y for x, y in
        zip(sorted_xs, sorted_xs[1:]
    )

3. 单元测试的 Mock 并没有发挥其应有的功能

单元测试的 Mock 的机制, 并没有能真正的高效、有效的使得开发人员在开发的阶段, 就能得知自身代码的变更对其他开发人员所负责的代码的影响。

针对这个问题, Contract Testing 是个相当好的解决方案。

%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7-2019-08-09-%E4%B8%8B%E5%8D%885.02.30.png
在 Pack 的配置文件中, 配置好 Billing Service 与 User Service 间交互的命令、请求与响应。Pack 便会是 User Service 的代理; Billing Service 便可独立的与 Pack 进行整合测试。Billing Service 的开发人员在开发的阶段就能得知自身代码的变更对 User Service 的影响。
%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7-2019-08-09-%E4%B8%8B%E5%8D%885.04.11.png
User Service 的开发人员在开发的阶段, 因在 User Service 上代码的变更, 而使 Billing Service 无法获取到所预期的响应。

单元测试启蒙了我们应该如何的去开发高质量的代码。 

开发者测试; Story 场景树, Property_Based Testing , Contract Testing; 使得我们能真正高效率、有效的去开发高质量的代码。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK