Siemens.AspNet.Lambda.MsTest.Sdk 0.1.0-alpha.281

Prefix Reserved
This is a prerelease version of Siemens.AspNet.Lambda.MsTest.Sdk.
dotnet add package Siemens.AspNet.Lambda.MsTest.Sdk --version 0.1.0-alpha.281
                    
NuGet\Install-Package Siemens.AspNet.Lambda.MsTest.Sdk -Version 0.1.0-alpha.281
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Siemens.AspNet.Lambda.MsTest.Sdk" Version="0.1.0-alpha.281" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Siemens.AspNet.Lambda.MsTest.Sdk" Version="0.1.0-alpha.281" />
                    
Directory.Packages.props
<PackageReference Include="Siemens.AspNet.Lambda.MsTest.Sdk" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Siemens.AspNet.Lambda.MsTest.Sdk --version 0.1.0-alpha.281
                    
#r "nuget: Siemens.AspNet.Lambda.MsTest.Sdk, 0.1.0-alpha.281"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Siemens.AspNet.Lambda.MsTest.Sdk@0.1.0-alpha.281
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Siemens.AspNet.Lambda.MsTest.Sdk&version=0.1.0-alpha.281&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Siemens.AspNet.Lambda.MsTest.Sdk&version=0.1.0-alpha.281&prerelease
                    
Install as a Cake Tool

Siemens.AspNet.Lambda.MsTest.Sdk

The Siemens.AspNet.Lambda.MsTest.Sdk provides testing utilities specifically designed for AWS Lambda functions using MS Test framework, focusing on mocking and logging capabilities.


📖 Overview

This SDK eases the testing of business logic within AWS Lambda functions by offering mock helpers and logging infrastructure.

✅ Key Features

  • 🚀 Enable lambda context mocking for unit tests.
  • 🔍 Structured logging and log history collection for test review.
  • 🛠️ Customizable test initialization with dependency injection.

📦 Installation

To integrate this SDK into your test project, ensure you have the necessary dependencies such as Moq and MS Test SDK. You can manage these dependencies via your .csproj file or .NET CLI.


🧠 Key Components

Component Description
LambdaContextMockHelper Utility for creating mocked ILambdaContext instances.
LambdaTestBase<TFunction> Base class to streamline Lambda function testing.
LogEntry Record to capture structured log information.

⚡ Quickstart Examples

Testing a Lambda Function with Logging

The LambdaTestBase<TFunction> class simplifies the setup of mock loggers and context for Lambda function tests:

[TestClass]
[TestCategory("Lambdas")]
[TestCategory("Lambdas Init Cognito User - Function - Ok")]
public class Function_Ok_Test : LambdaTestBase<Function> 
{
    [DataTestMethod]
    // More infos about DynamicResource locator you find here: https://www.nuget.org/packages/AspNetCore.Simple.MsTest.Sdk
    [DynamicRequestLocator]
    public async Task Lambda_Should_Execute_Successfully(string useCase)
    {
        // 1. Get payload as JSON string from embedded file
        var json = EmbeddedFile.GetFileContentFrom($"SiemensGPT.InitCognitoUser.Tests.Ok.Requests.{useCase}");
        
        // 2. Setup cognito confirmation event
        var cognitoPostConfirmationEvent = json.FromJsonStringAs<CognitoPostConfirmationEvent>();

        // 3. Execute lambda function - test passes if no exception is thrown
        var response = await Function.InvokeAsync(cognitoPostConfirmationEvent, LambdaContext).ConfigureAwait(false);

        // 4. Evaluate function response
        Assert.That.ObjectsAreEqual($"SiemensGPT.InitCognitoUser.Tests.Ok.Responses.{useCase}", response);
        
        // 5. Evaluate logging
        Assert.That.ObjectsAreEqual($"SiemensGPT.InitCognitoUser.Tests.Ok.Responses.LogHistory.json", LogHistory);
    }
}

Mocking Lambda Context

Use LambdaContextMockHelper to create a mocked Lambda context, providing realistic values for AWS Lambda environment variables. Is already setup with the LambdaTestBase

public class LambdaTestBase<TFunction> where TFunction : class, new()
{
    protected TFunction Function { get; private set; } = null!;

    protected Mock<ILogger> LoggerMock { get; private set; } = null!;

    protected ILogger Logger { get; private set; } = null!;

    protected ILambdaContext LambdaContext { get; private set; } = null!;

    protected List<LogEntry> LogHistory { get; private set; } = new();

    protected LambdaSettings LambdaSettings { get; private set; } = new();
}

To customize your ILambdaContext, you can use our LambdaMockHelper

