如何在Python中指定和強制執行介面規格?

王林
發布: 2023-08-27 14:09:14
轉載
1163 人瀏覽過

如何在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 languages such as C and Java. It describes the prototypes for the langk 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 −

#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.

使用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中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!