标签:wiki param 文件的 spec fail type test led 第一个
1、这个包下面有哪些东西?
2、为什么说 pytest是一个包?
这个文件夹下有一个文件,叫__init__.py,这是一个包标识文件。即有这个文件的文件夹,python中不叫文件夹,叫包或者模块。
这个模块__init__.py的作用(简单说一下结论)
(1)做包标识工具。防止不同包中存在相同的.py文件(之后叫“模块”),导入时发生冲突
(2)一些导包的操作可以在这里做。比如pytest包里面,将导入__pytest中的包都放在__init__.py里面了。
(3)每一个父包和子包都有__init__.py模块。python3中,不管用户手动生成与否,都会默认有这个包(没有生成的时候,隐式存在),python2必须显示的有这个模块。
(4)导入父包,就只执行父包下的__init__.py,导入子包,先执行父包下的__init__.py,再执行子包下的__init__.py
(5)导包之后,不管要调用包里面哪个类、函数,首先第一个执行的都是__init__.py里面的逻辑。
3、pytest包中的__init__.py源码走读。
首先,把这段代码插进来看看。
"""pytest: unit and functional testing with Python.""" from . import collect from _pytest import __version__ from _pytest.assertion import register_assert_rewrite from _pytest.cacheprovider import Cache from _pytest.capture import CaptureFixture from _pytest.config import cmdline from _pytest.config import console_main from _pytest.config import ExitCode from _pytest.config import hookimpl from _pytest.config import hookspec from _pytest.config import main from _pytest.config import UsageError from _pytest.debugging import pytestPDB as __pytestPDB from _pytest.fixtures import _fillfuncargs from _pytest.fixtures import fixture from _pytest.fixtures import FixtureLookupError from _pytest.fixtures import FixtureRequest from _pytest.fixtures import yield_fixture from _pytest.freeze_support import freeze_includes from _pytest.logging import LogCaptureFixture from _pytest.main import Session from _pytest.mark import Mark from _pytest.mark import MARK_GEN as mark from _pytest.mark import MarkDecorator from _pytest.mark import MarkGenerator from _pytest.mark import param from _pytest.monkeypatch import MonkeyPatch from _pytest.nodes import Collector from _pytest.nodes import File from _pytest.nodes import Item from _pytest.outcomes import exit from _pytest.outcomes import fail from _pytest.outcomes import importorskip from _pytest.outcomes import skip from _pytest.outcomes import xfail from _pytest.pytester import Pytester from _pytest.pytester import Testdir from _pytest.python import Class from _pytest.python import Function from _pytest.python import Instance from _pytest.python import Metafunc from _pytest.python import Module from _pytest.python import Package from _pytest.python_api import approx from _pytest.python_api import raises from _pytest.recwarn import deprecated_call from _pytest.recwarn import WarningsRecorder from _pytest.recwarn import warns from _pytest.runner import CallInfo from _pytest.tmpdir import TempdirFactory from _pytest.tmpdir import TempPathFactory from _pytest.warning_types import PytestAssertRewriteWarning from _pytest.warning_types import PytestCacheWarning from _pytest.warning_types import PytestCollectionWarning from _pytest.warning_types import PytestConfigWarning from _pytest.warning_types import PytestDeprecationWarning from _pytest.warning_types import PytestExperimentalApiWarning from _pytest.warning_types import PytestUnhandledCoroutineWarning from _pytest.warning_types import PytestUnhandledThreadExceptionWarning from _pytest.warning_types import PytestUnknownMarkWarning from _pytest.warning_types import PytestUnraisableExceptionWarning from _pytest.warning_types import PytestWarning set_trace = __pytestPDB.set_trace __all__ = [ "__version__", "_fillfuncargs", "approx", "Cache", "CallInfo", "CaptureFixture", "Class", "cmdline", "collect", "Collector", "console_main", "deprecated_call", "exit", "ExitCode", "fail", "File", "fixture", "FixtureLookupError", "FixtureRequest", "freeze_includes", "Function", "hookimpl", "hookspec", "importorskip", "Instance", "Item", "LogCaptureFixture", "main", "mark", "Mark", "MarkDecorator", "MarkGenerator", "Metafunc", "Module", "MonkeyPatch", "Package", "param", "PytestAssertRewriteWarning", "PytestCacheWarning", "PytestCollectionWarning", "PytestConfigWarning", "PytestDeprecationWarning", "PytestExperimentalApiWarning", "Pytester", "PytestUnhandledCoroutineWarning", "PytestUnhandledThreadExceptionWarning", "PytestUnknownMarkWarning", "PytestUnraisableExceptionWarning", "PytestWarning", "raises", "register_assert_rewrite", "Session", "set_trace", "skip", "TempPathFactory", "Testdir", "TempdirFactory", "UsageError", "WarningsRecorder", "warns", "xfail", "yield_fixture", ]
(1)导入了工具包。这些包是这个框架的核心
(2)set_trace = __pytestPDB.set_trace,是python自带的pdb断点工具。试了一下,这个直接使用属性,不是使用方法pdb.set_trace(),起不到调试的作用,该执行完的还是执行完了。
(3)__all__ ,就相当于白名单的作用,意思是,如果测试的时候,import pytest 那么,只能导入pytest包下面的 __init__.py 中,__all__ 中列出来类、包或者方法,其他类、包或者方法,即使在pytest包 中import了,我们在import pytest的时候 也是不会被我们连带着导入的。
参考资料:
关于python的debug:https://www.liaoxuefeng.com/wiki/1016959663602400/1017602696742912
关于__all__的白名单功能 :https://www.cnblogs.com/hester/p/10546235.html
标签:wiki param 文件的 spec fail type test led 第一个
原文地址:https://www.cnblogs.com/ansonwan/p/14471149.html