var mockContext = LambdaContextMockHelper.CreateMockContext<ILambdaContext>();

Capturing and Reviewing Logs

LogHistory in LambdaTestBase allows inspection of all log entries recorded during the test:

var errors = LogHistory.Where(log => log.Level == LogLevel.Error);
Assert.IsTrue(errors.Any(), "Expected at least one error log entry.");

📌 Error Handling and Logging

Structured logging within tests helps identify issues by logging exception details, error messages, and execution context. The logger is already set up and the logger mock provides access to the log data for detailed examination.

Customize logging as needed:

// The property LogHistory provides the collected log information from the lambda execution.
// in this sample we are asserting the whole LogHistory
Assert.That.ObjectsAreEqual($"SiemensGPT.InitCognitoUser.Tests.Ok.Responses.LogHistory.json", LogHistory);

Sample for LogHistory.json

[
  {
    "level": "Information",
    "id": {
      "id": 0,
      "name": null
    },
    "entry": "FunctionTimeout - Started",
    "errorLogInfo": null
  },
  {
    "level": "Information",
    "id": {
      "id": 0,
      "name": null
    },
    "entry": "FunctionTimeout - Step 1",
    "errorLogInfo": null
  },
  {
    "level": "Error",
    "id": {
      "id": 0,
      "name": null
    },
    "entry": "[TaskCanceledException] TaskCanceledException was thrown. Details: {\u0022errorType\u0022:\u0022TaskCanceledException\u0022,\u0022title\u0022:\u0022TaskCanceledException was thrown.\u0022,\u0022message\u0022:\u0022A task was canceled.\u0022,\u0022statusCode\u0022:500,\u0022requestInfos\u0022:{\u0022remainingTime\u0022:\u002200:00:10.1200000\u0022,\u0022functionVersion\u0022:\u0022$LATEST\u0022,\u0022functionName\u0022:\u0022FunctionTimeout\u0022,\u0022awsRequestId\u0022:\u0022test-request-id-123\u0022,\u0022memoryLimitInMB\u0022:256,\u0022logStreamName\u0022:\u00222025.05.27/[$LATEST]123456789\u0022,\u0022logGroupName\u0022:\u0022/aws/lambda/FunctionTimeout\u0022},\u0022errorDetails\u0022:{},\u0022extensions\u0022:{},\u0022stackTrace\u0022:[\u0022   at SiemensGPT.InitCognitoUser.FunctionTimeout.HandleAsync(CognitoPostConfirmationEvent request, ILambdaContext context, CancellationToken cancellationToken) in /Users/z0052jaz/RiderProjects/siemens-aspnet-sdk/src/SiemensGPT.InitCognitoUser/Functions/Timeout/FunctionTimeout.cs:line 20\u0022,\u0022   at Siemens.AspNet.Lambda.Sdk.ErrorLogging.ErrorLogRequestResponseMiddleware\\u00602.InvokeAsync(TRequest request, ILambdaContext context, LambdaRequestDelegate\\u00602 requestDelegate, CancellationToken cancellationToken) in /Users/z0052jaz/RiderProjects/siemens-aspnet-sdk/src/Siemens.AspNet.Lambda.Sdk/ErrorLogging/ErrorLogMiddleware{TRequest, TResponse}.cs:line 26\u0022]}",
    "errorLogInfo": {
      "errorType": "TaskCanceledException",
      "title": "TaskCanceledException was thrown.",
      "message": "A task was canceled.",
      "statusCode": 500,
      "requestInfos": {
        "logGroupName": "/aws/lambda/FunctionTimeout",
        "memoryLimitInMB": 256,
        "functionName": "FunctionTimeout",
        "logStreamName": "2025.05.27/[$LATEST]123456789",
        "remainingTime": "00:00:10.1200000",
        "functionVersion": "$LATEST",
        "awsRequestId": "test-request-id-123"
      },
      "errorDetails": {},
      "extensions": {},
      "stackTrace": [
        "   at SiemensGPT.InitCognitoUser.FunctionTimeout.HandleAsync(CognitoPostConfirmationEvent request, ILambdaContext context, CancellationToken cancellationToken) in /Users/z0052jaz/RiderProjects/siemens-aspnet-sdk/src/SiemensGPT.InitCognitoUser/Functions/Timeout/FunctionTimeout.cs:line 20",
        "   at Siemens.AspNet.Lambda.Sdk.ErrorLogging.ErrorLogRequestResponseMiddleware\u00602.InvokeAsync(TRequest request, ILambdaContext context, LambdaRequestDelegate\u00602 requestDelegate, CancellationToken cancellationToken) in /Users/z0052jaz/RiderProjects/siemens-aspnet-sdk/src/Siemens.AspNet.Lambda.Sdk/ErrorLogging/ErrorLogMiddleware{TRequest, TResponse}.cs:line 26"
      ]
    }
  }
]


