Skip to content

Commit 55a92b1

Browse files
add new output generators for mstest and xunit, update readme
1 parent 38ecac9 commit 55a92b1

File tree

3 files changed

+195
-9
lines changed

3 files changed

+195
-9
lines changed

README.md

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ The following test will output to console and copy to clipboard the following te
359359

360360
## ✅ Usage in Tests
361361

362-
### ✅ Use `TestimizeGeneratedTestCases` Attributte
362+
### ✅ Use `TestimizeGeneratedTestCases` NUnit Attributte
363363

364364
```csharp
365365
[TestFixture]
@@ -390,6 +390,67 @@ public class SampleTests
390390

391391
---
392392

393+
### ✅ Use `TestimizeGeneratedTestCases` MSTest Attributte
394+
395+
```csharp
396+
[TestClass]
397+
public class SampleTests
398+
{
399+
public static List<TestCase> ConfigureEngine() =>
400+
TestimizeEngine.Configure(
401+
parameters => parameters
402+
.AddText(6, 12)
403+
.AddEmail(5, 10)
404+
.AddPhone(6, 8)
405+
.AddText(4, 10)
406+
, settings =>
407+
{
408+
settings.Mode = TestGenerationMode.HybridArtificialBeeColony;
409+
settings.TestCaseCategory = TestCaseCategory.Validation;
410+
}
411+
).Generate();
412+
413+
[DataTestMethod]
414+
[TestimizeGeneratedTestCases(nameof(ConfigureEngine))]
415+
public void TestABCGeneration(string textValue, string email, string phone, string anotherText)
416+
{
417+
// your test logic here
418+
}
419+
}
420+
421+
```
422+
---
423+
424+
### ✅ Use `TestimizeGeneratedTestCases` xUnit Attributte
425+
426+
```csharp
427+
public class SampleXUnitTests
428+
{
429+
public static List<TestCase> ConfigureEngine() =>
430+
TestimizeEngine.Configure(
431+
parameters => parameters
432+
.AddText(6, 12)
433+
.AddEmail(5, 10)
434+
.AddPhone(6, 8)
435+
.AddText(4, 10)
436+
, settings =>
437+
{
438+
settings.Mode = TestGenerationMode.HybridArtificialBeeColony;
439+
settings.TestCaseCategory = TestCaseCategory.Validation;
440+
}
441+
).Generate();
442+
443+
[Theory]
444+
[TestimizeGeneratedTestCases(nameof(ConfigureEngine))]
445+
public void TestABCGeneration(string textValue, string email, string phone, string anotherText)
446+
{
447+
// your test logic here
448+
}
449+
}
450+
451+
```
452+
---
453+
393454
## 🛠 Available Parameters
394455

395456
| Type | Parameter Class |
@@ -473,14 +534,18 @@ Define custom equivalence classes and settings for each input type:
473534
}
474535

475536
```
476-
## 📦 Output Generators
477-
478-
| Class Name | Description |
479-
|----------------------------------------|------------------------------------|
480-
| `NUnitTestCaseAttributeOutputGenerator` | `[TestCase(...)]` attributes |
481-
| `NUnitTestCaseSourceOutputGenerator` | `IEnumerable<object[]>` method |
482-
| `CsvTestCaseOutputGenerator` | CSV output |
483-
| `JsonTestCaseOutputGenerator` | JSON test data output |
537+
## 📦 Supported Output Generators
538+
539+
| Output Generator Class | Target Framework | Description |
540+
|----------------------------------------------|------------------|-----------------------------------------------------------------------------|
541+
| `NUnitTestCaseAttributeOutputGenerator` | NUnit | Generates `[TestCase(...)]` attributes per test case. |
542+
| `NUnitTestCaseSourceOutputGenerator` | NUnit | Outputs a method with `IEnumerable<object[]>` for use with `TestCaseSource`.|
543+
| `XUnitInlineDataOutputGenerator` | xUnit | Generates `[InlineData(...)]` attributes for xUnit tests. |
544+
| `MSTestTestMethodAttributeOutputGenerator` | MSTest | Generates `[DataTestMethod]` and `[DataRow(...)]` attributes. |
545+
| `CsvTestCaseOutputGenerator` | CSV | Writes test cases as CSV rows (e.g., for import/export or tools). |
546+
| `JsonTestCaseOutputGenerator` | JSON | Outputs test cases as a structured JSON array (used in pipelines/tools). |
547+
| `FactoryMethodTestCaseOutputGenerator` | Any (.NET) | Generates `List<Model>` factory-style methods for object-based test cases. |
548+
484549

