skUnit 0.29.0-beta

This is a prerelease version of skUnit.
dotnet add package skUnit --version 0.29.0-beta
NuGet\Install-Package skUnit -Version 0.29.0-beta
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="skUnit" Version="0.29.0-beta" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add skUnit --version 0.29.0-beta
#r "nuget: skUnit, 0.29.0-beta"
#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.
// Install skUnit as a Cake Addin
#addin nuget:?package=skUnit&version=0.29.0-beta&prerelease

// Install skUnit as a Cake Tool
#tool nuget:?package=skUnit&version=0.29.0-beta&prerelease

skUnit

Build and Deploy NuGet version (skUnit) NuGet downloads

skUnit is a testing tool for SemanticKernel units, such as plugin functions and kernels.

For example, you can use skUnit to test a GetSentiment function that analyzes a text and returns its sentiment, such as "Happy" or "Sad". You can write different scenarios to check how the function behaves with various inputs, such as:

## PARAMETER input
Such a beautiful day it is

## ANSWER Equals
Happy

This scenario verifies that the function returns "Happy" when the input is "Such a beautiful day it is".

This is an Invocation Scenario, which tests a single function call. You can also write Chat Scenarios, which test a sequence of interactions between the user and the SemanticKernel.

skUnit offers many features to help you write more complex and flexible scenarios. In this section, we will show you some of them with an example.

Suppose you have a function called GetSentiment that takes two parameters and returns a sentence describing the sentiment of the text:

Parameters:

  • input: the text to analyze
  • options: the possible sentiment values, such as happy, angry, or sad

Returns: a sentence like "The sentiment is happy" or "The sentiment of this text is sad".

Here is a scenario that tests this function:

# SCENARIO GetSentimentHappy

## PARAMETER input
Such a beautiful day it is

## PARAMETER options
happy, angry

## ANSWER SemanticSimilar
The sentiment is happy

The most interesting part of this scenario is:

## ANSWER SemanticSimilar
The sentiment is happy

This line specifies the expected output of the function and how to compare it with the actual output. In this case, the output should be semantically similar to "The sentiment is happy". This means that the output can have different words or syntax, but the meaning should be the same.

This is a powerful feature of skUnit scenarios, as it allows you to use OpenAI itself to perform semantic comparisons.

You can also write this assertion in another way:

## ANSWER
The sentiment of the sentence is happy

## CHECK SemanticSimilar
The sentiment is happy

In this style, the expected answer is just a reminder and not used for comparison; and then a ## CHECK SemanticSimilar is used to explicitly perform the assertion.

However, SemanticSimilar is not the only assertion method. There are many more assertion checks available (like SemanticCondition, Equals).

You can see the full list of CHECK statements here: CHECK Statement spec.

Scenarios are Valid Markdowns

One of the benefits of skUnit scenarios is that they are valid Markdown files, which makes them very readable and easy to edit.

skUnit scenarios are valid Markdown files, which makes them very readable and easy to edit.

For example, you can see how clear and simple this scenario is: Chatting about Eiffel height

image

Executing a Test Using a Scenario

Executing tests is a straightforward process. You have the flexibility to utilize any preferred test frameworks such as xUnit, nUnit, or MSTest. With just two lines of code, you can load and run a test:

var scenarios = InvocationScenario.LoadFromText(scenario);
await SemanticKernelAssert.CheckScenarioAsync(Kernel, scenarios);

The standout feature of skUnit is its detailed test output. Here's an example:

# SCENARIO GetSentimentHappy_Fail

## PARAMETER input
You are such a bastard, Fuck off!

## PARAMETER options
happy, angry

## EXPECTED ANSWER
The sentiment is happy

## ACTUAL ANSWER
angry

## ANSWER SemanticSimilar
The sentiment is happy
Exception as EXPECTED:
The two texts are not semantically equivalent. The first text expresses anger, while the second text expresses happiness.

As demonstrated, when a SemanticSimilar check fails, it provides a semantic explanation for the failure. This feature proves to be incredibly useful during debugging.

Here's another example of an executing The Chatting about Eiffel height test:

image

Documents

To better understand skUnit, Check these documents:

Requirements

  • .NET 7.0 or higher
  • An OpenAI API key

Installation

You can easily add skUnit to your project as it is available as a NuGet package. To install it, execute the following command in your terminal:

dotnet add package skUnit

Afterwards, you'll need to instantiate the SemanticKernelAssert class in your test constructor. This requires passing your OpenAI subscription details as parameters:

public class MyTest
{
  SemanticKernelAssert SemanticKernelAssert { get; set; }
  MyTest(ITestOutputHelper output)
  {
    SemanticKernelAssert = new SemanticKernelAssert(_deploymentName, _endpoint, _apiKey, message => output.WriteLine(message));
  }

  [Fact]
  MyFunctionShouldWork()
  {
    var scenarios = await InvocationScenario.LoadFromResourceAsync(scenario);
    await SemanticKernelAssert.CheckScenarioAsync(Kernel, scenarios);
  }
}

And that's all there is to it! 😊

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.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.29.0-beta 85 1/4/2024
0.28.0-beta 60 1/2/2024
0.27.0-beta 83 1/2/2024
0.26.0-beta 63 1/2/2024
0.25.0-beta 77 12/30/2023
0.24.0-beta 63 12/29/2023
0.23.0-beta 70 12/28/2023
0.22.0-beta 68 12/28/2023
0.21.0-beta 54 12/28/2023
0.20.0-beta 74 12/28/2023
0.19.0-beta 54 12/27/2023
0.18.0-beta 63 12/27/2023
0.16.0-beta 53 12/27/2023
0.15.0-beta 69 12/27/2023
0.14.0-beta 65 12/26/2023
0.13.0-beta 58 12/26/2023
0.12.0-beta 60 12/26/2023
0.11.0-beta 64 12/26/2023
0.10.0-beta 61 12/25/2023
0.9.0-beta 65 12/25/2023
0.8.0-beta 61 12/25/2023
0.7.0-beta 62 12/25/2023
0.6.0-beta 62 12/25/2023
0.5.0-beta 56 12/24/2023
0.4.0-beta 54 12/24/2023
0.3.0-beta 64 12/24/2023
0.1.0-beta 57 12/24/2023