简体   繁体   English

在多个环境中测试相同的测试用例

[英]Testing same test cases across multiple environments

Currently we have set up our UI test automation in one environment(Dev).目前我们已经在一个环境(Dev)中设置了我们的 UI 测试自动化。 We are using C#, .Net, Visual Studio, specflow and MSTest我们正在使用 C#、.Net、Visual Studio、specflow 和 MSTest

CONFIG配置

We read an app.config file for environment specific variables.我们读取了一个app.config文件以获取特定于环境的变量。 We use Azure Pipeline for CI and run these tests on a nightly build.我们使用 Azure 管道进行 CI 并在每晚构建中运行这些测试。

<configuration>
    <appSettings>
        <add key="DevApp" value="https://dev.websitename.com />
    </appSettings>
    <connectionStrings>
        <add name="DevDatabase" connectionString="http://dev.url/" />
    </connectionStrings>
</configuration>

Now we want to run these tests on our UAT environment as well.现在我们也想在我们的 UAT 环境中运行这些测试。 The setup will be the same, and we want to run the same tests with the same data.设置将是相同的,我们希望使用相同的数据运行相同的测试。 The only difference is that for UAT we will point to different URL's and a different database.唯一的区别是对于 UAT,我们将指向不同的 URL 和不同的数据库。

for example例如

Dev env = https://dev.websitename.com
UAT env = https://uat.websitename.com

server name="DevDatabase" connectionString="http://dev.url/"
server name="UATDatabase" connectionString="http://uat.url/"

PASSWORDS密码

In terms of password, out application is an internal application and we use windows auth.在密码方面,out 应用程序是一个内部应用程序,我们使用 windows auth。 So in our dev and uat environment we have the same password set up for all users.因此,在我们的 dev 和 uat 环境中,我们为所有用户设置了相同的密码。 So in Dev = devpassword and UAT = uatpassword所以在 Dev = devpassword 和 UAT = uatpassword

For both dev and test we are using the same users with the password being the only difference.When testing we launch the browser using impersonation and launch the browser as 'run as' for that user对于开发和测试,我们使用相同的用户,密码是唯一的区别。在测试时,我们使用模拟启动浏览器,并以该用户的“运行身份”启动浏览器

var service = ChromeDriverService.CreateDefaultService(driverpath)

if user is not null then we do this如果用户不是 null 那么我们这样做

var pwd = new SecureString()

service.StartDomain = Configurationhelper.Domain
service.StartupUserName = username
service.StartupPassword= = pwd
service.StartupLoadUserProfile = true

we store domain and password and other environmental variables in a separate config file as constants.我们将域和密码以及其他环境变量作为常量存储在单独的配置文件中。

**Main issue: ** This wont work now so I think it could be best to store passwords as secrets in AZURE pipeline variables? **主要问题:** 这现在不起作用,所以我认为最好将密码作为机密存储在 AZURE 管道变量中? if so, how would i change this code?如果是这样,我将如何更改此代码? for example例如

The server team, db team and devops team have taken care of server,db setup and urls etc服务器团队、数据库团队和 devops 团队负责服务器、数据库设置和 url 等

So for me its just configuring the test automation repo with my changes to configuration所以对我来说,它只是用我对配置的更改来配置测试自动化回购

What could be an elegant approach to do this?有什么优雅的方法可以做到这一点?

AZURE PIPELINE AZURE 管道

How could we run tests for both these environments in parallel?我们如何同时为这两个环境运行测试? by parallel i mean having both run on a nightly run.并行我的意思是让两者都在夜间运行。 Our Azure pipeline has 2 separate clients UAT and DEV pointing to the same artifact.我们的 Azure 管道有 2 个独立的客户端 UAT 和 DEV 指向同一个工件。 The tasks and Variable are the same for both environments but with different values obviously两种环境的任务和变量是相同的,但显然具有不同的值

Currently they both would run in isolation目前他们都将孤立运行

Solutions to this problem come down to how does the context (in your case the environment and all its associated connection strings and URLs) get to the tests where they will be consumed.此问题的解决方案归结为上下文(在您的情况下是环境及其所有关联的连接字符串和 URL)如何到达将要使用它们的测试。 In your question, you stated several orthogonal concerns:在您的问题中,您陈述了几个正交问题:

  • using the same data使用相同的数据
  • running in a different environment在不同的环境中运行
  • running in parallel并行运行

Not mentioned is another concern未提及是另一个问题

  • how to handle secrets (eg passwords in connection strings)如何处理机密(例如连接字符串中的密码)

I'll explain one solution (a strategy really) that addresses these concerns, and why it appears to be a maintainable and extensible solution.我将解释一个解决这些问题的解决方案(实际上是一种策略),以及为什么它看起来是一个可维护和可扩展的解决方案。

Using the same data使用相同的数据

This can be very simple or very complex.这可以非常简单或非常复杂。 The simple solution is to create a database of canonical and representative test data, and to then swap in that database to your environment.简单的解决方案是创建一个包含规范和代表性测试数据的数据库,然后将该数据库交换到您的环境中。 That can be done via your database's backup/restore or by creating the data programmatically.这可以通过数据库的备份/恢复或以编程方式创建数据来完成。 You would need to have a mechanism to restore or wipe the data whether the test(s) succeeds or fails.无论测试成功还是失败,您都需要有一种机制来恢复或擦除数据。

