在独立pytest测试用例中,我想使用断言unittest.TestCase.assertNotIn(和unittest其他断言),而不依赖任何uniittest模块。使用python-assert之类的库是最佳实践吗?
我需要使用来声明一条错误消息py.test,
import pandas as pd
import numpy as np
from inv_exception_store import InvAmtValError
MAX_INV_VAL = 10000000.0
MIN_INV_VAL = 0.0
class Invoices:
def __init__(self, data=None):
if data is None:
self.__invoices = pd.Series([], dtype=np.float32)
else:
self.__invoices = pd.Series(pd.Series(data).astype(np.float32))
def addInvoice(self, amount):
try:
if self.__invoices.size > MAX_INV_SIZE:
raise InvNumError
elif amount > MAX_INV_VAL or amount < MIN_INV_VAL:
raise InvAmtValError(amount)
else:
self.__invoices = self.__invoices.append(pd.Series(amount).astype(np.float32), ignore_index=True)
except (InvNumError, InvAmtValError) as e:
print(str(e))
class InvAmtValError(Exception):
def __init__(self, amount, message=None):
if message is None:
if …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Jenkins 中运行 Python 映像以使用 pytest 执行一系列单元测试,但是我在使用 Docker 时遇到了一些奇怪的行为。
我的 Jenkinsfile 管道是
agent {
docker { image 'python:3.6-jessie' }
}
stages {
stage('Run tests') {
steps {
withCredentials([
string(credentialsId: 'a-secret', variable: 'A_SECRET')
{
sh label: "Install dependencies", script: 'pip install -r requirements.txt'
sh label: 'Execute tests', script: "pytest mytests.py"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行管道时,Docker 似乎正在执行一条很长的指令(-e环境变量比我定义的凭据多得多?),然后是cat.
然后构建只是挂起并且永远不会完成:
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 996:994
-w /var/lib/jenkins/workspace/myproject
-v …Run Code Online (Sandbox Code Playgroud) 我目前正在参数化我使用的所有测试用例pytest_generate_tests,这很有效。
我现在想做的是为特定测试覆盖此行为。如果我尝试pytest.mark.parametrize在测试本身上使用装饰器,我会收到一个ValueError: duplicate错误,这是可以理解的,因为我现在试图在两个地方对测试进行参数化。
有没有办法可以覆盖这个测试用例的“默认”参数化?
我可以通过执行以下操作来实现这一点,但这是一种非常hacky的方法:
def pytest_generate_tests(metafunc):
fixture_modes = ['mode1', 'mode2']
if 'fixture' in metafunc.fixturenames:
fixture = metafunc.config.getoption('fixture')
if fixture:
fixture_modes = [fixture]
if metafunc.function.__name__ != 'func_to_skip':
metafunc.parametrize('fixture_mode', fixture_modes, indirect=True)
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
我创建了一个pytest.ini文件,
addopts = --resultlog=log.txt
Run Code Online (Sandbox Code Playgroud)
这会创建一个日志文件,但我想在每次运行测试时创建一个新的日志文件。
我是 pytest 的新手,如果我在阅读文档时遗漏了任何内容,请原谅我。
谢谢
我的tensorflow 2.0.0beta1正常运行,但是我无法使用命令pip install tensorflow-text安装tensorflow-text(如tensorflow页面上所述)。我可以使用pip search tensorflow-text找到它,但出现错误
ERROR: Could not find a version that satisfies the requirement tensorflow-text (from versions: none)
Run Code Online (Sandbox Code Playgroud)
该软件包没有要求(即特定的python版本)。我在Windows上运行,使用conda,python 3.6.9
我已经完成了这个小包,我想在我的社区中分发。它现在在 test.pypi 上,当我想尝试安装它时,它给出了找不到依赖项的错误。
设置文件
...
install_requires=[
'defcon>=0.6.0',
'fonttools>=3.31.0'
]
...
Run Code Online (Sandbox Code Playgroud)
抛出这个错误
ERROR: Could not find a version that satisfies the requirement defcon>=0.6.0 (from sameWidther==0.6) (from versions: none)
ERROR: No matching distribution found for defcon>=0.6.0 (from sameWidther==0.6)
Run Code Online (Sandbox Code Playgroud)
但是当我手动安装时,它可以工作
pip install 'fonttools>=3.6.0'
pip install 'defcon>=0.6.0'
Run Code Online (Sandbox Code Playgroud) 我有一个代码,我需要检查输入的密码是否为空。如果是,则打印消息并退出脚本。
我正在使用pytest验证此代码并使用固定装置捕获输出capsys。但capsys.readouterr()没有捕获输出。
要测试的代码
def get_password():
password = getpass.getpass('Password required :', stream=None)
if not password:
print("NoPasswordError: Password not provided.Exiting from run")
'''return -1'''
sys.exit(-1)
else:
return password
Run Code Online (Sandbox Code Playgroud)
py测试代码
def test_input_validation_nopass(self,getpass,capsys):
getpass.return_value = ''
get_password()
out, err = capsys.readouterr()
sys.stdout.write(out)
assert re.match('NoPasswordError',out,re.I)
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除sys.exit并放置return,则会捕获输出。目前我收到以下错误:
pytest -q UnitTest.py -k test_input_validation_nopass -rx -rP
F [100%]
=============================================================================== FAILURES ================================================================================
________________________________________________________________ TestClass.test_input_validation_nopass _________________________________________________________________
self = <UnitTest_buildUpgrade.TestClass object at 0x10d179978>, getpass = <MagicMock name='getpass' id='4514617832'>
capsys = <_pytest.capture.CaptureFixture …Run Code Online (Sandbox Code Playgroud) 是否有一个选项可以在 cli 输出中列出取消选择的测试以及触发取消选择的标记?
我知道,在有许多测试的套件中,这作为默认值并不好,但在 api 测试之类的测试可能会受到更多限制的情况下,这将是一个有用的选项。
数字摘要
collected 21 items / 16 deselected / 5 selected
Run Code Online (Sandbox Code Playgroud)
在尝试组织标记并查看 ci 构建中发生的情况时很有帮助,但还不够。
mymodule.c 启动如下:
#define PY_SSIZE_T_CLEAN
#define Py_LIMITED_API 0x03070000
#include "Python.h"
Run Code Online (Sandbox Code Playgroud)
它构建时没有错误和警告。然而,生成的文件被命名为
mymodulename.cpython-37m-x86_64-linux-gnu.so
Run Code Online (Sandbox Code Playgroud)
构建命令:
$ python setup.py build
Run Code Online (Sandbox Code Playgroud)
当我改为发出
$ pip wheel .
Run Code Online (Sandbox Code Playgroud)
轮子中包含的扩展名称相同,因此轮子也没有 ABI3 标签。
我期望文件名是 abi3 或类似的文件名,如 PEP 425 中所述。
到目前为止我的研究令人沮丧。我查看了 PEP 384 和 425、docs.python.org 上的文档和相关教程,特别是它的 C/API 和 distutils、PYPA 的文档、setuptools、wheel 和 pip 上的文档 - 无济于事。
当前 Python 源发行版中包含的文件 /modules/xxmodule.c 仅具有历史意义。
我在这里缺少什么?
python ×9
pytest ×6
python-3.x ×3
testing ×3
setuptools ×2
c ×1
distutils ×1
distutils2 ×1
docker ×1
exception ×1
jenkins ×1
logging ×1
pandas ×1
pip ×1
pypi ×1
stdout ×1
tensorflow ×1