Last active
January 30, 2018 13:41
-
-
Save lavn0/ad928bf9f71ca609f912e7b953f9cb1f to your computer and use it in GitHub Desktop.
MSTestV2関連
This file contains hidden or 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 UnitTest1 | |
{ | |
[DataTestMethod] | |
[DataRow(1, "001")] | |
[DataRow(2, "002")] | |
[DataRow(3, "003")] | |
public void Test1(int num, string str) | |
{ | |
Assert.AreEqual(str, num.ToString("D3")); | |
} | |
} |
This file contains hidden or 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 Sample | |
{ | |
public static IEnumerable<object[]> GetParameters() | |
{ | |
yield return new object[] { "hoge", new List<int> { 1, 10, 11 }, 22 }; | |
yield return new object[] { "fuga", new List<int> { 2, 27, 88 }, 117 }; | |
yield return new object[] { "piyo", new List<int> { 5, 64 }, 69 }; | |
} | |
[TestMethod] | |
[DynamicData("GetParameters", DynamicDataSourceType.Method)] | |
public void Test1(string str, List<int> items, int result) | |
{ | |
Assert.AreEqual(result, items.Sum()); | |
} | |
} |
This file contains hidden or 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
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System.Collections.Generic; | |
using System.Linq; | |
[TestClass] | |
public class Sample | |
{ | |
[CombinationTestMethod(2, "おはよう", "こんにちは", "こんばんは", "さようなら")] | |
public void Test(string item1, string item2) | |
{ | |
System.Diagnostics.Debug.WriteLine($"{item1}, {item2}"); | |
// 以下が出力される | |
//おはよう, こんにちは | |
//おはよう, こんばんは | |
//おはよう, さようなら | |
//こんにちは, こんばんは | |
//こんにちは, さようなら | |
//こんばんは, さようなら | |
} | |
} | |
public class CombinationTestMethodAttribute : TestMethodAttribute | |
{ | |
public CombinationTestMethodAttribute(int count, params object[] data) | |
{ | |
this.Count = count; | |
this.Data = data; | |
} | |
public int Count { get; } | |
public object[] Data { get; } | |
public IEnumerable<IEnumerable<object>> GetCombination() => Combination(this.Data, this.Count); | |
public override TestResult[] Execute(ITestMethod testMethod) => this.GetCombination().Select(c => testMethod.Invoke(c.ToArray())).ToArray(); | |
/// <summary>組み合わせ</summary> | |
private static IEnumerable<IEnumerable<T>> Combination<T>(IEnumerable<T> items, int r) | |
{ | |
if (r == 0) | |
{ | |
yield return Enumerable.Empty<T>(); | |
yield break; | |
} | |
int i = 1; | |
foreach (var item in items) | |
foreach (var c in Combination(items.Skip(i++), r - 1)) | |
yield return Before(c, item); | |
} | |
private static IEnumerable<T> Before<T>(IEnumerable<T> items, T first) | |
{ | |
yield return first; | |
foreach (var item in items) yield return item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment