SORU
18 HAZİRAN 2009, PERŞEMBE


MSTest NUnits deneme durumu için bir Eşdeğer Var mı

NUnit TestCase özelliği oldukça kullanışlı, her test için ayrı bir yöntem gerek kalmadan test parametreleri belirlemek için hızlı bir yol olarak buluyorum. MSTest benzer bir şey var mı?

 [TestFixture]  
 public class StringFormatUtilsTest  
 {  
     [TestCase("tttt", "")]  
     [TestCase("", "")]  
     [TestCase("t3a4b5", "345")]  
     [TestCase("3&5*", "35")]  
     [TestCase("123", "123")]  
     public void StripNonNumeric(string before, string expected)  
     {  
         string actual = FormatUtils.StripNonNumeric(before);  
         Assert.AreEqual(expected, actual);  
     }  
 }  

CEVAP
23 EKİM 2013, ÇARŞAMBA


Bu geç bir cevap biliyorum ama umarım başkalarına faydası dokunur.

Her yerde zarif bir çözüm aradım ve kendimi yazılı sona erdi. Birim testleri binlerce ve yineleme yüz binlerce 20 projelerinde kullanıyoruz. Hiç cevapsız bir yendi.

https://github.com/Thwaitesy/MSTestHacks

1)NuGet paketini yükleyin.

2)Test sınıf TestBase devralır

public class UnitTest1 : TestBase
{ }

3)Özelliği, Alan veya Yöntem oluşturmak, döndürür IEnumerable

[TestClass]
public class UnitTest1 : TestBase
{
    private IEnumerable<int> Stuff
    {
        get
        {
            //This could do anything, get a dynamic list from anywhere....
            return new List<int> { 1, 2, 3 };
        }
    }
}

4)Test yöntemi için MSTest Kaynağı niteliği, IEnumerable adı yukarıda işaret ekleyin. Bu tam olması gerekir.

[TestMethod]
[DataSource("Namespace.UnitTest1.Stuff")]
public void TestMethod1()
{
    var number = this.TestContext.GetRuntimeDataSourceObject<int>();

    Assert.IsNotNull(number);
}

Sonuç:3 yineleme sadece normal veri Kaynağı gibi :)

using Microsoft.VisualStudio.TestTools.UnitTesting;
using MSTestHacks;

namespace Namespace
{
    [TestClass]
    public class UnitTest1 : TestBase
    {
        private IEnumerable<int> Stuff
        {
            get
            {
                //This could do anything, get a dynamic list from anywhere....
                return new List<int> { 1, 2, 3 };
            }
        }

        [TestMethod]
        [DataSource("Namespace.UnitTest1.Stuff")]
        public void TestMethod1()
        {
            var number = this.TestContext.GetRuntimeDataSourceObject<int>();

            Assert.IsNotNull(number);
        }
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BASS212M

    BASS212M

    15 Temmuz 2009
  • DJPixcell

    DJPixcell

    20 NİSAN 2007
  • lifeisforsharingDT

    lifeisforsha

    18 Mayıs 2011