如何进行 Web 测试的模拟?

发布于 2024-10-12 18:07:05 字数 551 浏览 4 评论 0原文

我想为我的 asp.net Web 应用程序编写一些 Web 测试(通过 WatiN/Selenium + CassiniDev Web 服务器)。

我遇到的问题是我不知道在这种情况下该怎么办: 有一个页面,用户可以单击按钮来调用某些第三方服务。在我的网络测试中,我想创建此服务的模拟,它将始终返回静态值(这些测试用例中的某些值和其他测试用例中的其他值)。

我怎样才能做到这一点?

目前我使用 IoC/DI 容器 Microsoft Unity。我的页面以 中描述的方式获取他的依赖项http://msdn.microsoft.com/en-us/library/ff664622%28v=pandp.50%29.aspx

我想到的唯一解决方案是:将每个测试用例的所有依赖项都放在 web.config 中,并在测试的 SetUp 上复制必要的 web.config。这个解决方案非常痛苦!

有什么想法吗?

I want to write a few web tests (over WatiN/Selenium + CassiniDev web server) for my asp.net web application.

Problem I encountered is that I dont know what to do in such situations:
there is a page where user can click the button to call some third-party service. In my web test i want to create mock of this service, which will always return static value (some value in these test case and other value in other test case).

How can i do that?

Currently i use IoC/DI container Microsoft Unity. And my pages gets his dependencies in a manner described in http://msdn.microsoft.com/en-us/library/ff664622%28v=pandp.50%29.aspx.

The only solution that comes to my head is: place all dependencies in web.config for each test case and copy necessary web.config on SetUp of test. This solution completly painful!

Any ideas?

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

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

发布评论

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

