我正在使用Python断言语句来匹配实际和预期的行为。我对这些没有控制权,好像有一个错误测试用例中止了一样。我想控制断言错误,并要定义是否要在失败断言时中止测试用例。
我还想添加一些类似的内容,如果存在断言错误,则应该暂停测试用例,并且用户可以随时恢复。
我不知道该怎么做
代码示例,我们在这里使用pytest
import pytest
def test_abc():
a = 10
assert a == 10, "some error message"
Run Code Online (Sandbox Code Playgroud)
Below is my expectation
当assert抛出assertionError时,我应该可以选择暂停测试用例,并且可以调试并稍后恢复。对于暂停和恢复,我将使用tkinter模块。我将做一个断言功能如下
import tkinter
import tkinter.messagebox
top = tkinter.Tk()
def _assertCustom(assert_statement, pause_on_fail = 0):
#assert_statement will be something like: assert a == 10, "Some error"
#pause_on_fail will be derived from global file where I can change it on runtime
if pause_on_fail == 1:
try:
eval(assert_statement)
except AssertionError as e:
tkinter.messagebox.showinfo(e)
eval (assert_statement)
#Above is to raise the …Run Code Online (Sandbox Code Playgroud) 我对local/ourperl的范围几乎没有疑问.我阅读了很多文档,但仍然存在混淆.以下是混乱
什么是local范围?
我读的是 - >本地复制全局变量的值,更改值,用户将使用它和块外它将保留全局值
混乱 - > my做同样的事情.我看到的唯一好处是,某些变量$package::var不能用我的作用域声明,但可以用本地作用域声明.还有什么适合当地的
什么是"全球"变量?
读的是 - >它的范围在包内.基本上我们将全局变量放在@EXPORT数组中并使用它或附加命名空间以在其他包中使用.
怀疑 - >再次,如果我们my在main中声明带范围的变量,那么我们可以在整个包中访问变量.是对的吗?是否可以my在@EXPORT数组中添加范围变量并在另一个包中使用它?
我认为全局变量是用our关键字声明的.有没有其他方法可以这样做?
这个问题可能看似重复,但我很困惑
我试图通过我已经定义的记录器获得pexepct stdout日志.下面是代码
import logging
import pexpect
import re
import time
# this will be the method called by the pexpect object to log
def _write(*args, **kwargs):
content = args[0]
# let's ignore other params, pexpect only use one arg AFAIK
if content in [' ', '', '\n', '\r', '\r\n']:
return # don't log empty lines
for eol in ['\r\n', '\r', '\n']:
# remove ending EOL, the logger will add it anyway
content = re.sub('\%s$' % eol, '', content)
return logger.info(content) …Run Code Online (Sandbox Code Playgroud) 我正在使用 Pexpect 模块连接到远程服务器。我可以成功发送和检索响应。我试图通过期待一些垃圾并假设它会清除缓冲区来清除缓冲区,但实际上它并没有清除缓冲区。
下面是我的示例代码
import pexpect
obj = pexpect.spawn("telnet 172.16.250.250", maxread=8192)
obj.sendline("")
result = obj.expect(expected, timeout=3) --> getting output here `OUTPUT 1`
obj.sendline("1")
time.sleep(3)
try:
obj.expect("Asdfgdsad", timeout=2) --> I am expecting to clear buffer here but it did not
except pexpect.TIMEOUT:
pass
print("buffer is", obj.buffer) . --> This is printing output `OUTPUT 1` as I have meniotned
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么??我正在使用 python3.7 。如果我没记错的话它在 python2.X 中工作正常
我甚至从perldoc和读到了解释StackOverflow.但有一点混乱.
use通常在编译时加载模块,而require在运行时加载模块use只调用内置的导入功能,而require需要单独调用导入模块
BEGIN {
require ModuleName;
ModuleName->import;
}
Run Code Online (Sandbox Code Playgroud)require 如果我们想偶尔加载更大的模块,则使用
use在早期状态抛出异常,而require在遇到问题时抛出异常随着use我们可以有选择地加载的程序不是所有的,但很少像
use Module qw(foo bar) # it will load foo and bar only
Run Code Online (Sandbox Code Playgroud)它也可能require吗?
Beisdes是有之间的另一个差异use和require?
很多关于谷歌的讨论,但我只理解上述这些要点.
请帮我其他点.
我正在使用TCL编写所有库.我想在Python中创建一个GUI,它只有很少的按钮和其他选项.在开始TCL shell将打开.当我单击按钮时,将在TCL shell上执行相应的命令.
是否可以在不关闭TCL shell的情况下在TCL的同一shell上触发命令.
我搜索谷歌并Tkniter在Python中找到模块,但每次我需要执行命令时它都会打开TCL shell.
0710我在代码中运行 mypy 版本时遇到错误。我制作了一个存在问题的小代码片段,但不确定为什么会出现此错误
a = None
version = 2
if version == 2:
a = 10
#print("asdfgh")
if float(a) == 10:
print("erty")
Run Code Online (Sandbox Code Playgroud)
当我在 mypy 中运行代码时出现错误
测试/test.py:8:错误:“float”的参数 1 具有不兼容的类型“Optional[int]”;预期“Union[SupportsFloat、str、bytes、bytearray]”
任何帮助将不胜感激
我在 conftest.py 中有一个固定装置
@pytest.fixture(scope="function", autouse=True)
@pytest.mark.usefixtures
def pause_on_assert():
yield
if hasattr(sys, 'last_value') and isinstance(sys.last_value, AssertionError):
tkinter.messagebox.showinfo(sys.last_value)
Run Code Online (Sandbox Code Playgroud)
同样,conftest.py 中还有许多其他修复程序,其范围为session,module
我的测试用例看起来像这样
test.py
@pytest.fixture(scope="function", autouse=True)
def _wrapper:
print("pre condition")
yield
print("post condition")
def test_abc():
assert 1==0
Run Code Online (Sandbox Code Playgroud)
问题是我希望 conftest.py 中yield的夹具在我的测试用例中的夹具之前运行
我如何改变夹具执行方式的顺序
我刚开始做 shell 脚本并regex在 if 语句中使用时遇到未知的操作数错误。我搜索了谷歌但没有得到任何东西
IP="172.21.1.1"
if [[ "$IP" =~ /d ]] ; then
echo "qqq"
fi
Run Code Online (Sandbox Code Playgroud)
获取错误为
sh: =~: unknown operand
Run Code Online (Sandbox Code Playgroud)
Bash 版本是:BusyBox v1.19.3 (2012-01-31 08:57:52 PST) 内置外壳(ash)
我对绝对导入定义感到困惑。它说:
\n\nAbsolute import involves full path i.e., from the project\xe2\x80\x99s root folder to the desired module
假设我有一个结构:
\n\nApp\n project1\n file1.py\n file2.py\n __init__.py\n project2\n file3.py\n file4.py\n __init__.py\nRun Code Online (Sandbox Code Playgroud)\n\n如果file3.py我必须从文件中导入一些函数file1.py,那么我会写为
from app.project1.file1 import someFucntion\nRun Code Online (Sandbox Code Playgroud)\n\n问题:python如何发现app是我的根目录。我的文件结构可能不同。我的文件夹上面可能有多个文件夹App。
就像/Users/Nitesh/App/Project1--> 这可能是我的目录结构
Python 是否从没有 的点考虑根目录__init__.py?
python ×7
perl ×2
pytest ×2
assert ×1
fixtures ×1
mypy ×1
perl-module ×1
pexpect ×1
python-2.7 ×1
python-3.x ×1
scope ×1
shell ×1
tcl ×1
testing ×1