Skip to content

Commit 04e88af

Browse files
committed
Endpoint collection example + configurable integration testing
1 parent 38eaa61 commit 04e88af

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

UnitTests/IntegrationTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using netmockery;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace UnitTests
12+
{
13+
public class IntegrationTests
14+
{
15+
private ITestOutputHelper output;
16+
17+
public IntegrationTests(ITestOutputHelper output)
18+
{
19+
this.output = output;
20+
}
21+
private const string FILENAME = "configurations_to_test.txt";
22+
23+
[Fact]
24+
public void KnownConfigurationsTestOK()
25+
{
26+
var configurationsToTest = new List<string>();
27+
configurationsToTest.Add("examples/example1");
28+
if (File.Exists(FILENAME))
29+
{
30+
configurationsToTest.AddRange(from line in File.ReadAllLines(FILENAME) where !string.IsNullOrEmpty(line) && !line.StartsWith("#") select line);
31+
}
32+
33+
output.WriteLine("Configurations:");
34+
foreach (var directory in configurationsToTest)
35+
{
36+
output.WriteLine($" {directory}");
37+
}
38+
output.WriteLine("");
39+
40+
foreach (var directory in configurationsToTest)
41+
{
42+
CheckConfigdirectory(directory);
43+
}
44+
}
45+
46+
public void CheckConfigdirectory(string directory)
47+
{
48+
Assert.True(Directory.Exists(directory), $"Directory {directory} does not exist");
49+
Assert.True(EndpointTestDefinition.HasTestSuite(directory), $"Directory {directory} has not test suite");
50+
var endpointCollection = EndpointCollectionReader.ReadFromDirectory(directory);
51+
Assert.True(endpointCollection.Endpoints.Count() > 0, $"No endpoints defined in {directory}");
52+
53+
output.WriteLine(directory);
54+
var tests = EndpointTestDefinition.ReadFromDirectory(directory);
55+
foreach (var test in tests.Tests)
56+
{
57+
output.WriteLine(test.Name);
58+
var result = test.ExecuteAsync(endpointCollection).Result;
59+
output.WriteLine(result.ResultAsString);
60+
Assert.True(result.OK, $"Test case {result.TestCase.Name}, message '{result.Message}'");
61+
}
62+
}
63+
}
64+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#
2+
# Add endpoint collection definition directories to this file to automatically run test cases for the endpoint definitions
3+
#
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Simple endpoint",
3+
"pathregex": "^/foobar",
4+
5+
"responses": [
6+
{
7+
"match": {},
8+
"response": {
9+
"literal": "Hello world",
10+
"contenttype": "text/plain"
11+
}
12+
}
13+
]
14+
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"name": "Check foobar endpoint",
4+
"requestpath": "/foobar",
5+
"expectedresponsebody": "Hello world"
6+
}
7+
]

UnitTests/project.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
},
1010

1111
"frameworks": {
12-
"net461": { }
12+
"net461": {}
13+
},
14+
15+
"buildOptions": {
16+
"copyToOutput": [
17+
"examples/",
18+
"configurations_to_test.txt"
19+
]
1320
}
1421
}

0 commit comments

Comments
 (0)