评论(2

世界如花海般美丽 2024-10-19 18:07:05

我在集成测试中也使用 WatiN 和 Cassini-dev,并且不得不处理类似的问题。在我的安装装置中,我将 Asp.Net Web 应用程序部署到测试文件夹中的临时文件夹,这使我可以在启动 cassini-dev 之前尝试配置。我在 CI 中使用 Windsor,它允许我在配置级别更改注入的组件。您也可以使用 Unity 来实现此目的。

如果您引用的服务是 Web 服务,您只需使用您已编码的接口模拟 Web 服务即可。

以下是我运行集成测试时采取的步骤:

  1. 创建临时 Web 目录
  2. 将 Asp.Net Web 应用程序发布到临时目录(我使用 MSBuild 来执行此操作)
  3. 部署临时数据库(使用 MSbuild 和数据库项目,但可以完成多种方式)
  4. 部署临时成员资格数据库(请参阅我的 博客文章 了解如何在代码中执行此操作)
  5. 更新已部署的 Asp.Net Web 应用程序的 web.config 以指向临时数据库并更改与测试相关的任何其他设置。
  6. 使用 Cassini-Dev 启动网站。我还使用 http 请求访问该站点,以便在运行任何测试之前验证该站点是否已启动。

运行测试。

运行测试后,您应该进行清理。

  1. 停止 cassini-dev
  2. 删除临时托管文件夹
  3. 删除临时数据库。我使用 Sql 服务器 SMO 对象,它允许我查询 Sql 服务器,我用它来删除在任何先前失败的测试运行后留下的任何旧数据库。

如何使用 MSbuild 部署网站代码

var properties = new Dictionary<string, string>
{
    {"Configuration", isDebug ? "Debug" : "Release"},
    {"WebProjectOutputDir", tempHostingDirectory.FullName},
    {"DeployToDatabase", "true"},
    {"OutDir", Path.Combine(tempHostingDirectory.FullName, "bin\\")}
};

using (var engine = new ProjectCollection(properties))
{
    engine
        .LoadProject(<web project path>, "4.0")
                .Build(new[] {"Build", "ResolveReferences", "_CopyWebApplication"});
}

Unity 配置节用法: http://www.pnpguidance.net/Post/UnityContainerUnityConfigurationSectionAppConfigWebConfig.aspx< /a>

在代码中生成 asp.net 会员数据库: http: //bronumski.blogspot.com/2011/06/generating-creating-aspnet-application.html

MSDN 上的 Msbuild ProjectCollection:http://msdn.microsoft.com/en-us/library/microsoft.build.evaluation.projectcollection.aspx

I use WatiN and Cassini-dev in my integration tests as well and have had to deal with similar issues. In my setup fixture I deploy my Asp.Net web application to a temporary folder in my test folder which allows me to play around with the configuration before starting up cassini-dev. I use Windsor for my CI which allows me to change injected components at the configuration level. You may also be able to acheive this with Unity.

If the service you are referring to is a web service you just mock out a web service using the interface you have been coding to.

Here are the steps that I take when running my integration tests:

  1. Create a temp web directory
  2. Publish the Asp.Net web application to the temp directory (I use MSBuild to do this)
  3. Deploy temp database (Using MSbuild and database projects but could be done a number of ways)
  4. Deploy temp membership database (see my blog post on how to do this in code)
  5. Update the web.config of the deployed Asp.Net web application to point to the temp databases and change any other settings relevant for testing.
  6. Start up the website using Cassini-Dev. I also hit the site with a http request so that I can verify the site is up before running any tests.

Run the tests.

After running the tests you should clean up.

  1. Stop cassini-dev
  2. Delete the temp hosting folder
  3. Delete the temp databases. I use Sql server SMO objects that allow me to query the Sql Server which I use to delete up any old databases that have been left lying around after any previously failed test runs.

How to deploy a website using MSbuild in code

var properties = new Dictionary<string, string>
{
    {"Configuration", isDebug ? "Debug" : "Release"},
    {"WebProjectOutputDir", tempHostingDirectory.FullName},
    {"DeployToDatabase", "true"},
    {"OutDir", Path.Combine(tempHostingDirectory.FullName, "bin\\")}
};

using (var engine = new ProjectCollection(properties))
{
    engine
        .LoadProject(<web project path>, "4.0")
                .Build(new[] {"Build", "ResolveReferences", "_CopyWebApplication"});
}

Unity configuration section usage: http://www.pnpguidance.net/Post/UnityContainerUnityConfigurationSectionAppConfigWebConfig.aspx

Generating asp.net membership database in code: http://bronumski.blogspot.com/2011/06/generating-creating-aspnet-application.html

Msbuild ProjectCollection on MSDN: http://msdn.microsoft.com/en-us/library/microsoft.build.evaluation.projectcollection.aspx

七秒鱼° 2024-10-19 18:07:05

听起来您正在尝试模拟网络服务。
Web 服务通常继承自 MarshalByRefObject,这意味着您可以通过继承 RealProxy 来创建模拟,以创建一个假装是 Web 服务的透明代理:

class Mock : RealProxy
{
    public Mock()
        : base(typeof(IStuff)) { }

    public IStuff GetStuff()
    {
        return (IStuff)GetTransparentProxy();
    }

    public override IMessage Invoke(IMessage msg)
    {
        IMethodCallMessage message = (IMethodCallMessage)msg;

        // the message object provides the MethodInfo that was called
        // as well as the arguments.

        // <Insert logic here>

        return new ReturnMessage(new NotImplementedException("comming soon to a test near you ..."), message);
    }
}

我相信 NMock2 使用 RealProxy 进行模拟,因此您应该能够使用它来模拟而是使用网络服务。

It sounds like you are trying to mock a web service.
Web services usually inherit from MarshalByRefObject, this means you can create a mock by inheriting from RealProxy to create a transparent proxy that pretends to be the webservice:

class Mock : RealProxy
{
    public Mock()
        : base(typeof(IStuff)) { }

    public IStuff GetStuff()
    {
        return (IStuff)GetTransparentProxy();
    }

    public override IMessage Invoke(IMessage msg)
    {
        IMethodCallMessage message = (IMethodCallMessage)msg;

        // the message object provides the MethodInfo that was called
        // as well as the arguments.

        // <Insert logic here>

        return new ReturnMessage(new NotImplementedException("comming soon to a test near you ..."), message);
    }
}

I belieave NMock2 uses RealProxy for it's mocks, so you should be able to use it to mock the web service instead.

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