MSTest에서 여러 매개 변수로 테스트 방법을 실행하는 방법은 무엇입니까?
NUnit에는 아래와 같은 Values라는 기능이 있습니다.
[Test]
public void MyTest(
[Values(1,2,3)] int x,
[Values("A","B")] string s)
{
// ...
}
이는 테스트 방법이 6 번 실행됨을 의미합니다.
MyTest(1, "A")
MyTest(1, "B")
MyTest(2, "A")
MyTest(2, "B")
MyTest(3, "A")
MyTest(3, "B")
우리는 지금 MSTest를 사용하고 있습니다. 여러 매개 변수로 동일한 테스트를 실행할 수 있도록 이에 상응하는 것이 있습니까?
[TestMethod]
public void Mytest()
{
// ...
}
불행히도 MSTest에서는 지원되지 않습니다. 분명히 확장 성 모델이 있으며 직접 구현할 수 있습니다 . 또 다른 옵션은 데이터 기반 테스트 를 사용 하는 것 입니다.
내 개인적인 의견은 NUnit을 고수하는 것입니다 ...
편집 : Visual Studio 2012, 업데이트 1부터 MSTest의 기능이 비슷합니다. 아래 @McAden의 답변을 참조하십시오.
편집 4 : MSTest V2 2016 년 6 월 17 일에 완료된 것 같습니다 : https://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-mstest- v2 /
원래 답변 :
약 일주일 전 Visual Studio 2012 Update 1에서 비슷한 것이 가능해졌습니다.
[DataTestMethod]
[DataRow(12,3,4)]
[DataRow(12,2,6)]
[DataRow(12,4,3)]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual( q, n / d );
}
편집 : WinRT / Metro에 대한 단위 테스트 프로젝트에서만 사용할 수 있습니다 . 버머
편집 2 : 다음은 Visual Studio에서 "정의로 이동"을 사용하여 찾은 메타 데이터입니다.
#region Assembly Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll, v11.0.0.0
// C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0\ExtensionSDKs\MSTestFramework\11.0\References\CommonConfiguration\neutral\Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll
#endregion
using System;
namespace Microsoft.VisualStudio.TestPlatform.UnitTestFramework
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DataTestMethodAttribute : TestMethodAttribute
{
public DataTestMethodAttribute();
public override TestResult[] Execute(ITestMethod testMethod);
}
}
편집 3 :이 문제는 Visual Studio의 UserVoice 포럼에서 제기되었습니다. 마지막 업데이트 상태 :
STARTED · Visual Studio Team ADMIN Visual Studio Team (Microsoft Visual Studio 제품 팀)이 응답했습니다. · 2016 년 4 월 25 일 의견을 보내 주셔서 감사합니다. 우리는이 일을 시작했습니다.
프라 탑 락쉬 만 비주얼 스튜디오
이 기능은 현재 시험판 버전 이며 VS 2015에서 작동합니다.
예를 들면 다음과 같습니다.
[TestClass]
public class UnitTest1
{
[DataTestMethod]
[DataRow(1, 2, 2)]
[DataRow(2, 3, 5)]
[DataRow(3, 5, 8)]
public void AdditionTest(int a, int b, int result)
{
Assert.AreEqual(result, a + b);
}
}
아무도 언급하지 않았으므로 NUnit의 Value
(또는 TestCase
) 속성 과 정확히 동일 하지는 않지만 MSTest에는 DataSource
속성이 있으므로 비슷한 일을 할 수 있습니다. NUnit의 기능만큼 간단하지는 않지만 데이터베이스 나 XML 파일에 연결할 수 있습니다.
MSTest has a powerful attribute called DataSource, using this you can perform data driven test as you asked. You can have your test data in XML, CSV or in a database. Here are few links that will guide you
http://visualstudiomagazine.com/articles/2009/09/15/unit-testing-with-vsts2008-part-3.aspx http://msdn.microsoft.com/en-us/library/ms182527.aspx
http://msdn.microsoft.com/en-us/library/ms243192.aspx
Hope this will help you.
There is, of course, another way to do this which has not been discussed in this thread, i.e. by way of inheritance of the class containing the TestMethod. In the following example, only one TestMethod has been defined but two test cases have been made.
In Visual Studio 2012, it creates two tests in the TestExplorer:
- DemoTest_B10_A5.test
DemoTest_A12_B4.test
public class Demo { int a, b; public Demo(int _a, int _b) { this.a = _a; this.b = _b; } public int Sum() { return this.a + this.b; } } public abstract class DemoTestBase { Demo objUnderTest; int expectedSum; public DemoTestBase(int _a, int _b, int _expectedSum) { objUnderTest = new Demo(_a, _b); this.expectedSum = _expectedSum; } [TestMethod] public void test() { Assert.AreEqual(this.expectedSum, this.objUnderTest.Sum()); } } [TestClass] public class DemoTest_A12_B4 : DemoTestBase { public DemoTest_A12_B4() : base(12, 4, 16) { } } public abstract class DemoTest_B10_Base : DemoTestBase { public DemoTest_B10_Base(int _a) : base(_a, 10, _a + 10) { } } [TestClass] public class DemoTest_B10_A5 : DemoTest_B10_Base { public DemoTest_B10_A5() : base(5) { } }
MsTest does not support that feature but you can implement your own attribute to achieve that. have a look at the below:
http://blog.drorhelper.com/2011/09/enabling-parameterized-tests-in-mstest.html
It's very simple to implement - you should use TestContext
property and TestPropertyAttribute
.
Example
public TestContext TestContext { get; set; }
private List<string> GetProperties()
{
return TestContext.Properties
.Cast<KeyValuePair<string, object>>()
.Where(_ => _.Key.StartsWith("par"))
.Select(_ => _.Value as string)
.ToList();
}
//usage
[TestMethod]
[TestProperty("par1", "http://getbootstrap.com/components/")]
[TestProperty("par2", "http://www.wsj.com/europe")]
public void SomeTest()
{
var pars = GetProperties();
//...
}
I couldn't get The DataRowAttribute
to work in Visual Studio 2015, this is what I ended up with:
[TestClass]
public class Tests
{
private Foo _toTest;
[TestInitialize]
public void Setup()
{
this._toTest = new Foo();
}
[TestMethod]
public void ATest()
{
this.Perform_ATest(1, 1, 2);
this.Setup();
this.Perform_ATest(100, 200, 300);
this.Setup();
this.Perform_ATest(817001, 212, 817213);
this.Setup();
}
private void Perform_ATest(int a, int b, int expected)
{
//Obviously this would be way more complex...
Assert.IsTrue(this._toTest.Add(a,b) == expected);
}
}
public class Foo
{
public int Add(int a, int b)
{
return a + b;
}
}
The real solution here is to just use NUnit (unless you're stuck in MSTest like I am in this particular instance).
'Programing' 카테고리의 다른 글
장고 수정 관리자 복수 (0) | 2020.07.06 |
---|---|
'.'는 무엇입니까 (0) | 2020.07.06 |
MyISAM과 InnoDB를 언제 사용해야합니까? (0) | 2020.07.06 |
iOS에서 합성 된 속성의 이름을 밑줄로 바꾸는 이유는 무엇입니까? (0) | 2020.07.06 |
FFmpeg로 이미지에서 비디오를 만드는 방법은 무엇입니까? (0) | 2020.07.06 |