Very rarely will using the environment's database "as is" lead to reliable tests; “按原样”使用环境的数据库很少会导致可靠的测试; tests often modify state, and a database is the ultimate form of state;测试经常修改state,数据库是state的终极形式; mutations of state will affect future tests. state 的突变将影响未来的测试。

It is with this last sentence that a full swap that occurs before/after each test is probably a) faster (occurring at a bulk/macro level with a quicker swap function), b) more maintainable (data can be reviewed/created ahead of time) and c) less brittle.正是在最后一句话中,每次测试之前/之后发生的完全交换可能a)更快(发生在具有更快交换功能的批量/宏级别),b)更易于维护(可以在之前查看/创建数据时间)和c)不那么脆。

Running in a different environment在不同的环境中运行

This, like the heart of your question discusses, is where you come down to whether to use multiple files or a single file.就像您讨论的问题的核心一样,这就是您归结为使用多个文件还是单个文件的地方。 Using multiple files means you can take advantage of some of the built-in .NET configuration mechanisms that allow you to specify the environment.使用多个文件意味着您可以利用一些允许您指定环境的内置 .NET 配置机制。 That means duplicating the files and changing the values to reflect the environment.这意味着复制文件并更改值以反映环境。

The other way, you mentioned, is storing all of this information into a single configuration file.您提到的另一种方法是将所有这些信息存储到一个配置文件中。 If you do it this way, you need some sort of discriminator to disambiguate the entries, and your test needs to be able to pass in the environment name to some API to pull the value.如果这样做,则需要某种鉴别器来消除条目的歧义,并且您的测试需要能够将环境名称传递给某些 API 以提取值。 I prefer this mechanism personally, because when you add a new feature/test you can add all the configuration in one place.我个人更喜欢这种机制,因为当您添加新功能/测试时,您可以将所有配置添加到一个地方。

So the two mechanisms are roughly the same in terms of work, except the latter leads to a more compact editing session when adding new config, and from a readability/maintainability scenario, you have fewer places to look.所以这两种机制在工作方面大致相同,除了后者在添加新配置时导致更紧凑的编辑 session,并且从可读性/可维护性场景来看,您可以查看的地方更少。

Secrets秘密

If you follow the single-source of configuration approach, you simply extend that to your secrets, but you select the appropriate secret store (eg a secrets file or Azure Key Vault, or some such... again with an environment-based discriminator).如果您遵循单一来源的配置方法,您只需将其扩展到您的秘密,但您 select 适当的秘密存储(例如秘密文件或 Azure 密钥库,或一些这样的......再次使用基于环境的鉴别器) . Here's an example:这是一个例子:

{
   "DEV.Some.Key" : "http://devhost/some/path",
   "UAT.Some.Key" : "https://uathost/some/other/path"
   ...
}

Using a discriminator means far less changes to your DevOps pipeline, which is, from a developer/editing experience, most likely slower and more cumbersome than editing a file or key vault.使用鉴别器意味着对 DevOps 管道的更改要少得多,也就是说,从开发人员/编辑经验来看,这很可能比编辑文件或密钥保管库更慢、更麻烦。

Running in parallel并行运行

While you could rotate out the context and design your solution to run in parallel using the MSTest mechanisms, it would be more elegant to allocate this to your pipeline itself, and have enough resources to be able to run these pipelines in parallel by having enough build agents and so on.虽然您可以轮换上下文并设计您的解决方案以使用 MSTest 机制并行运行,但将其分配给您的管道本身会更优雅,并有足够的资源通过足够的构建来并行运行这些管道代理等。

Conclusion结论

It comes down to which parts of the solution should be addressed by which resources.它归结为解决方案的哪些部分应该由哪些资源来解决。 The solution above allocates environmental selection and test execution into the pipeline itself.上面的解决方案将环境选择和测试执行分配给管道本身。 Granular values such as connection strings and secrets are allocated to a single source to reduce the friction that occurs when having to edit these values.诸如连接字符串和秘密之类的粒度值被分配给单个源,以减少在必须编辑这些值时发生的摩擦。

Following this strategy might better leverage your team's skills as well.遵循此策略也可能更好地利用您团队的技能。 Those with a DevOps mindset can most likely spin up new environments and parallelize more readily than a Developer mindset, who would be more aware of what data needs to be setup and how to craft the tests.具有 DevOps 思维方式的人很可能比开发人员思维方式更容易启动新环境并进行并行化,开发人员思维方式更清楚需要设置哪些数据以及如何制作测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium WebDriver 在测试用例之间共享同一个会话 - Selenium WebDriver to share same session across test cases CodedUI测试 - 无法在sharepoint中跨环境重用测试 - CodedUI Test - cannot re-use test across environments in sharepoint 将多个测试用例链接到一个测试用例 - link multiple test cases to one test case Visual Studio C#单元测试 - 使用各种/多个测试初始化​​运行单元测试,多次运行相同的单元测试? - Visual Studio C# unit testing - Run Unit test with varied/multiple test initializations, Run same unit test multiple times? 在一个测试方法中,ExpectedException是否用于测试多个案例? - Is ExpectedException used for testing more than one cases in one test method? C# 测试框架循环测试用例 - C# Testing Framework Loop Through Test Cases 建议部署到多个Azure环境(开发,测试,生产)的方法是什么? - What is the recommended approach for deploying to multiple Azure environments (dev, test, production)? 具有相同别名的 switch 语句中的多个案例 - Multiple cases in switch statement with same alias 跨多个云驱动程序运行测试 - Running a test across multiple Cloud Drivers 单元测试 - 相同的方法但不同的测试数据 - Unit testing - same method but for different test data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM