开发者

Visual Studio passing configuration values to UnitTests

开发者 https://www.devze.com 2023-02-04 13:01 出处:网络
I\'m using MSTest (Visual Studio) unit tests to run Selenium to test the functionality of a website.What I want to do is to be able to pass some configuration variables to my tests.Things like, the se

I'm using MSTest (Visual Studio) unit tests to run Selenium to test the functionality of a website. What I want to do is to be able to pass some configuration variables to my tests. Things like, the server address,开发者_StackOverflow社区 Selenium browser type...etc. I've been trying to use the TestContext, but there doesn't seem to be anyway other than using LoadTests to pass this information.

I also tried to use Spring.NET but that didn't seem to help either.

Any ideas on using TestContext? Or maybe something else.

Thanks


I thought I'd share what I ended up doing. I used Spring.net to inject the settings into a SeleniumSettings class like this;

<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" >
  <object id="Settings" type="Sample.SeleniumSettings, Sample" singleton="true">
    <property name="Server" value="localhost"/>
    <property name="Port" value="4444"/>
    <property name="Browser" value="*firefox" />
    <property name="Url" value="http://website.com"/>
    <property name="Email" value="sample@website.com"/>
  </object>
</objects>

This will inject the SeleniumSettings into a Property called Settings on the Test class. The tests need to inherit from AbstractDependencyInjectionSpringContextTests, and implement;

protected override string[] ConfigLocations 

The settings class looks like this;

public class SeleniumSettings
{
    public const string DefaultEmailAddress = "sample@website.com";
    public const string DefaultServerAddress = "localhost";
    public const string DefaultProtocol = "http://";
    public const string DefaultEndPoint = "/";

    public string Server = DefaultServerAddress;
    public int Port = 4444;
    public string Browser = "*firefox";
    public string Url = "http://localhost";
    public string Email = DefaultEmailAddress;

    public ISelenium factory()
    {
        return new DefaultSelenium(Server, Port, Browser, Url);
    }
}

Then use SeleniumSettings.factory() to get the DefaultSelenium object to run your tests with.

The Selenium documentation has some info on this but it dives in too deep too fast, and skips the basic information needed to set this stuff up.

I tried to inject the DefaultSelenium object into the class originally but I was having issues with Selenium crashing internally. It didn't seem to like being created by the Spring.net injection.

I hope this helps someone.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号