你怎么样(虚拟)的数据添加到您的单元测试? [英] How do you add sample (dummy) data to your unit tests?

查看:124
本文介绍了你怎么样(虚拟)的数据添加到您的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更大的项目我的单元测试通常需要一些虚拟(样品)数据与运行。一些默认的客户,用户等我想知道你的设置的样子。




  1. 你如何组织/维持这个数据?

  2. 你怎么把它应用到你的单元测试(任何自动化工具)?

  3. 请你实际需要的测试数据,或你认为它是无用的?



我目前的解决方案:



我区分的主数据的和的的样本数据的其中前者将可在系统投入生产(安装了第一次),后者是典型的使用情况下,我需要为我的测试运行(与发展过程中发挥)。



我店所有这一切在一个Excel文件(因为它是如此该死的易维护),其中每个工作表中包含特定实体(如:用户,客户等),并标记主或样品



我有2个测试用例,我(小姐)用来导入所需的数据:




  1. InitForDevelopment(创建模式,导入主数据,导入示例数据)

  2. InitForProduction(创建模式,导入主数据)


解决方案

我用的是库模式,有一个是由实例化一个虚拟存储库有问题的单元测试,它提供了一组已知的数据,它包括一个例子是内和超出范围的各个字段。



这意味着我可以测试我的代码通过提供从测试单元测试或在运行时产生库(通过依赖注入(城堡))实例化资源库不变。



我不知道为此一个良好的网络参考,但我从Apress出版出版的史蒂芬·桑德森的专业ASP.NET MVC 1.0本书学到了很多。 MVC方法自然提供了关切,是必要的,让您的测试用较少依赖操作的分离。



的基本要素是您存储库实现数据访问接口, 。同样的接口,然后由您在测试项目中建造一个假的库实现



在我当前的项目我有这样一个接口:

 命名空间myProject.Abstract 
{
公共接口ISeriesRepository
{
&IQueryable的LT;系列>系列{搞定; }
}
}

这是作为我的两个实时数据仓库(使用LINQ to SQL),也是一个假的库这样的:

 命名空间myProject.Tests.Respository 
{
类FakeRepository:ISeriesRepository
{
私有静态的IQueryable<&系列GT; fakeSeries =新的List<&系列GT; {
系列新{ID = 1,名称=系列1,openingDate =新日期时间(2001,1,1)},
系列新{ID = 2,名称=系列2,openingDate =新的DateTime(2002,1,30),
...
系列新{ID = 10,NAME =Series10,openingDate =新日期时间(2001,5,5)
} .AsQueryable();

公众的IQueryable<&系列GT;系列
{
{返回fakeSeries; }
}
}
}



那么这就是消费类该数据被实例化传递库引用的构造器:

 命名空间myProject的
{
公共类SeriesProcessor
{
私人ISeriesRepository seriesRepository;

公共无效SeriesProcessor(ISeriesRepository seriesRepository)
{
this.seriesRepository = seriesRepository;
}

公众的IQueryable<&系列GT; GetCurrentSeries()
{
从s在seriesRepository.Series
返回,其中s.openingDate.Date< = DateTime.Now.Date
选择s;
}
}
}



然后在我的测试中,我可以这样来解决:

 命名空间myProject.Tests 
{
[TestClass中]
公共类SeriesTests
{
[TestMethod的]
公共无效Meaningful_Test_Name()
{
//安排
SeriesProcessor处理器=新SeriesProcessor(新FakeRepository());

//法案
&IQueryable的LT;系列> currentSeries = processor.GetCurrentSeries();

//断言
Assert.AreEqual(currentSeries.Count(),10);
}

}
}



接着看在CastleWindsor的控制方法反转为您直播的项目,让你的产品代码,通过依赖注入自动实例化的活动资料库。这应该让你更接近,你需要的人。


In bigger projects my unit tests usually require some "dummy" (sample) data to run with. Some default customers, users, etc. I was wondering how your setup looks like.

  1. How do you organize/maintain this data?
  2. How do you apply it to your unit tests (any automation tool)?
  3. Do you actually require test data or do you think it's useless?

My current solution:

I differentiate between Master data and Sample data where the former will be available when the system goes into production (installed for the first time) and the latter are typical use cases I require for my tests to run (and to play during development).

I store all this in an Excel file (because it's so damn easy to maintain) where each worksheet contains a specific entity (e.g. users, customers, etc.) and is flagged either master or sample.

I have 2 test cases which I (miss)use to import the necessary data:

  1. InitForDevelopment (Create Schema, Import Master data, Import Sample data)
  2. InitForProduction (Create Schema, Import Master data)

解决方案

I use the repository pattern and have a dummy repository that's instantiated by the unit tests in question, it provides a known set of data that encompasses a examples that are both within and out of range for various fields.

This means that I can test my code unchanged by supplying the instantiated repository from the test unit for testing or the production repository at runtime (via a dependency injection (Castle)).

I don't know of a good web reference for this but I learnt much from Steven Sanderson's Professional ASP.NET MVC 1.0 book published by Apress. The MVC approach naturally provides the separation of concern that's necessary to allow your testing to operate with fewer dependencies.

The basic elements are that you repository implements an interface for data access, that same interface is then implemented by a fake repository that you construct in your test project.

In my current project I have an interface thus:

namespace myProject.Abstract
{
    public interface ISeriesRepository
    {
        IQueryable<Series> Series { get; }
    }
}

This is implemented as both my live data repository (using Linq to SQL) and also a fake repository thus:

namespace myProject.Tests.Respository
{
    class FakeRepository : ISeriesRepository
    {
        private static IQueryable<Series> fakeSeries = new List<Series> {
            new Series { id = 1, name = "Series1", openingDate = new DateTime(2001,1,1) },
            new Series { id = 2, name = "Series2", openingDate = new DateTime(2002,1,30),
            ...
            new Series { id = 10, name = "Series10", openingDate = new DateTime(2001,5,5)
        }.AsQueryable();

        public IQueryable<Series> Series
        {
            get { return fakeSeries; }
        }
    }
}

Then the class that's consuming the data is instantiated passing the repository reference to the constructor:

namespace myProject
{
    public class SeriesProcessor
    {
        private ISeriesRepository seriesRepository;

        public void SeriesProcessor(ISeriesRepository seriesRepository)
        {
            this.seriesRepository = seriesRepository;
        }

        public IQueryable<Series> GetCurrentSeries()
        {
            return from s in seriesRepository.Series
                   where s.openingDate.Date <= DateTime.Now.Date
                   select s;
        }
    }
}

Then in my tests I can approach it thus:

namespace myProject.Tests
{
    [TestClass]
    public class SeriesTests
    {
        [TestMethod]
        public void Meaningful_Test_Name()
        {
            // Arrange
            SeriesProcessor processor = new SeriesProcessor(new FakeRepository());

            // Act
            IQueryable<Series> currentSeries = processor.GetCurrentSeries();

            // Assert
            Assert.AreEqual(currentSeries.Count(), 10);
        }

    }
}

Then look at CastleWindsor for the inversion of control approach for your live project to allow your production code to automatically instantiate your live repository through dependency injection. That should get you closer to where you need to be.

这篇关于你怎么样(虚拟)的数据添加到您的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