如何使用Selenium运行我的NUnit测试用例以针对不同的环境运行?

Duk*_*Duk 1 selenium nunit unit-testing

我使用Selenium编写了NUnit测试用例来测试Web应用程序.我想针对不同的环境(例如QA,分期和生产)运行相同的测试用例.最简单的方法是什么?

Arr*_*ran 5

NUnit支持参数​​化测试夹具以及参数化测试.所以第一件事就是你想要针对不同的环境运行特定的测试,还是整个测试夹具都将在两种环境中重新运行?

我问,因为这个答案决定了你将传递参数(环境)的位置.如果您只是想重新运行整个测试夹具,则应该在测试夹具级别传递环境,即创建参数化测试夹具.如果您只想针对这些环境运行特定测试,则必须将其传递给每个单独的测试用例.下面是一个例子,我是如何做同样的事情:

首先创建一种方法来定义测试可以"附加"的"环境".我建议也许把它推到app.config并有一个'Settings'类和一个enum来配合它:

public enum Environment
{
    QA,
    Production,
    Hotfix,
    Development
}

public class Settings
{   
    public static string QAUrl { get { return "some url"; } }

    public static string ProductionUrl { get { return "some url"; } }

    public static string HotfixUrl { get { return "some url"; } }

    public static string DevUrl { get { return "some url"; } }
}
Run Code Online (Sandbox Code Playgroud)

上面的"一些网址"将从您的配置文件中读取或硬编码,但是您可以.

我们现在有一个环境的概念,它的URL,但它们没有链接在一起或以任何方式相关.理想情况下,您希望为其提供枚举的"QA"值,然后它会为您整理URL.

接下来创建一个基础测试夹具,所有测试夹具都可以继承,从而保持当前环境.我们还创建了一个Dictionary现在将环境值与其URL相关联的信息:

public class BaseTestFixture
{
    private Dictionary<Environment, string> PossibleEnvironments 
    {
        get
        {
            return new Dictionary<Environment, string>()
            {
                { Environment.QA, Settings.QAUrl },
                { Environment.Production, Settings.ProductionUrl },
                { Environment.Hotfix, Settings.HotfixUrl },
                { Environment.Development, Settings.DevelopmentUrl },
            }
        }
    }

    private Environment CurrentEnvironment { get; set; }

    public BaseTestFixture(Environment environment)
    {
        CurrentEnvironment = environment;
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用Reflection来确定哪些URL映射到什么enum值.

太酷了,我们有一个可以反对的环境.要作为管理员登录您网站的示例测试:

public class LoginToSite
{
    [Test]
    public void CanAdministratorSeeAdministratorMenu()
    {
        // go to the site
        driver.Navigate().GoToUrl("production site");
        // login as administrator
    }
}
Run Code Online (Sandbox Code Playgroud)

我们如何让它转到特定的URL?让我们稍微修改我们的基类......

public class BaseTestFixture
{
    private Dictionary<Environment, string> PossibleEnvironments 
    {
        get
        {
            return new Dictionary<Environment, string>()
            {
                { Environment.QA, Settings.QAUrl },
                { Environment.Production, Settings.ProductionUrl },
                { Environment.Hotfix, Settings.HotfixUrl },
                { Environment.Development, Settings.DevelopmentUrl },
            }
        }
    }

    private Environment CurrentEnvironment { get; set; }

    protected string CurrentEnvironmentURL 
    { 
        get 
        { 
            string url;
            if (PossibleEnviroments.TryGetValue(CurrentEnviroment, out url))
            {
                return url;
            }

            throw new InvalidOperationException(string.Format("The current environment ({0}) is not valid or does not have a mapped URL!", CurrentEnviroment));
        }
    }

    public BaseTestFixture(Environment environment)
    {
        CurrentEnvironment = environment;
    }

            public BaseTestFixture()
            {
            }
}
Run Code Online (Sandbox Code Playgroud)

我们的基类现在可以告诉我们,根据我们所处的环境,去哪个页面...

所以我们现在有了这个测试,继承自我们的基础:

public class LoginToSite : BaseTestFixture
{
    [Test]
    public void CanAdministratorSeeAdministratorMenu()
    {
        // go to the site
        driver.Navigate().GoToUrl(CurrentEnvironmentURL);
        // login as administrator
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这很好,但上面不会编译......为什么?我们实际上并没有给它一个环境,所以我们必须通过一个......

[TestFixture(Environment.QA)]
public class LoginToSite : BaseTestFixture
{
    [Test]
    public void CanAdministratorSeeAdministratorMenu()
    {
        // go to the site
        driver.Navigate().GoToUrl(CurrentEnvironmentURL);
        // login as administrator
    }
}
Run Code Online (Sandbox Code Playgroud)

这很好,它现在已经传入了环境,URL等的检查现在都在后台完成了......但是这仍然无法编译.由于我们使用的是继承,因此我们必须有一个构造函数来为我们传递它:

    public LoginToSite(Environment currentEnvironment)
    {
        CurrentEnvironment = currentEnvironment;
    }
Run Code Online (Sandbox Code Playgroud)

Etvoilà.

至于具体的测试用例,从早些时候开始我们的测试用例就更容易了:

public class LoginToSite
{
    [TestCase(Environment.QA)]
    public void CanAdministratorSeeAdministratorMenu(Environment environment)
    {
        // go to the site
        driver.Navigate().GoToUrl("production site");
        // login as administrator
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个会将环境传递到特定的测试用例中.然后你需要一个新的Settings类,为你做环境检查(以与我之前类似的方式):

public class EnvironmentHelper
{
    private static Dictionary<Environment, string> PossibleEnvironments 
    {
        get
        {
            return new Dictionary<Environment, string>()
            {
                { Environment.QA, Settings.QAUrl },
                { Environment.Production, Settings.ProductionUrl },
                { Environment.Hotfix, Settings.HotfixUrl },
                { Environment.Development, Settings.DevelopmentUrl },
            }
        }
    }

    public static string GetURL(Environment environment)
    {
        string url;
        if (PossibleEnviroments.TryGetValue(environment, out url))
        {
            return url;
        }

        throw new InvalidOperationException(string.Format("The current environment ({0}) is not valid or does not have a mapped URL!", environment));
    }
}
Run Code Online (Sandbox Code Playgroud)