我试图从页面中获取div id列表.当我打印出属性时,我会列出ID.
for tag in soup.find_all(class_="bookmark blurb group") :
print(tag.attrs)
Run Code Online (Sandbox Code Playgroud)
结果是:
{'id': 'bookmark_8199633', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}
{'id': 'bookmark_7744613', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}
{'id': 'bookmark_7338591', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}
{'id': 'bookmark_7338535', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}
{'id': 'bookmark_4530078', 'role': 'article', 'class': ['bookmark', 'blurb', 'group']}
Run Code Online (Sandbox Code Playgroud)
所以我知道有些ID.但是,当我打印出tag.id时,我只得到一个"无"列表.我在这做错了什么?
我是使用Pytest测试Python的新手,我遇到了一个小而烦人的挂机.在命令行测试会话结果中,我看到我的测试通过,但显示的百分比不是100%.通过一些激进的日志记录,我能够确认我的测试正在按预期传递 - 那么显示的百分比是什么信息?
示例测试会话输出:
platform win32 -- Python 3.7.0a4, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
rootdir: C:\api-check, inifile:
collected 5 items
test_Me.py ... [ 60%]
test_env.py .. [100%]
========================== 5 passed in 6.04 seconds ===========================
Run Code Online (Sandbox Code Playgroud) 长话短说,如果会话是针对我们的生产 API 运行的,我希望能够跳过一些测试。运行测试的环境是使用命令行选项设置的。
我想到了使用pytest_namespace来跟踪全局变量的想法,所以我在我的 conftest.py 文件中进行了设置。
def pytest_namespace():
return {'global_env': ''}
Run Code Online (Sandbox Code Playgroud)
我接受命令行选项并在 conftest.py 的夹具中设置各种 API url(来自 config.ini 文件)。
@pytest.fixture(scope='session', autouse=True)
def configInfo(pytestconfig):
global data
environment = pytestconfig.getoption('--ENV')
print(environment)
environment = str.lower(environment)
pytest.global_env = environment
config = configparser.ConfigParser()
config.read('config.ini') # local config file
configData = config['QA-CONFIG']
if environment == 'qa':
configData = config['QA-CONFIG']
if environment == 'prod':
configData = config['PROD-CONFIG']
(...)
Run Code Online (Sandbox Code Playgroud)
然后我有了我想跳过的测试,它是这样装饰的:
@pytest.mark.skipif(pytest.global_env in 'prod',
reason="feature not in Prod yet")
Run Code Online (Sandbox Code Playgroud)
但是,每当我针对 prod 运行测试时,它们都不会被跳过。我做了一些摆弄,发现:
a) global_env 变量可以通过另一个夹具访问
@pytest.fixture(scope="session", autouse=True)
def …Run Code Online (Sandbox Code Playgroud) 如果 api 调用返回 403 代码,我会尝试使测试失败并显示特定错误消息。我尝试了几个选项:
if int(addresses.status_code) is 403:
fail("Auth Error: Missing Role {}".format(response.json()))
assert addresses.status_code is not 403
assert addresses.status_code is not codes.forbidden
assert addresses.status_code is codes.ok
Run Code Online (Sandbox Code Playgroud)
其中唯一失败的是最后一个assert addresses.status_code is codes.ok。然而,api 调用响应的状态代码是403。我已经尝试过确保类型相同等,但不确定还能去哪里。
如何测试 status_code 是否不是特定值?