📚 Documentation

Detailed documentation and further examples can be found within the codebase and will soon be available online.


📢 Contributing

Contributions and feedback are welcome! Please create issues or pull requests to suggest improvements.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0-alpha.281 42 9/16/2025
0.1.0-alpha.280 34 9/16/2025
0.1.0-alpha.279 33 9/16/2025
0.1.0-alpha.278 33 9/16/2025
0.1.0-alpha.275 135 9/3/2025
0.1.0-alpha.274 313 9/2/2025
0.1.0-alpha.273 195 9/1/2025
0.1.0-alpha.272 113 9/1/2025
0.1.0-alpha.271 163 8/29/2025
0.1.0-alpha.270 152 8/29/2025
0.1.0-alpha.269 148 8/29/2025
0.1.0-alpha.268 157 8/29/2025
0.1.0-alpha.267 162 8/27/2025
0.1.0-alpha.266 163 8/27/2025
0.1.0-alpha.264 242 8/22/2025
0.1.0-alpha.263 67 8/22/2025
0.1.0-alpha.262 71 8/22/2025
0.1.0-alpha.261 84 8/22/2025
0.1.0-alpha.260 92 8/22/2025
0.1.0-alpha.259 95 8/22/2025
0.1.0-alpha.258 184 8/19/2025
0.1.0-alpha.257 190 8/18/2025
0.1.0-alpha.246 140 8/14/2025
0.1.0-alpha.245 117 8/14/2025
0.1.0-alpha.244 136 8/14/2025
0.1.0-alpha.243 116 8/14/2025
0.1.0-alpha.238 122 8/12/2025
0.1.0-alpha.237 383 8/6/2025
0.1.0-alpha.236 194 8/5/2025
0.1.0-alpha.235 197 8/5/2025
0.1.0-alpha.234 229 8/5/2025
0.1.0-alpha.233 162 8/4/2025
0.1.0-alpha.232 175 8/4/2025
0.1.0-alpha.231 72 8/1/2025
0.1.0-alpha.230 74 8/1/2025
0.1.0-alpha.229 93 7/31/2025
0.1.0-alpha.228 92 7/31/2025
0.1.0-alpha.227 93 7/31/2025
0.1.0-alpha.225 93 7/31/2025
0.1.0-alpha.224 94 7/30/2025
0.1.0-alpha.222 252 7/16/2025
0.1.0-alpha.219 161 7/14/2025
0.1.0-alpha.217 83 7/11/2025
0.1.0-alpha.212 157 7/8/2025
0.1.0-alpha.211 126 7/3/2025
0.1.0-alpha.207 117 7/3/2025
0.1.0-alpha.206 236 6/30/2025
0.1.0-alpha.205 100 6/27/2025
0.1.0-alpha.202 98 6/27/2025
0.1.0-alpha.200 98 6/27/2025
0.1.0-alpha.198 96 6/27/2025
0.1.0-alpha.196 103 6/27/2025
0.1.0-alpha.195 99 6/27/2025
0.1.0-alpha.194 95 6/27/2025
0.1.0-alpha.193 97 6/27/2025
0.1.0-alpha.192 97 6/27/2025
0.1.0-alpha.191 97 6/27/2025
0.1.0-alpha.189 118 6/26/2025
0.1.0-alpha.188 116 6/26/2025
0.1.0-alpha.187 113 6/26/2025
0.1.0-alpha.186 120 6/26/2025
0.1.0-alpha.185 121 6/26/2025
0.1.0-alpha.184 115 6/26/2025
0.1.0-alpha.183 113 6/26/2025
0.1.0-alpha.182 115 6/26/2025
0.1.0-alpha.181 119 6/25/2025
0.1.0-alpha.180 120 6/24/2025
0.1.0-alpha.179 121 6/23/2025
0.1.0-alpha.178 125 6/23/2025
0.1.0-alpha.176 122 6/23/2025
0.1.0-alpha.174 123 6/19/2025
0.1.0-alpha.173 123 6/19/2025
0.1.0-alpha.172 124 6/17/2025
0.1.0-alpha.171 121 6/16/2025
0.1.0-alpha.169 123 6/16/2025
0.1.0-alpha.165 231 6/13/2025
0.1.0-alpha.164 236 6/13/2025
0.1.0-alpha.163 235 6/13/2025