让 Microsoft 测试框架使用 Spring.Net 创建测试类

发布于 2024-10-17 02:29:40 字数 1701 浏览 9 评论 0原文

我有一个使用.Net 4.0、VS 2010 和 Spring 1.3 的项目。在每个集成测试运行之前和之后,我希望能够设置和清理测试的一些数据。我正在考虑使用自定义属性来做到这一点。

[TestClass]
public class UnitTest1
{
    [TestMethod]
    [PreTestSqlExecute(SqlScript="SetUpDataForMethod1.sql")]
    public virtual void TestMethod1()
   {
   .

为此,我创建了自定义属性和周围方法拦截器

public class PreTestSqlExecuteAdvice : IMethodInterceptor
{
    public object Invoke(IMethodInvocation invocation)
    {
    .

基本上,现在的问题是如何让 Microsoft 中的测试框架使用 Spring 创建“UnitTest1”类,而不是直接创建具体类。例如,我可以使用具有适当配置的虚拟控制台应用程序来执行类似这样的操作,

    static void Main(string[] args)
    {
        IApplicationContext ctx = ContextRegistry.GetContext();
        var ut = (UnitTest1)ctx["mySqlTest"];
        ut.TestMethod1();

效果很好。但是,当我直接使用 Visual Studio 运行测试并单击“在解决方案中运行所有测试”时,它不会调用拦截器并执行前后代码,只是直接进入测试方法。

我尝试在配置中使用 AttributeAutoProxyCreator ,如下所示,

<object type="Spring.Aop.Framework.AutoProxy.AttributeAutoProxyCreator, Spring.Aop">
  <property name="AttributeTypes" value="SpringTests.ClassInstanceAttribute"/>
  <!-- Interceptor names can be either of the type IAdvice, IAdvisor, or IAdvisors -->
  <property name="InterceptorNames" value="aroundAdvisor"/>
</object>

在类上使用适当的属性

[TestClass]
[ClassInstance]
public class UnitTest1
{

或从 AbstractDependencyInjectionSpringContextTests 继承,

public class UnitTest1 : AbstractDependencyInjectionSpringContextTests

但这些似乎都不起作用。我的方法拦截器没有被调用。那么我如何让 Visual Studio 中的 Microsoft 测试框架使用 spring 上下文来创建测试类。或者,让 Spring 拦截这些测试类的具体构造并注入 Proxy 类。

预先感谢您的帮助。

I have a project using .Net 4.0, VS 2010 and Spring 1.3. Before and after each of my Integration tests run, I want to be able setup and clean up some data for the test. I was thinking of using custom attributes to do this.

[TestClass]
public class UnitTest1
{
    [TestMethod]
    [PreTestSqlExecute(SqlScript="SetUpDataForMethod1.sql")]
    public virtual void TestMethod1()
   {
   .

To do this, I have created the custom attribute and the Around Method Interceptor

public class PreTestSqlExecuteAdvice : IMethodInterceptor
{
    public object Invoke(IMethodInvocation invocation)
    {
    .

Basically, the problem now is how to get the Test framework in Microsoft to use Spring to create the "UnitTest1" class rather than creating a concrete class directly. For example, I can use a dummy console app with appropriate configuration to do something like this

    static void Main(string[] args)
    {
        IApplicationContext ctx = ContextRegistry.GetContext();
        var ut = (UnitTest1)ctx["mySqlTest"];
        ut.TestMethod1();

This works fine. But when I run the tests directly using visual studio and clicking "Run All Tests In Solution" for example, it does not call the interceptor and execute the before and after code, just goes directly to the test method.

I have tried using AttributeAutoProxyCreator in the config as shown below

<object type="Spring.Aop.Framework.AutoProxy.AttributeAutoProxyCreator, Spring.Aop">
  <property name="AttributeTypes" value="SpringTests.ClassInstanceAttribute"/>
  <!-- Interceptor names can be either of the type IAdvice, IAdvisor, or IAdvisors -->
  <property name="InterceptorNames" value="aroundAdvisor"/>
</object>

with the appropriate attribute on the class

[TestClass]
[ClassInstance]
public class UnitTest1
{

or inheriting from the AbstractDependencyInjectionSpringContextTests

public class UnitTest1 : AbstractDependencyInjectionSpringContextTests

But none of these seems to work. My method interceptor is not being called. So how do I get the Microsoft test framework in Visual Studio to either use spring context to create the Test Classes. Or alternately, get Spring to intercept the concrete constructions of these test classes and inject the Proxy class instead.

Thanks in advance for the help.

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

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

发布评论

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

评论(1

魂归处 2024-10-24 02:29:40

找到了我自己的解决方案,详见此处 - http://www.chaitanyaonline.net/2011/09/25/improving-integration-tests-in-net-by-using-attributes-to-execute-sql-scripts /

基本上我从“ContextBoundObjects”继承了我的单元测试,这让我可以注入我自己的代码并执行类似 AOP 的操作。我创建了自己的自定义属性,以便可以运行预处理和后处理 Sql 脚本。

Found my own solution, as detailed here - http://www.chaitanyaonline.net/2011/09/25/improving-integration-tests-in-net-by-using-attributes-to-execute-sql-scripts/

Basically I inherit my unit tests from "ContextBoundObjects", this lets me inject my own code and do AOP like stuff. I created my own custom attributes so that I can run the pre and post processing Sql scripts.

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