pytest可以有几个mysql下载后没有setupp

继续文档的第二章
(一)pytest中可以在命令行中静态/动态添加option,这里没什么好讲的,略过...
& & & & 这里面主要讲下如何试用skip/xfail,还有incremental(包含一些列的测试步骤,如果某一步出错后,该步骤之后的所有步骤都没有任何意义了,xfail掉)
1)首先来看下怎样通过命令行来skip一些case,首先要添加个option,然后使用@pytest.mark.skipif( condition) , xfail使用@pytest.mark.xfail( condition,reson,run,raise),这里不详细介绍了
import pytest
def test_func_fast():
print 'fast'
@pytest.mark.skipif(not pytest.config.getoption("--runslow"))
def test_func_slow_1():
print 'skip slow'
@pytest.mark.xfail(not pytest.config.getoption("--runslow"))
def test_func_slow_2():
print 'xfail slow'
2)然后来看下如何实现incremental,首先在测试文件的目录下创建conftest.py, 代码如下:
# content of conftest.py
import pytest
def pytest_runtest_makereport(item, call):
if "incremental" in item.keywords:
if call.excinfo is not None:
parent = item.parent
parent._previousfailed = item
def pytest_runtest_setup(item):
if "incremental" in item.keywords:
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail("previous test failed (%s)" %previousfailed.name)
然后创建test_steps.py
# content of test_step.py
import pytest
@pytest.mark.incremental
class TestUserHandling:
def test_login(self, fix_err):
def test_modification(self):
def test_deletion(self):
def test_normal():
执行结果,在test_modification后的test_deletion 执行为x
3)由于pytest中的Error也划分到Fail中,而Unittest中是单独讲Error独立出来,说明由于case中存在异常或错误导致case未能正常运行,其实在pytest中也可以做到这样,只需要加上一点儿代码,如下:
# content of test_step.py
import pytest
@pytest.fixture
def fix_err(x):
@pytest.mark.incremental
class TestUserHandling:
def test_modification(self):
def test_login(self, fix_err):
raise RuntimeError("error")
except AssertionError,e:
fix_err(e)
def test_deletion(self):
def test_normal():
其执行效果如下:
(二)、这里讲下关于fixture,fixture是pytest特有的功能,它用pytest.fixture标识,定义在函数前面。在你编写测试函数的时候,你可以将此函数名称做为传入参数,pytest将会以依赖注入方式,将该函数的返回值作为测试函数的传入参数。例如上面的例子fix_err.
1)准备函数usefixtures
@pytest.fixture()
def cleandir():
newpath = tempfile.mkdtemp()
os.chdir(newpath)
@pytest.mark.usefixtures("cleandir")
class TestDirectoryInit:
def test_cwd_starts_empty(self):
assert os.listdir(os.getcwd()) == []
with open("myfile", "w") as f:
f.write("hello")
2)销毁函数addfinalizer
@pytest.fixture()
def smtp(request):
smtp = smtplib.SMTP("")
def fin():
print ("teardown smtp")
smtp.close()
request.addfinalizer(fin)
return smtp
# provide the fixture value
获取调用函数信息: request&使用fixture标记函数后,函数将默认接入一个request参数,它将包含使用该fixture函数的函数信息
3)fixture的参数,有时我们需要全面测试多种不同条件下的一个对象,功能是否符合预期。我们可以通过params参数来指定传入的参数。
class SMTP:
def __init__(self, smtp, sender, receiver):
self.smtp = smtp
self.sender = sender
self.receiver = receiver
def __del__(self):
self.smtp.close()
@pytest.fixture(params=[["", "", ""], ["mail.python.org", "from@python.org", "to@python.org"])
def smtp(request):
return SMTP(#param)
def test_mail(smtp):
message = "hello"
assert smtp.sendmail(message)
4)fixture的作用域:function、module、session ,autouse=True使得函数将默认执行
work_dir = "/tmp/app"
@pytest.fixture(session="session", autouse=True)
def clean_workdir():
shutil.rmtree(work_dir)
os.mkdir(work_dir)
os.chrdir(work_dir)
fixture的存在使得我们在编写测试函数的准备函数、销毁函数或者多个条件的测试提供了更加灵活的选择。py.test --fixtures 可以查看所有的fixtures,包含目录下及子目录下的conftest.py
pytest.hookimpl这个没太看明白,应该就是个钩子函数,以后弄明白了再说,今天就到这儿了,下一节再说这个参数化的问题
阅读(...) 评论()自动化测试框架pytest中文文档:pytest参考文档 - xdist: pytest分布式测试插件 - 推酷
自动化测试框架pytest中文文档:pytest参考文档 - xdist: pytest分布式测试插件
pytest-xdist插件有增加了一些测试执行方式:
Looponfail:多次在子进程运行测试。每次运行后,pytest在文件变化之后重新运行fail的测试,直到所有的测试都通过。然后重新执行所有测试。
多进程负载均衡:如果你有多个CPU或主机,可以使用它们的组合运行测试。
多平台覆盖:您可以指定不同的Python解释器或不同的平台并行运行测试 。
在远程运行测试,pytest需要同步程序的源代码到远端。所有的测试结果会显示在本地。您可以指定不同的Python版本和解释器。
安装xdist插件
os.path.expanduser用来替换~为用户的真实目录。
#pip install pytest-xdist
Downloading/unpacking pytest-xdist
Downloading pytest-xdist-1.10.tar.gz
Running setup.py (path:/tmp/pip_build_root/pytest-xdist/setup.py) egg_info for package pytest-xdist
no previously-included directories found matching '.hg'
Downloading/unpacking execnet&=1.1 (from pytest-xdist)
Downloading execnet-1.2.0.tar.gz (163kB): 163kB downloaded
Running setup.py (path:/tmp/pip_build_root/execnet/setup.py) egg_info for package execnet
warning: no files found matching 'conftest.py'
Requirement already satisfied (use --upgrade to upgrade): pytest&=2.4.2 in /usr/lib/python2.7/site-packages (from pytest-xdist)
Requirement already satisfied (use --upgrade to upgrade): py&=1.4.22 in /usr/lib/python2.7/site-packages (from pytest&=2.4.2-&pytest-xdist)
Installing collected packages: pytest-xdist, execnet
Running setup.py install for pytest-xdist
no previously-included directories found matching '.hg'
Running setup.py install for execnet
warning: no files found matching 'conftest.py'
Successfully installed pytest-xdist execnet
Cleaning up...
多CPU: py.test -n NUM
python子进程: py.test -d --tx popen
python=python2.4; py.test -d --tx 3*popen
python=python2.4
looponfail模式:py.test -f。文件改变的目录由根目录及其子目录的looponfailingroots配置。可以在配置文件中修改:
# content of a pytest.ini, setup.cfg or tox.ini file
looponfailroots = mypkg testdir
发送测试到远端SSH账号:py.test -d --tx ssh=myhostpopen --rsyncdir mypkg mypkg。多个--rsyncdir可以分发到多个目录。
发送测试到远端Socket服务器。代码参见:
,分发:py.test -d --tx socket=192.168.1.102:8888 --rsyncdir mypkg mypkg。
多平台执行:py.test --dist=each --tx=spec1 --tx=spec2
ini文件中制定测试执行环境:
addopts = -n3
addopts = --tx ssh=myhost//python=python2.5 --tx ssh=myhost//python=python2.6
然后执行分发:
# py.test --dist=each
# py.test --dist=each
现在你想修改成在路径后面添加一个斜杠,即把'/root'修改成'/root/',可以补丁如下:
rsyncdirs = . mypkg helperpkg
rsyncignore = .hg
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致I'm creating the test cases for web-tests using Jenkins, Python, Selenium2(webdriver) and Py.test frameworks.
So far I'm organizing my tests in the following structure:
each Class is the Test Case and each test_ method is a Test Step.
This setup works GREAT when everything is working fine, however when one step crashes the rest of the "Test Steps" go crazy. I'm able to contain the failure inside the Class (Test Case) with the help of teardown_class(), however I'm looking into how to improve this.
What I need is somehow skip(or xfail) the rest of the test_ methods within one class if one of them has failed, so that the rest of the test cases are not run and marked as FAILED (since that would be false positive)
UPDATE: I'm not looking or the answer "it's bad practice" since calling it that way is very arguable. (each Test Class is independent - and that should be enough).
UPDATE 2: Putting "if" condition in each test method is not an option - is a LOT of repeated work. What I'm looking for is (maybe) somebody knows how to use the
to the class methods.
解决方案 I like the general "test-step" idea.
I'd term it as "incremental" testing and it makes most sense in functional testing scenarios IMHO.
Here is a an implementation that doesn't depend on internal details of pytest (except for the official hook extensions):
import pytest
def pytest_runtest_makereport(item, call):
if "incremental" in item.keywords:
if call.excinfo is not None:
parent = item.parent
parent._previousfailed = item
def pytest_runtest_setup(item):
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail("previous test failed (%s)" %previousfailed.name)
If you now have a "test_step.py" like this:
import pytest
@pytest.mark.incremental
class TestUserHandling:
def test_login(self):
def test_modification(self):
def test_deletion(self):
then running it looks like this (using -rx to report on xfail reasons):
(1)hpk@t2:~/p/pytest/doc/en/example/teststep$ py.test -rx
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev17
plugins: xdist, bugzilla, cache, oejskit, cli, pep8, cov, timeout
collected 3 items
test_step.py .Fx
=================================== FAILURES ===================================
______________________ TestUserHandling.test_modification ______________________
self = &test_step.TestUserHandling instance at 0x1e0d9e0&
def test_modification(self):
test_step.py:8: AssertionError
=========================== short test summary info ============================
XFAIL test_step.py::TestUserHandling::()::test_deletion
reason: previous test failed (test_modification)
================ 1 failed, 1 passed, 1 xfailed in 0.02 seconds =================
I am using "xfail" here because skips are rather for wrong environments or missing dependencies, wrong interpreter versions.
Edit: Note that neither your example nor my example would directly work with distributed testing.
For this, the pytest-xdist plugin needs to grow a way to define groups/classes to be sent whole-sale to one testing slave instead of the current mode which usually sends test functions of a class to different slaves.
本文地址: &
我创建测试用例使用詹金斯,Python和Selenium2(webdriver的)和Py.test框架网络的测试。 到目前为止,我组织我在下面的结构测试:每个类与测试用例每个 测试_ 法是测试步骤即可。 此安装程序时一切正常,但是当一步崩溃“测试步骤”的其余部分发疯的伟大工程。我能够包含类(测试用例)与 teardown_class()的帮助里面的失败,但是我正在寻找如何改善这一点。我需要的是某种方式跳过(或xfail)的测试一个类中_ 方法的其余部分如果其中一个出现故障,这样的测试案例,其余不运行,并标记为失败(因为这将是假阳性)谢谢! 更新:我不是在寻找或者回答“这是不好的做法”,因为调用它的方式是很值得商榷的。 (每个测试类是独立的 - 这应该是足够了)。 更新2:把“如果”,在每个测试方法的条件是不是一种选择 - 是重复的工作很多。我正在寻找的是(也许)有人知道如何使用来类的方法。解决方案 我喜欢一般的“测试步骤”的想法。我把它称为是“增量”的测试,其最有意义的功能性测试方案恕我直言。下面是一个不依赖于pytest的内部细节(除官方钩扩展)的实现: 进口pytest高清pytest_runtest_makereport(项目,调用):
如果item.keywords“增量”:
如果call.excinfo不是无:
父= item.parent
母公司._ previousfailed =项目高清pytest_runtest_setup(项目):
previousfailed = GETATTR(item.parent,“_ previousfailed”,无)
如果previousfailed不无:
pytest.xfail(“previous测试失败(%S)”%previousfailed.name) 如果你现在有一个“test_step.py”是这样的: 进口pytest@ pytest.mark.incremental类TestUserHandling:
高清test_login(个体经营):
高清test_modification(个体经营):
高清test_deletion(个体经营):
通过 然后运行它看起来像这样(用-rx对xfail原因报告): (1)HPK @ T2:?/ P / pytest / DOC / EN /例子/一步步测试$ py.test -rx=============================测试环节开始================== ============平台将linux2
- 的Python 2.7.3
pytest-2.3.0.dev17插件:xdist,Bugzilla的,高速缓存,oejskit,CLI,PEP8,COV,超时收集3项test_step.py .FX=================================== FAILURES ============== =====================______________________ TestUserHandling.test_modification ______________________自= LT;在0x1e0d9e0&GT test_step.TestUserHandling实例;
高清test_modification(个体经营):>断言0?断言0test_step.py:8:Asse田===========================短期测试总结信息=================== =========XFAIL test_step.py::TestUserHandling::()::test_deletion
理由是:previous测试失败(test_modification)================ 1失败,1过去了,1 xfailed在0.02秒================= 我使用“xfail”在这里,因为跳跃是相当的错误环境或缺少的依赖关系,错间preTER版本。 编辑:请注意,无论是你的榜样,也没有我的例子就是直接与分布式测试工作。对于这一点,pytest-xdist插件需要增长的方式来定义组/类要发送整个出售给一个测试从属代替通常发送一个类的测试功能不同的从站的当前模式
本文地址: &
扫一扫关注官方微信自动化测试框架pytest中文文档:pytest参考文档 - 基本测试配置 - 推酷
自动化测试框架pytest中文文档:pytest参考文档 - 基本测试配置
待更多汉化
命令行行选项和配置文件设置
下面命令可以查看命令行行选项和ini配置文件的设置
# py.test -h
usage: py.test [options] [file_or_dir] [file_or_dir] [...]
positional arguments:
file_or_dir
-k EXPRESSION
only run tests which match the given substring
expression. An expression is a python evaluatable
expression where all names are substring-matched
against test names and their parent classes. Example:
-k 'test_method or test other' matches all test
functions and classes whose name contains
'test_method' or 'test_other'. Additionally keywords
are matched to classes and functions containing extra
names in their 'extra_keyword_matches' set, as well as
functions which have names assigned directly to them.
-m MARKEXPR
only run tests matching given mark expression.
example: -m 'mark1 and not mark2'.
show markers (builtin, plugin and per-project ones).
-x, --exitfirst
exit instantly on first error or failed test.
--maxfail=num
exit after first num failures or errors.
run pytest in strict mode, warnings become errors.
load configuration from `file` instead of trying to
locate one of the implicit configuration files.
--fixtures, --funcargs
show available fixtures, sorted by plugin appearance
start the interactive Python debugger on errors.
--capture=method
per-test capturing method: one of fd|sys|no.
shortcut for --capture=no.
--runxfail
run tests even if they are marked xfail
reporting:
-v, --verbose
increase verbosity.
-q, --quiet
decrease verbosity.
show extra test summary info as specified by chars
(f)ailed, (E)error, (s)skipped, (x)failed, (X)passed
(w)warnings.
-l, --showlocals
show locals in tracebacks (disabled by default).
--report=opts
(deprecated, use -r)
--tb=style
traceback print mode (long/short/line/native/no).
--full-trace
don't cut any tracebacks (default is to cut).
--color=color
color terminal output (yes/no/auto).
--durations=N
show N slowest setup/test durations (N=0 for all).
--pastebin=mode
send failed|all info to bpaste.net pastebin service.
--junit-xml=path
create junit-xml style report file at given path.
--junit-prefix=str
prepend prefix to classnames in junit-xml output
--result-log=path
path for machine-readable result log.
collection:
--collect-only
only collect tests, don't execute them.
try to interpret all arguments as python packages.
--ignore=path
ignore path during collection (multi-allowed).
--confcutdir=dir
only load conftest.py's relative to specified dir.
--doctest-modules
run doctests in all .py modules
--doctest-glob=pat
doctests file matching pattern, default: test*.txt
test session debugging and configuration:
--basetemp=dir
base temporary directory for this test run.
display pytest lib version and import information.
-h, --help
show help message and configuration info
early-load given plugin (multi-allowed). To avoid
loading of plugins, use the `no:` prefix, e.g.
`no:doctest`.
--trace-config
trace considerations of conftest.py files.
store internal tracing debug information in
'pytestdebug.log'.
--assert=MODE
control assertion debugging tools. 'plain' performs no
assertion debugging. 'reinterp' reinterprets assert
statements after they failed to provide assertion
expression information. 'rewrite' (the default)
rewrites assert statements in test modules on import
to provide assert expression information.
--no-assert
DEPRECATED equivalent to --assert=plain
--no-magic
DEPRECATED equivalent to --assert=plain
--genscript=path
create standalone pytest script at given target path.
[pytest] ini-options in the next pytest.ini|tox.ini|setup.cfg file:
markers (linelist)
markers for test functions
norecursedirs (args)
directory patterns to avoid for recursion
usefixtures (args)
list of default fixtures to be used with this project
python_files (args)
glob-style file patterns for Python test module discovery
python_classes (args)
prefixes for Python test class discovery
python_functions (args)
prefixes for Python test function and method discovery
addopts (args)
extra command line options
minversion (string)
minimally required pytest version
to see available markers type: py.test --markers
to see available fixtures type: py.test --fixtures
(shown according to specified file_or_dir or current dir if not specified)
ini文件查找顺序
pytest.ini
当文件中有[pytest]时就会停止查找,不会合并配置文件。比如执行py.test path/to/testdir,查找顺序如下:
path/to/testdir/pytest.ini
path/to/testdir/tox.ini
path/to/testdir/setup.cfg
path/to/pytest.ini
path/to/tox.ini
path/to/setup.cfg
... # up until root of filesystem
如果没有指定目录,默认用当前目录。
修改配置文件
# content of pytest.ini
# (or tox.ini or setup.cfg)
addopts = -rsxX -q
内置配置文件选项
minversion:Specifies a minimal pytest version required for running tests。minversion = 2.1 # will fail if we run with pytest-2.0
addopts: 添加参数,比如:
addopts = --maxfail=2 -rf # exit after 2 failures, report fail info
执行py.test test_hello.py等同于py.test test_hello.py。
norecursedirs
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致}

我要回帖

更多关于 setup.exe可以删除吗 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信