485550
---
486551

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// <copyright file="MSTestTestMethodAttributeOutputGenerator.cs" company="Automate The Planet Ltd.">
2+
// Copyright 2025 Automate The Planet Ltd.
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// You may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
// Unless required by applicable law or agreed to in writing,
7+
// software distributed under the License is distributed on an "AS IS" BASIS,
8+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
// See the License for the specific language governing permissions and
10+
// limitations under the License.
11+
// </copyright>
12+
// <author>Anton Angelov</author>
13+
// <site>https://automatetheplanet.com/</site>
14+
15+
using System.Text;
16+
using Testimize.Parameters.Core;
17+
using TextCopy;
18+
19+
namespace Testimize.OutputGenerators;
20+
public class MSTestTestMethodAttributeOutputGenerator : TestCaseOutputGenerator
21+
{
22+
public override void GenerateOutput(string methodName, IEnumerable<TestCase> testCases, TestCaseCategory testCaseCategory = TestCaseCategory.All)
23+
{
24+
var sb = new StringBuilder();
25+
var testCasesToBeGenerated = FilterTestCasesByCategory(testCases, testCaseCategory);
26+
27+
Console.WriteLine($"🧪 Total test cases: {testCasesToBeGenerated.Count()}");
28+
29+
foreach (var testCase in testCasesToBeGenerated)
30+
{
31+
var values = testCase.Values.Select(x => ToLiteral(x.Value)).ToList();
32+
var expectedMessage = testCase.Values.FirstOrDefault(v =>
33+
!string.IsNullOrWhiteSpace(v.ExpectedInvalidMessage))?.ExpectedInvalidMessage;
34+
values.Add(ToLiteral(expectedMessage ?? string.Empty));
35+
36+
sb.AppendLine($"[DataRow({string.Join(", ", values)})]");
37+
}
38+
39+
var output = sb.ToString();
40+
Console.WriteLine(output);
41+
ClipboardService.SetText(output);
42+
Console.WriteLine("✅ MSTest DataRow attributes copied to clipboard.");
43+
}
44+
45+
private static string ToLiteral(object value)
46+
{
47+
return value switch
48+
{
49+
null => "null",
50+
string s => $"\"{EscapeString(s)}\"",
51+
bool b => b.ToString().ToLowerInvariant(),
52+
DateTime dt => $"\"{dt:dd-MM-yyyy}\"",
53+
string[] arr => $"new[] {{ {string.Join(", ", arr.Select(a => $"\"{EscapeString(a)}\""))} }}",
54+
_ => value.ToString()
55+
};
56+
}
57+
58+
private static string EscapeString(string input) =>
59+
input.Replace("\\", "\\\\").Replace("\"", "\\\"");
60+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// <copyright file="XUnitInlineDataOutputGenerator.cs" company="Automate The Planet Ltd.">
2+
// Copyright 2025 Automate The Planet Ltd.
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// You may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
// Unless required by applicable law or agreed to in writing,
7+
// software distributed under the License is distributed on an "AS IS" BASIS,
8+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
// See the License for the specific language governing permissions and
10+
// limitations under the License.
11+
// </copyright>
12+
// <author>Anton Angelov</author>
13+
// <site>https://automatetheplanet.com/</site>
14+
15+
using System.Text;
16+
using Testimize.Parameters.Core;
17+
using TextCopy;
18+
19+
namespace Testimize.OutputGenerators;
20+
21+
public class XUnitInlineDataOutputGenerator : TestCaseOutputGenerator
22+
{
23+
public override void GenerateOutput(string methodName, IEnumerable<TestCase> testCases, TestCaseCategory testCaseCategory = TestCaseCategory.All)
24+
{
25+
var sb = new StringBuilder();
26+
var testCasesToBeGenerated = FilterTestCasesByCategory(testCases, testCaseCategory);
27+
28+
Console.WriteLine($"🧪 Total test cases: {testCasesToBeGenerated.Count()}");
29+
30+
foreach (var testCase in testCasesToBeGenerated)
31+
{
32+
var values = testCase.Values.Select(x => ToLiteral(x.Value)).ToList();
33+
var expectedMessage = testCase.Values.FirstOrDefault(v =>
34+
!string.IsNullOrWhiteSpace(v.ExpectedInvalidMessage))?.ExpectedInvalidMessage;
35+
values.Add(ToLiteral(expectedMessage ?? string.Empty));
36+
37+
sb.AppendLine($"[InlineData({string.Join(", ", values)})]");
38+
}
39+
40+
var output = sb.ToString();
41+
Console.WriteLine(output);
42+
ClipboardService.SetText(output);
43+
Console.WriteLine("✅ xUnit InlineData attributes copied to clipboard.");
44+
}
45+
46+
private static string ToLiteral(object value)
47+
{
48+
return value switch
49+
{
50+
null => "null",
51+
string s => $"\"{EscapeString(s)}\"",
52+
bool b => b.ToString().ToLowerInvariant(),
53+
DateTime dt => $"\"{dt:dd-MM-yyyy}\"",
54+
string[] arr => $"new[] {{ {string.Join(", ", arr.Select(a => $"\"{EscapeString(a)}\""))} }}",
55+
_ => value.ToString()
56+
};
57+
}
58+
59+
private static string EscapeString(string input) =>
60+
input.Replace("\\", "\\\\").Replace("\"", "\\\"");
61+
}

0 commit comments

Comments
 (0)