4

Pytest 源码解读 [0] - Kickoff 篇

 2 years ago
source link: https://markshao.github.io/2019/08/10/pytest-source-analysis/
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

Pytest 源码解读 [0] - Kickoff 篇

Pytest 源码解读

之前定的目标,2019 年要完成 pytest 的源码解读,实在是因为之前太忙耽搁下来,眼看已经到了8月份,再不开始的话估计就要黄了 (之前信誓旦旦立的Flag不能轻易打破)。其实时间挤挤也总是有的,不能再找借口了。而且估计未来也不会再去看 pytest 的source code 了,就当给自己的 Pytest 这段经历做个总结吧。

Python 在测试框架的选择上主要是3个

  • unittest
  • pytest

unittest 是 python 自带的一个轮子,一般常用的需求都能满

nose (https://nose.readthedocs.io/en/latest/) 是一个第三方的测试框架,之前在emcgreenplum 团队一起搞了一个项目是基于 nose 的,感觉功能要比 unittest 要复杂一些,不够我没有用的太深入,也没资格点评

pytest 是我加入到 Splunk 的时候开始接触的,Splunk 当时内部有一个叫 Helmut 的框架,就是基于 pytest 来打造的 。这也是我第一次知道Pytest,刚开始最不习惯的使用 Fixture, 后来才慢慢品味到了 Fixture 这种 Lazy Loading 的设计价值。后来基本上我就一直在用 Pytest 了,而且在依图的时候团队写了一个 Fixture Factory 的 module ,进一步简化了 Fixture 的使用和管理复杂度。当然这次主要是聚焦在 Pytest 的源码分析上,至于 Pytest 的使用,大家自己看 Pytest 的官方文档吧。

一些周边信息

Holger Krekel
  • Pytest 的作者,其实 Pytest 最开始的名字叫 Py.test
  • 他是个德国人,同时还是 PyPytox 的作者
Pluggy
  • (https://github.com/pytest-dev/pluggy)
  • Pytest 的核心实际的基于 Pluggy 这个 plugin framework 的,实际上 pytest 本身就是由一个一个插件组成的
  • 本来 pluggy 的代码是在 pytest 的 repo 里,后来迁移了出来,作为一个独立的项目。Pluggy 作为一个独立的plugin framework 来看也是很优雅的存在

源码分析计划

主要是分两步走

  1. Pluggy 的分析,这个是 Pytest 的核心引擎
  2. Pytest 分析

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK