简体   繁体   English

NUnit是硒测试的错误选择吗?

[英]is NUnit bad choice for Selenium test?

I have read umpteen answers on SO while searching for NUnit + dependent methods + order of test execution. 在搜索NUnit +依赖方法+测试执行顺序时,我已经阅读了很多关于SO的答案。 Every single answer suggests that forcing any set of order for unit tests is extremely evil. 每个答案都表明,强制对单元测试执行任何命令集都是极其邪恶的。

I am writing Selenium tests using NUnit. 我正在使用NUnit编写Selenium测试。 So I am trying to write integration tests using Unit testing framework!!! 因此,我正在尝试使用单元测试框架编写集成测试!!!

To cite an example of integration tests (and this is just one example). 举一个集成测试的例子(这只是一个例子)。 I need to create a valid account before proceeding with other tests. 在进行其他测试之前,我需要创建一个有效的帐户。 If creation of account fails then I would like to abort entire test execution. 如果帐户创建失败,那么我想中止整个测试执行。

Since I don't want to rely on alphabetic order of test and in true spirit of NUnit, decided to create an account before any further test. 由于我不想依赖于测试的字母顺序,并且本着NUnit的实质,因此决定在进行进一步测试之前创建一个帐户。 Though it does not look right to me for two core reasons - 尽管由于两个核心原因,它在我看来并不正确-

  1. Unnecessary code duplication/execution 不必要的代码重复/执行
  2. What if application account creation is not working, all my tests would still try to create and account again and again and failing 如果无法创建应用程序帐户,怎么办,我所有的测试仍会尝试创建并一次又一次地帐户化而失败

I am inclined to think that NUnit may be not be right deal with Selenium tests. 我倾向于认为NUnit可能不适用于硒测试。 But if not Nunit then what should I use? 但是,如果不是Nunit,那我应该怎么用?

Selenium Core itself comes with a TestRunner that is written in Javascript and you can run your tests directly from the browser. Selenium Core本身带有用Javascript编写的TestRunner,您可以直接从浏览器运行测试。

For more see: 有关更多信息,请参见:

http://www.developerfusion.com/article/84484/light-up-your-development-with-selenium-tests/ http://www.developerfusion.com/article/84484/light-up-your-development-with-selenium-tests/

Apart from that, using Nunit and tests written in C# are much more easier to write and maintain. 除此之外,使用Nunit和用C#编写的测试更加容易编写和维护。 Are you using SetUp and TearDown while writing your tests? 您在编写测试时是否正在使用SetUp和TearDown? That way you can avoid code duplication. 这样,您可以避免代码重复。

Regarding you second point, you can have a flag that is set on first setup failure and skips the setup the next time or the setup itself tracking it and quickly failing the next time. 关于第二点,您可以设置一个标志,该标志在第一次设置失败时设置并在下一次跳过设置,或者设置本身跟踪它并在下次失败时快速失败。 And tests don't run if setup fails in Nunit. 如果在Nunit中安装失败,则测试不会运行。

I run Selenium with NUnit all the time. 我一直在用NUnit运行Selenium。 It just depends on how you write your tests. 这仅取决于您如何编写测试。 To avoid code duplication, I make a library of helper functions that do common things, like log in or log out of my site, that the other tests use to get to the page they need to test. 为了避免代码重复,我建立了一个帮助程序函数库,这些函数执行一些常见的事情,例如登录或注销我的站点,其他测试则使用这些函数来访问需要测试的页面。 (I use the term 'library' in a loose sense; I don't actually split them into their own C# project.) (我在广义上使用术语“库”;实际上,我没有将它们拆分为自己的C#项目。)

You are right that if the account creation function is broken, the other tests will fail. 没错,如果帐户创建功能被破坏,其他测试将失败。 But personally, I don't see that as a problem, as the point of unit tests is to make sure that your changes didn't have unintended effects elsewhere in your project. 但就我个人而言,我认为这不是问题,因为单元测试的目的是确保所做的更改不会在项目的其他地方产生意外的影响。 If the account creation broke, clearly that affects a lot of things. 如果帐户创建失败,显然会影响很多事情。 Ditto if my login helper method fails: if you can't log in, you can't get to anything in the site. 如果我的登录帮助程序方法失败,则同上:如果无法登录,则无法访问该站点中的任何内容。 Effectively, the whole site is broken. 实际上,整个站点都已损坏。

If you need to create new accounts on each test then the approach that I would take is to have that code moved into your SetUp code. 如果您需要在每次测试中创建新帐户,那么我采用的方法是将该代码移至您的SetUp代码中。 If some of your tests don't require login, split them out into different files. 如果您的某些测试不需要登录,请将其拆分为不同的文件。

Any bits of duplcation should be removed, test code should be as clean and robust as production code. 应当删除任何重复的内容,测试代码应与生产代码一样干净和健壮。 Splitting the files with different tests help maintain the idea of Single Responsibility . 使用不同的测试拆分文件有助于维护“ 单一职责”的思想。

Did you also look at PNunit? 您也看过PNunit吗?

See one of the anwers in this question: 请参阅此问题中的答案之一:

Has anyone found a way to run C# Selenium RC tests in parallel? 有没有人找到并行运行C#Selenium RC测试的方法?

I'm still not 100% sure how TestNG would work with grid, suppose you have a 3-step registration process and you divide this up in 3 tests. 我仍然不确定100%TestNG如何与网格一起工作,假设您有一个三步注册过程,并将其分为3个测试。 Is TestNG with grid going to help you here? 带网格的TestNG会在这里帮助您吗? I suppose not, or will it detect that test C needs to have test A and B runned on the same thread? 我想不是,还是会检测到测试C需要在同一线程上运行测试A和B?

PNunit looks like it could provide a way to distribute dependent tests to the same machine. PNunit看起来可以提供一种将依赖测试分发到同一台计算机的方法。 Although it's probably quite complicated to set up. 尽管设置可能很复杂。

Two approaches might help you, with the problem you describe as an answer to AutomatedTester: 有两种方法可以帮助您解决您描述为AutomatedTester的答案的问题:

First , NUnit 2.4.4 defines a SuiteAttribute that lets you run tests in the order you want. 首先 ,NUnit 2.4.4定义了SuiteAttribute ,可让您以所需的顺序运行测试。 Very handy but it has a major restriction: it is not compatible with TestCaseAttribute . 非常方便,但有一个主要限制:与TestCaseAttribute不兼容。 That means all your tests have to be triggered only by TestAttribute ; 这意味着您所有的测试必须仅由TestAttribute触发; which is very annoying if you target coverage of value-based boundary tests (thus several data-driven test cases). 如果您将目标放在基于值的边界测试(因此有几个数据驱动的测试用例)上,这将非常烦人。 More info on http://www.nunit.org/index.php?p=suite&r=2.5.10 有关更多信息, 请访问http://www.nunit.org/index.php?p=suite&r=2.5.10

Another approach is to prepare an integration sample database tailored just for your test cases. 另一种方法是准备一个专门为您的测试案例量身定制的集成样本数据库。 Say you have a 15-steps registration process: create a student record and push it to step one, then another student and push it to step two, and so on. 假设您有15个步骤的注册过程:创建一个学生记录并将其推送到第一步,然后将另一个学生并推送到第二步,依此类推。 Save your database and restore it as test fixture setup. 保存数据库,并将其还原为测试夹具设置。 Then test each step with a different student. 然后与其他学生测试每个步骤。

It is perfectly valid in most cases to do integration tests on different records for each step, as it provides the same functionwise and codewise coverage, and it follows the idea of integration testing because your records in the DB are true records (created by the UI with all flaws that comes with the UI). 在大多数情况下,对每个步骤的不同记录进行集成测试是完全有效的,因为它提供了相同的功能和代码覆盖,并且遵循了集成测试的思想,因为您在数据库中的记录是真实记录(由UI创建)以及用户界面附带的所有缺陷)。

Of course it needs more time to run and storage space because of the DB copies you'll have to store. 当然,由于您必须存储数据库副本,因此需要更多时间来运行和存储空间。 If your system can't afford that, then you'll probably want to look at the first solution. 如果您的系统负担不起,那么您可能需要考虑第一个解决方案。

It also gives you the advantage of being able to spot bugs on later steps even if earlier steps are unstable: all tests are run on each test campaign which is not the case in the solution you ask for. 即使先前的步骤不稳定,它也使您能够在以后的步骤中发现错误:所有测试都在每个测试活动上运行,而您所要求的解决方案中就没有这种情况。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM