Last active
October 8, 2017 15:42
-
-
Save wipiano/ad914c09ed3d1a39cf3966f0c1b6b5cf to your computer and use it in GitHub Desktop.
MSTest で TestCaseSource を型付きで書く ref: http://qiita.com/wipiano/items/a5a47811a85bbbf382cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int Add(int x, int y) | |
{ | |
return x + y; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class SampleTest | |
{ | |
public TestContext TestContext { get; set; } | |
[TestCaseSource(nameof(AddTestCaseSource))] | |
[TestMethod] | |
public void AddTest() | |
{ | |
TestContext.Run((int x, int y, int expected) => | |
{ | |
Add(x, y).Is(expected); | |
}); | |
} | |
// テストケース | |
private static readonly object[][] AddTestCaseSource = new object[][] | |
{ | |
// object の配列。。 | |
new object[] {1, 2, 3}, | |
new object[] {4, 4, 8}, | |
new object[] {-1, -1, -2}, | |
new object[] {0, 0, 0}, | |
// ... | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class SampleTest | |
{ | |
public TestContext TestContext { get; set; } | |
[TestCaseSource(nameof(AddTestCaseSource))] | |
[TestMethod] | |
public void AddTest() | |
{ | |
TestContext.Run((AddTestCase testCase) => | |
{ | |
Add(testCase.X, testCase.Y).Is(testCase.Expected); | |
}); | |
} | |
private static readonly object[][] AddTestCaseSource = new object[][] | |
{ | |
new object[] {new AddTestCase() {X = 1, Y = 2, Expected = 3}}, | |
new object[] {new AddTestCase() {X = 4, Y = 4, Expected = 8}}, | |
new object[] {new AddTestCase() {X = -1, Y = -1, Expected = -2}}, | |
new object[] {new AddTestCase() {X = 0, Y = 0, Expected = 0}}, | |
// ... | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class SampleTest | |
{ | |
public TestContext TestContext { get; set; } | |
[TestCaseSource(nameof(AddTestCaseSource))] | |
[TestMethod] | |
public void AddTest() | |
{ | |
TestContext.Run((AddTestCase testCase) => | |
{ | |
Add(testCase.X, testCase.Y).Is(testCase.Expected); | |
}); | |
} | |
private static readonly object[][] AddTestCaseSource = new object[][] | |
{ | |
new AddTestCase() {X = 1, Y = 2, Expected = 3}, | |
new AddTestCase() {X = 4, Y = 4, Expected = 8}, | |
new AddTestCase() {X = -1, Y = -1, Expected = -2}, | |
new AddTestCase() {X = 0, Y = 0, Expected = 0}, | |
// ... | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class AddTestCase | |
{ | |
public int X { get; set; } | |
public int Y { get; set; } | |
public int Expected { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class AddTestCase | |
{ | |
public int X { get; set; } | |
public int Y { get; set; } | |
public int Expected { get; set; } | |
public static implicit operator object[](AddTestCase testCase) | |
=> new object[] {testCase}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment