我正在尝试创建一组单元测试来测试 Bigquery 的 Google 客户端库。我正在努力制作一个 Unittest 文件,该文件将模拟客户端并让我测试我的输入。我提供了一个简单的脚本,它具有一些集合功能来返回属于数据集的表列表。
有人会向我展示一个模拟 Google 客户端库的示例示例作为我找到的文档@ https://github.com/googleapis/google-cloud-python/blob/master/bigquery/tests/unit/test_client.py是不直接与代码的方法交互,所以我无法将它应用到我的代码中。
感谢实现这一目标的任何想法或方法,我似乎无法在 Stack Overflow 上找到任何记录此问题的地方。
谢谢
from google.cloud import bigquery
def get_dataset():
client = bigquery.Client.from_service_account_json('some_client_secret.json')
dataset_id = 'some_project.some_dataset'
dataset = client.get_dataset(dataset_id)
full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id)
friendly_name = dataset.friendly_name
print(
"Got dataset '{}' with friendly_name '{}'.".format(
full_dataset_id, friendly_name
)
)
# View dataset properties
print("Description: {}".format(dataset.description))
print("Labels:")
labels = dataset.labels
if labels:
for label, value in labels.items():
print("\t{}: {}".format(label, value))
else:
print("\tDataset has no labels defined.")
# View tables in …Run Code Online (Sandbox Code Playgroud) google-app-engine unit-testing mocking python-3.x google-client
我目前正在尝试学习如何使用 Python 进行单元测试,并了解了 Mocking 的概念,我是一名初学者 Python 开发人员,希望在发展 Python 技能的同时学习 TDD 的概念。我正在努力学习使用Python unittest.mock 文档的用户给定输入来模拟类的概念。如果我能得到一个如何模拟某个函数的例子,我将非常感激。我将使用此处找到的示例:示例问题
class AgeCalculator(self):
def calculate_age(self):
age = input("What is your age?")
age = int(age)
print("Your age is:", age)
return age
def calculate_year(self, age)
current_year = time.strftime("%Y")
current_year = int(current_year)
calculated_date = (current_year - age) + 100
print("You will be 100 in", calculated_date)
return calculated_date
Run Code Online (Sandbox Code Playgroud)
请有人使用 Mocking 创建一个示例单元测试来自动输入年龄,以便它返回模拟年龄为 100 的年份。
谢谢。