当前位置:网站首页>Tips of this week 135: test the contract instead of implementation
Tips of this week 135: test the contract instead of implementation
2022-07-07 17:54:00 【-Flying crane-】
As TotW#135 Originally published in 2017 year 6 month 5 Japan
from James Dennett A literary creation
“ If you have a true friend , Then you have more than what you have ”—— Thomas · Fuller
C++ One uses public , The protection of , Private and friend detailed access control mechanisms . The test code has its own rules for using these devices ,GoogleTest Use it FRIEND_TEST Macros to expand them . Use FRIEND_TEST It should be the last resort , Not a priority .
Test Convention
We write tests to find errors in the implementation of component conventions , Or let us have enough confidence to believe that there is no such mistake . Using test driven development (TDD) when , We also write tests to help us design conventions . Tests that rely on unspecified content of components are fragile , Even if the production code works properly , It is also easy to report failure .
Give priority to testing through the public interface of components . More generally speaking , Tests should verify the conventions of components , And like any other client , No assumptions should be made that exceed the guarantee .
Provide technology from test access
Many techniques can be used to allow test code the necessary access to complete its work . Here are some , Roughly from good to bad .
Add public API
When testing through the smallest interface , Sometimes it's hard to get enough coverage . If your component is implementing a very narrow interface specified by the base class ( for example , only one ProcessItem Interface of virtual function ) And it is unrealistic to gain enough confidence from tests that only use this interface , Please consider creating a new 、 Testable components , Including implementation details . Then classes containing virtual functions can be very simple , Only minimal testing is required . BUILD Visibility can be used to limit the use of your implementation classes ( If necessary and your build system supports it ).
If the test depends only on oneortwo private functions , Consider these functions as part of the public interface . It's not bad : in any case , You all need them to have clearly documented interfaces , And other clients ( It's not just testing ) You may find them very useful . If after considering , You decide that a function is really only used for testing , Then it should be recorded , And may be ForTesting Suffix naming .
Use peers to avoid exposing implementations
If the test still needs access to private implementation details , Please create a test peer ( Sometimes called test pairing ). The test peer is a friend class of the tested class , Usually in _test.cc The document defines ( Although some people prefer to define it in the same file as the friend class ), And the test code used to provide controlled access to the class to be tested . Test peers will not exist in anonymous namespaces , Because its exact name needs to match the friend declaration , But the rest of the test code can be in anonymous namespaces as usual . The name of the test peer class is usually Peer ending .
The last resort : Use FRIEND_TEST
Although it is very common in old code , however FRIEND_TEST Should not be used in new code . It introduces reverse coupling , Make the production code header file dependent on the details of the relevant unit tests . It forces tests to move out of anonymous namespaces . Every FRIEND_TEST Both grant a test function unrestricted access to the class under test ; In long test functions , It is difficult to find out where the test modifies the state of the class under test . It requires the use of GoogleTest Provide an unusual header file to include in the production code , But almost all GoogleTest For testing only . Finally, its scalability is very poor , When adding new tests, you need to add new FRIEND_TEST Add to the header file of production . In practice , This usually leads to long header files FRINED_TEST block .
Don't let the whole testing device become friends
It is strongly recommended not to make the entire test device a class to be tested ( With friend class MyClassTest) Friends . Compare with the above options , It allows the entire test setup ( But not the test itself , They are subclasses of test devices ) Unrestricted and uncommented access to each member of the class under test , This means that the reader testing the code has no visible clues about when to break the package . It also forces the test device to be outside the anonymous namespace . Compared with friend devices , Testing peers makes the code more self explanatory , And the code author only needs to spend a little extra work .
Summary of recommendations
- Test the client interface of components first , And ensure that the test is independent of the details of the private implementation .
- If the client interface does not meet the requirements of completely running the unit to be tested , Will be testable , The sub components that may only be used for testing are decomposed .
- Sometimes it makes sense to add to public interfaces to make components testable .
- If necessary, , Please use the test peer instead of FRIEND_TEST Access private members from tests .
- Don't pretend to be friends with the whole test . Use one of the more targeted methods mentioned above .
边栏推荐
- Use onedns to perfectly solve the optimization problem of office network
- 基于PyTorch利用CNN对自己的数据集进行分类
- Simple loading animation
- js拉下帷幕js特效显示层
- Functions and usage of viewflipper
- Native JS verification code
- Slider plug-in for swiper left and right switching
- 基于RGB图像阈值分割并利用滑动调节阈值
- TaffyDB开源的JS数据库
- Dateticket and timeticket, functions and usage of date and time selectors
猜你喜欢
Yarn capacity scheduler (ultra detailed interpretation)
What is agile testing
2021-06-28
DatePickerDialog and trimepickerdialog
zdog. JS rocket turn animation JS special effects
Vscode three configuration files about C language
仿今日头条APP顶部点击可居中导航
Deep learning - make your own dataset
Numberpick的功能和用法
Pro2:修改div块的颜色
随机推荐
js拉下帷幕js特效显示层
swiper左右切换滑块插件
基于PyTorch利用CNN对自己的数据集进行分类
ViewSwitcher的功能和用法
actionBar 导航栏学习
[4500 word summary] a complete set of skills that a software testing engineer needs to master
DatePickerDialog and trimepickerdialog
【4500字归纳总结】一名软件测试工程师需要掌握的技能大全
什么是敏捷测试
How to implement safety practice in software development stage
Taffydb open source JS database
深度学习机器学习各种数据集汇总地址
MRS离线数据分析:通过Flink作业处理OBS数据
TaffyDB开源的JS数据库
Machine vision (1) - Overview
Use seven methods to enhance all the images in a folder
本周小贴士#135:测试约定而不是实现
Cf:c. factors and powers of two [DP + sort + Select Board + select several numbers equal to the minimum number of known sums]
线上比赛相关规则补充说明
百度地图自定义样式向右拖拽导致全球地图经度0度无法正常显示