使用 MSTest 进行数据驱动的单元测试

发布于 2024-07-13 21:34:16 字数 353 浏览 6 评论 0 原文

我正在开发一个使用 MSTest 进行单元测试的项目,我需要为与数据源紧密耦合的模型类编写一些单元测试。 我们在数据库项目 (.dbproj) 中维护模型类所依赖的数据库的源代码。 我正在考虑做的是在我的测试类上编写一个 ClassInitialize 方法,该方法使用数据库项目以编程方式构建数据库的 SQL CE 实例,然后我可以填充数据,我可以将模型类连接到该实例并对其执行测试与已知数据。

如果有人有其他建议,我愿意接受。

我试图弄清楚如何从 ClassInitialize 方法中的代码中运行此数据库项目。 我还没有找到任何关于如何做类似事情的好例子。 这可能吗? 如果是这样,怎么办? 有人有代码示例吗? 谢谢。

I'm working on a project that uses MSTest for unit testing and I need to write some unit tests for a model class thats fairly tightly coupled to the data source. We maintain the source code for the database that the model classes are dependent on in a Database Project (.dbproj). What I was thinking of doing was writing a ClassInitialize method on my test class that uses the Database Project to programatically build a SQL CE instance of our database that I could then populate with data that I could connect my model class to and perform tests against it with known data.

I'm open to other suggestions if anyone has them.

What I'm trying to figure out how to do is run this Database Project from within code in my ClassInitialize method. I haven't been able to find any good examples of how to do something like that. Is this possible? If so, how? Anyone have a code sample? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

早乙女 2024-07-20 21:34:16

不幸的是,数据层的单元测试是出了名的困难。 我能建议的最好的办法是,为数据层构建一个通用接口,并模拟该接口以供业务层调用数据层。

如果您尝试对数据层进行单元测试,您将陷入痛苦的境地。 我建议您跳过它并尝试在集成测试期间清除任何数据层错误。

也就是说,如果您使用 LINQ to SQL,我会遇到 这篇文章展示了如何模拟 LINQ to SQL 存储库。 它需要一些额外的工作才能使其正常工作,但它可能对您有用。

Unfortunately, unit testing the data layer is notoriously difficult. The best I can suggest is that you build a generic interface for your data layer and mock that for your business layer calls into the data layer.

If you try to unit test your data layer you're going to be in for a world of pain. I suggest you skip it and try to flush out any data layer bugs during integration testing.

That said, if you're using LINQ to SQL, I ran across this article the other day that shows how to mock a LINQ to SQL repository. It requires some extra work to get it to work but it might be useful to you.

撑一把青伞 2024-07-20 21:34:16

也许您应该研究一下模拟 - moq 是一个很好的起点。 基本上,模拟是有一个框架来模拟代码中的依赖关系以帮助测试。

Perhaps you should look into mocking - moq is a good place to start. Basically mocking is having a framework that will impersonate dependencies in your code to aid in testing.

明天过后 2024-07-20 21:34:16

我同意有关模拟的评论,但您仍然可以进行自动化集成测试来练习存储库中的方法(例如类)。

我对我使用的技术(带 MS 测试)做了非常详细的描述 此处

I agree with the comments about mocking, but you can still do automated integration testing to exercise the methods in your repository like classes.

I did a very detailed description of a technique I used (w/ MS Test) here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文