1

Cloudwatch 日志是 AWS 上具有日志组 > 日志流 > 日志事件的对象。我正在尝试为此编写测试,但是 moto 模拟在应用于 boto3.client('logs') 时会引发客户端错误。我正在寻找其他方法来模拟日志的行为。你会如何为这个函数编写一个测试?

例如:

    client = boto3.client('logs')
    def get_recent_log_stream_name(logGroupName):
        response = client.describe_log_streams(
            logGroupName=logGroupName,
            orderBy='LastEventTime',
            descending=True,
            limit=1)
         logStreamName = response['logStreams'][0]['logStreamName']
         return logStreamName
4

1 回答 1

1

我会这样写测试moto

import boto3
from moto import mock_logs


def get_recent_log_stream_name(logs, logGroupName):
    """Function under test"""
    response = logs.describe_log_streams(
        logGroupName=logGroupName,
        orderBy='LastEventTime',
        descending=True,
        limit=1)
    log_stream_name = response['logStreams'][0]['logStreamName']
    return log_stream_name

@mock_logs
def test_get_recent_log_stream_name():
    """Test function"""
    log_group_name = 'test-group'
    log_stream_name = 'test-stream'
    logs = boto3.client('logs')
    logs.create_log_group(logGroupName=log_group_name)
    logs.create_log_stream(
        logGroupName=log_group_name,
        logStreamName=log_stream_name,
    )
    assert get_recent_log_stream_name(logs, log_group_name) == log_stream_name

test_get_recent_log_stream_name()
于 2019-04-16T21:44:40.970 回答