DataAccessMethod 열거형
데이터 구동 테스트에서 데이터 행에 액세스하는 방법을 나타내는 상수를 지정합니다.
네임스페이스: Microsoft.VisualStudio.TestTools.UnitTesting
어셈블리: Microsoft.VisualStudio.QualityTools.UnitTestFramework(Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)
구문
‘선언
Public Enumeration DataAccessMethod
public enum DataAccessMethod
public enum class DataAccessMethod
type DataAccessMethod
public enum DataAccessMethod
멤버
멤버 이름 | 설명 | |
---|---|---|
Sequential | 일정한 순서에 따라 행이 반환됩니다. | |
Random | 무작위 순서로 행이 반환됩니다. |
예제
다음 코드에는 테스트할 클래스와 메서드가 포함되어 있습니다.
using System;
namespace BankAccountNS
{
public class BankAccount
{
private string custName;
private double bal;
public BankAccount(string customerName, double balance)
{
custName = customerName;
bal = balance;
}
public double Balance
{ get { return bal; } }
public void Debit(double amount)
{
if (amount < 0)
throw new ArgumentOutOfRangeException("amount");
bal -= amount;
}
}
}
Imports System
Namespace BankAccountNS
Public Class BankAccount
Private customerName As String
Private bal As Double
Public Sub New(ByVal customerName2 As String, ByVal balance As Double)
customerName = customerName2
bal = balance
End Sub
Public ReadOnly Property Balance() As Double
Get
Return bal
End Get
End Property
Public Sub Debit(ByVal amount As Double)
If amount < 0 Then
Throw New ArgumentOutOfRangeException("amount")
End If
bal -= amount
End Sub
End Class
End Namespace
아래의 테스트는 성공합니다. 이 테스트에서는 다음과 같은 데이터가 들어 있는 sample.mdb Access 데이터베이스를 사용합니다.
Name |
Balance |
양 |
---|---|---|
Jorg Bott |
100 |
25 |
Pedro Ruivo |
70 |
60 |
Mandar Samant |
75 |
71.25 |
Russell King |
159 |
158 |
Jun Cao |
11.99 |
11.22 |
DataAccessMethod가 순차적임을 알 수 있습니다.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BankAccountNS;
using System;
namespace MyCSTestProject
{
[TestClass()]
public class BankAccountTest
{
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[TestMethod()]
[DataSource("System.Data.OleDb",
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\sample.mdb\"",
"Table1",
DataAccessMethod.Sequential)]
public void DebitTest()
{
string customerName = testContextInstance.DataRow["Name"].ToString();
double bal = Convert.ToDouble(testContextInstance.DataRow["Balance"]);
double amt = Convert.ToDouble(testContextInstance.DataRow["Amount"]);
double newBalance = bal - amt;
BankAccount target = new BankAccount(customerName, bal);
target.Debit(amt);
Assert.AreEqual(newBalance, target.Balance, .00);
}
}
}
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports BankAccountNS
Imports System
Namespace TestProject1
<TestClass()> _
Public Class BankAccountTest
Private testContextInstance As TestContext
Public Property TestContext() As TestContext
Get
Return testContextInstance
End Get
Set(ByVal Value As TestContext)
testContextInstance = Value
End Set
End Property
<TestMethod()> _
<DataSource("System.Data.OleDb", _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""D:\sample.mdb""", _
"Table1", DataAccessMethod.Sequential)> _
Public Sub DebitTest()
Dim customerName As String = testContextInstance.DataRow("Name").ToString()
Dim balance As Double = Convert.ToDouble(testContextInstance.DataRow("Balance"))
Dim amount As Double = Convert.ToDouble(testContextInstance.DataRow("Amount"))
Dim NewBalance As Double = balance - amount
Dim target As BankAccount = New BankAccount(customerName, balance)
target.Debit(amount)
Assert.AreEqual(NewBalance, target.Balance, 0.0)
End Sub
End Class
End Namespace
참고 항목
참조
Microsoft.VisualStudio.TestTools.UnitTesting 네임스페이스