-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitTestClass.cs
119 lines (94 loc) · 3.38 KB
/
UnitTestClass.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
namespace EasyTestFileXunit.Tests.Samples;
using System.IO;
using System.Threading.Tasks;
using global::EasyTestFile;
using global::EasyTestFile.Json;
using Xunit;
[UsesEasyTestFile]
public class UnitTestClass
{
// begin-snippet: LoadAsText
[Fact]
public async Task LoadAsText()
{
// Executing this test for the first time will create an empty testfile and throw an exception.
// Executing this test for the second time, this statement will read the testfile
// and returns the content as a string.
string text = await EasyTestFile.LoadAsText();
// and do whatever you want
}
// end-snippet
// begin-snippet: LoadAsStream
[Fact]
public async Task LoadAsStream()
{
// You can also load the testfile content as a stream.
Stream stream = await EasyTestFile.LoadAsStream();
}
// end-snippet
// begin-snippet: LoadJson
[Fact] // or [Test]
public async Task JsonTestFile()
{
// load testfile
var settings = new EasyTestFileSettings();
settings.UseExtension("json");
TestFile testFile = EasyTestFile.Load(settings);
// deserialize testfile using Newtonsoft Json.
Person person = await testFile.AsObjectUsingNewtonsoft<Person>();
// do something with person object
// i.e. sut.Process(person);
}
public class Person
{
public string Name { get; set; }
}
// end-snippet
// begin-snippet: LoadAsTestFileBasic
[Fact]
public async Task LoadAsTestFile()
{
// You can also load the test file as a TestFile object.
TestFile testFile = EasyTestFile.Load();
// then you can load the content as a stream
Stream stream = await testFile.AsStream();
// or like
string text = await testFile.AsText();
}
// end-snippet
// begin-snippet: LoadAsTestFile
[Fact]
public async Task LoadAsTestFileWithJson()
{
// You can also load the test file as a TestFile object.
TestFile testFile = EasyTestFile.Load();
// then you can load the content as a stream
Stream stream = await testFile.AsStream();
// or use extension methods like
Person person = await testFile.AsObjectUsingNewtonsoft<Person>();
// or like
string text = await testFile.AsText();
}
// end-snippet
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Used for markdown")]
private async Task Configuration()
{
// begin-snippet: Configuration
var settings = new EasyTestFileSettings();
// specify assembly containing the testfiles (only applicable when embedded).
settings.UseAssembly(typeof(Person).Assembly);
// custom directory where testfiles are stored.
settings.UseDirectory("myTestFiles");
settings.UseFileName("filename");
// or
settings.UseMethodName("Configuration2");
settings.SetTestFileNameSuffix("suffix");
settings.UseExtension("jpg");
settings.DisableAutoCreateMissingTestFile();
// Load testfile as object with settings.
TestFile testFile = EasyTestFile.Load(settings);
// or directly as content with settings.
var text = await EasyTestFile.LoadAsText(settings);
// end-snippet
}
}