如何在Python中指定和强制执行接口规范?

王林
发布: 2023-08-27 14:09:14
转载
1273人浏览过

如何在python中指定和强制执行接口规范?

Let us first see what is an interface spec. The interface spec is an interface specification for a module provided by programming languages such as C++ and Java. It describes the prototypes for the methods and functions of the module.

The abc module

在Python 2.6中引入了abc模块,用于定义抽象基类(ABCs)。使用isinstance()和issubclass()来检查一个实例或一个类是否实现了特定的抽象基类。通过这个,collections.abc模块定义了一组有用的抽象基类,如Iterable、Container和MutableMapping。

collections模块有一些从ABCs派生的类。collections.abc子模块有一些ABCs,可以用来测试一个类或实例是否提供特定的接口。

The rewards of interface specifications can be attained by a suitable test discipline in Python −

立即学习Python免费学习笔记(深入)”;

Test Suite

A good test suite for a module can both provide a regression test and serve as a module interface specification and a set of examples. Many Python modules can be run as a script to provide a simple self test.

Stub Emulations

可以翻译为:

存根仿真

Even modules which use complex external interfaces can often be tested in isolation using trivial “stub” emulations of the external interface.

MagicStudio
MagicStudio

图片处理必备效率神器!为你的图片提供神奇魔法

MagicStudio 102
查看详情 MagicStudio

使用doctest和unittest模块创建测试套件

The doctest and unittest modules or third-party test frameworks can be used to construct exhaustive test suites that exercise every line of code in a module.

doctest模块搜索看起来像是交互式Python会话的文本片段,然后执行这些会话以验证它们是否与显示的完全一致。

unittest模块支持测试自动化,共享测试的设置和关闭代码,将测试聚合到集合中,并使测试与报告框架独立。

构建大型复杂应用程序

A suitable testing discipline can help build large complex applications in Python as well as having interface specifications would.

测试驱动开发

编写测试套件非常有帮助,您可能希望设计您的代码以便轻松进行测试。一种越来越流行的技术,即测试驱动开发,要求您首先编写测试套件的部分内容,然后再编写实际的代码。

以上就是如何在Python中指定和强制执行接口规范?的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号