小编Pet*_*Pet的帖子

使用 pytest 在 python 中循环测试

我有一个问题,希望任何人都可以帮助我。我正在使用 pytest 并设置一些测试功能。这是一个例子:

def square(number):
    return(number ** 2)

Run Code Online (Sandbox Code Playgroud)

使用 pytest 我可以设置不同的测试功能,例如

def test_square_2():
    assert(square(2) == 4)
def test_square_3():
    assert(square(3) == 9)
Run Code Online (Sandbox Code Playgroud)

现在我的问题:有没有办法设置一个列表列表,例如

test_list = [[1,1],[2,4],[3,9],[4,16],[5,25]]
Run Code Online (Sandbox Code Playgroud)

并设置一个循环来测试列表中的所有元组?

最佳F

python pytest

7
推荐指数
2
解决办法
1万
查看次数

如何从 python 函数登录 Google Cloud Storage?

我是谷歌云存储的新手,我尝试设置一个每天下载一次 blob 的功能。目前我正在我的 Jupyter Notebook 中工作,但最终,代码将在 Azure 函数中运行。我正在努力设置将我连接到存储桶的客户端。我有一个服务帐户凭据 JSON,它使我能够连接到 google。

在本地我找到了一个解决方案:

from google.cloud import storage

client = storage.Client.from_service_account_json('<PATH_TO_SERVICE_ACCOUNT_JSON>')
Run Code Online (Sandbox Code Playgroud)

问题是我没有将 JSON 存储在云中的路径,但我将其存储在密钥保管库中。我想出了以下解决方案:

from google.cloud import storage
import json
from google.oauth2 import service_account

string_key = get_key_from_key_vault()
service_account_info = json.loads(string_key)
google_credentials = service_account.Credentials.from_service_account_info(
    service_account_info
)
scoped_credentials = google_credentials.with_scopes(
    ['https://www.googleapis.com/auth/cloud-platform.read-only'])
print(type(scoped_credentials))
client = storage.Client(credentials = scoped_credentials)
Run Code Online (Sandbox Code Playgroud)

我不完全确定我是否需要该scoped_credentials = ...部件,但我只有对存储桶的读取权限。(如果我跳过该部分,错误保持不变)

当我选择此解决方案时,出现以下错误:

DefaultCredentialsError: Could not automatically determine credentials. Please set 
GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For
 more information, please see …
Run Code Online (Sandbox Code Playgroud)

python google-authentication google-cloud-storage google-cloud-platform google-auth-library

5
推荐指数
1
解决办法
644
查看次数