Siemens.AspNet.Lambda.MsTest.Sdk
0.1.0-alpha.222
Prefix Reserved
dotnet add package Siemens.AspNet.Lambda.MsTest.Sdk --version 0.1.0-alpha.222
NuGet\Install-Package Siemens.AspNet.Lambda.MsTest.Sdk -Version 0.1.0-alpha.222
<PackageReference Include="Siemens.AspNet.Lambda.MsTest.Sdk" Version="0.1.0-alpha.222" />
<PackageVersion Include="Siemens.AspNet.Lambda.MsTest.Sdk" Version="0.1.0-alpha.222" />
<PackageReference Include="Siemens.AspNet.Lambda.MsTest.Sdk" />
paket add Siemens.AspNet.Lambda.MsTest.Sdk --version 0.1.0-alpha.222
#r "nuget: Siemens.AspNet.Lambda.MsTest.Sdk, 0.1.0-alpha.222"
#:package Siemens.AspNet.Lambda.MsTest.Sdk@0.1.0-alpha.222
#addin nuget:?package=Siemens.AspNet.Lambda.MsTest.Sdk&version=0.1.0-alpha.222&prerelease
#tool nuget:?package=Siemens.AspNet.Lambda.MsTest.Sdk&version=0.1.0-alpha.222&prerelease
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 | Versions 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. |
-
net9.0
- Microsoft.NET.Test.Sdk (>= 17.14.1)
- Moq (>= 4.20.72)
- MSTest.TestFramework (>= 3.9.3)
- Siemens.AspNet.Lambda.Sdk.Contracts (>= 0.1.0-alpha.222)
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.222 | 158 | 7/16/2025 |
0.1.0-alpha.219 | 149 | 7/14/2025 |
0.1.0-alpha.217 | 70 | 7/11/2025 |
0.1.0-alpha.212 | 144 | 7/8/2025 |
0.1.0-alpha.211 | 112 | 7/3/2025 |
0.1.0-alpha.207 | 103 | 7/3/2025 |
0.1.0-alpha.206 | 221 | 6/30/2025 |
0.1.0-alpha.205 | 88 | 6/27/2025 |
0.1.0-alpha.202 | 87 | 6/27/2025 |
0.1.0-alpha.200 | 87 | 6/27/2025 |
0.1.0-alpha.198 | 86 | 6/27/2025 |
0.1.0-alpha.196 | 92 | 6/27/2025 |
0.1.0-alpha.195 | 88 | 6/27/2025 |
0.1.0-alpha.194 | 85 | 6/27/2025 |
0.1.0-alpha.193 | 86 | 6/27/2025 |
0.1.0-alpha.192 | 87 | 6/27/2025 |
0.1.0-alpha.191 | 86 | 6/27/2025 |
0.1.0-alpha.189 | 106 | 6/26/2025 |
0.1.0-alpha.188 | 105 | 6/26/2025 |
0.1.0-alpha.187 | 103 | 6/26/2025 |
0.1.0-alpha.186 | 110 | 6/26/2025 |
0.1.0-alpha.185 | 109 | 6/26/2025 |
0.1.0-alpha.184 | 104 | 6/26/2025 |
0.1.0-alpha.183 | 103 | 6/26/2025 |
0.1.0-alpha.182 | 105 | 6/26/2025 |
0.1.0-alpha.181 | 109 | 6/25/2025 |
0.1.0-alpha.180 | 110 | 6/24/2025 |
0.1.0-alpha.179 | 112 | 6/23/2025 |
0.1.0-alpha.178 | 116 | 6/23/2025 |
0.1.0-alpha.176 | 113 | 6/23/2025 |
0.1.0-alpha.174 | 115 | 6/19/2025 |
0.1.0-alpha.173 | 114 | 6/19/2025 |
0.1.0-alpha.172 | 115 | 6/17/2025 |
0.1.0-alpha.171 | 112 | 6/16/2025 |
0.1.0-alpha.169 | 116 | 6/16/2025 |
0.1.0-alpha.165 | 223 | 6/13/2025 |
0.1.0-alpha.164 | 228 | 6/13/2025 |
0.1.0-alpha.163 | 226 | 6/13/2025 |