2017-05-22 184 views
0

这几乎是我开始进行自动化测试的原因,所以如果我不能让你清楚,我想道歉!使用Selenium和NUnit进行UI自动化测试测试执行顺序

因此,一两个星期阅读自动化的博客后,我决定用NUNIT硒的webdriver的UI自动化。

我的应用是在开发3年后留存的企业级应用。这是一个项目和投资组合管理系统。

我有数百页,其中约50%执行CRUD操作。

,我决定为我的应用程序如下strucutre:

Project structure

我使用的测试数据为JSON在下面的格式和这个数据retrive到我的视图模型:

[ 
    { 
    "Name": "Finance", 
    "Description": "division with complete test data", 
    "Color": "#ff0000", 
    "ExpectedStatus": { 
     "WillBeAdded": true, 
     "WillBeDeleted": true, 
     "WillBeUpdated": true 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": false, 
     "ShouldUpdate": true 
    } 
    }, 
    { 
    "Name": "IT", 
    "Description": "IT projects", 
    "Color": "pink", 
    "ExpectedStatus": { 
     "WillBeAdded": true, 
     "WillBeDeleted": true, 
     "WillBeUpdated": true 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": false, 
     "ShouldUpdate": true 
    } 
    }, 
    { 
    "Name": "Business", 
    "Description": "division with name and color name", 
    "Color": "yellow", 
    "ExpectedStatus": { 
     "WillBeAdded": true, 
     "WillBeDeleted": true, 
     "WillBeUpdated": true 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": false, 
     "ShouldUpdate": true 
    } 
    }, 
    { 
    "Name": "", 
    "Description": "division without name and color name, should add white color", 
    "Color": "", 
    "ExpectedStatus": { 
     "WillBeAdded": true, 
     "WillBeDeleted": true, 
     "WillBeUpdated": true 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": true, 
     "ShouldUpdate": true 
    } 
    }, 
    { 
    "Name": "", 
    "Description": "without name and color name and will not be added", 
    "Color": "black", 
    "ExpectedStatus": { 
     "WillBeAdded": false, 
     "WillBeDeleted": false, 
     "WillBeUpdated": false 
    }, 
    "PerformableActions": { 
     "ShouldAdd": true, 
     "ShouldDelete": false, 
     "ShouldUpdate": false 
    } 
    } 
] 

我这里使用两件事:

1. PerformableActions ie ie what我可以通过使用这个测试用例数据来执行操作。例如,shouldAdd表示应该为添加记录执行此TestCase,shouldDelete表示此TestCase是否应该运行以删除记录,同样应该运行UpdateUpdate。

2. ExpectedStatus即测试用例的预期结果是什么 WillBeAdded意思是将这个数据添加到网格中。同样的WillBeDeleted和WillBeUpdated工作。

问题:

1)我使用相同的测试数据为所有CUD操作,这样做的原因是,我要保持我的应用程序的CUD流。由于只需要测试用户界面,所以我只是假设创建,检索和删除将按顺序工作。

这是在CRUD操作情况下进行测试的正确方法吗?

2)想我要在我的项目运行所有测试,也可能是数百或数千,我将如何维持秩序,因为我能看到的唯一可能的方法是使用订购属性, NUNit。我该如何使用它,或者是否有任何替代方法来单独测试独立模块组和独立模块组?

回答

1
  1. 您的方法没有任何问题。您可以为每个案例创建一个函数,并按照您希望的顺序制作C,U和D.也许你需要在它们之间添加wait,让后端执行它的企业内容。
  2. NUnit的OrderAttribute工作得很好,即使有更好的方法。您还可以创建测试用例列表并按顺序依次运行它们。为什么不?不要将自己限制在类和方法上。
  3. 我知道,你用C#标记了你的问题,但是对于这么多的工作,或许值得去看看一些其他的库,而不是你现在拥有的。如果你不害怕很少学习,你可以享受F#:

    • Expecto你以何种顺序和测试运行
    • Canopy您对硒的一个很好的DSL完全控制。举个小例子,这里是starter kit。你甚至可以在docker为Chrome浏览器无头模式
    • 运行它,你甚至可以得到智能感知您的JSON测试定义与Json Type Provider

UPDATE:为命令你可以定义自己的属性,甚至使用NUnit中的现有属性,并以正确的顺序调用每个反射的方法。或者你定义自己的DSL进行测试并遍历列表并提取你需要的信息。

public struct CRUD 
{ 
    public bool C; 
    public bool R; 
    public bool U; 
    public bool D; 
    public CRUD(bool c, bool r, bool u, bool d) 
    { 
     this.C = c; 
     this.R = r; 
     this.U = u; 
     this.D = d; 
    } 
} 
public enum ActionKind 
{ 
    Create, 
    Read, 
    Update, 
    Delete 
} 
public struct TestResult 
{ 
    public ActionKind Action; 
    public bool IsPerformed; 
} 
public class TestCase 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Color { get; set; } 
    public CRUD Action { get; set; } 
    public List<TestResult> Actual { get; set; } 
    public List<TestResult> Expected { get; set; } // or List<TestResult> 

} 
public class Tests 
{ 
    public void TestAListOfCases() 
    { 
     var cases = new List<TestCase> 
     { 
      new TestCase { Name = "Finance", Description = "division with complete test data", Color = "#ff0000", 
       Expected = new List<TestResult> {new TestResult {Action = ActionKind.Create, IsPerformed = true }, 
               new TestResult {Action = ActionKind.Update, IsPerformed = false}, 
               new TestResult {Action = ActionKind.Delete, IsPerformed = true } 
       }}, 
      new TestCase { Name = "IT", Description = "Description"} 
     }; 

     cases 
      .Where(x => x.Name == "IT") 
      .Where(x => x.Color != "pink") 
      .Select(RunTestCase) 
      .Where(x => !x.Item2) 
      .Select(x => OutputFailedTestCase(x.Item1)); // boah c# is verbose 
    } 
    public Tuple<TestCase, bool> RunTestCase(TestCase testCase) 
    { 
     foreach(var exp in testCase.Expected) 
     { 
      switch (exp.Action) { 
       case ActionKind.Delete: 
        // do delete if needed and create actual result 
        var actual = exp.IsPerformed 
         ? new TestResult { Action = exp.Action, IsPerformed = true } 
         : new TestResult { Action = exp.Action, IsPerformed = false }; 
        break; 

      } 
     } 
     var isFailed = 
      Enumerable.Zip(
       testCase.Actual, 
       testCase.Expected, 
       (expected, actual) => expected.Action == actual.Action && expected.IsPerformed == actual.IsPerformed) 
      .All(x=>x); 
     // your selenium stuff 
     return Tuple.Create(testCase, isFailed); 
    } 
    public bool OutputFailedTestCase(TestCase testCase) 
    { 
     // write to console or do something else 
     Console.Write($"{testCase.Name} is failed to perform following actions: {calculateFailedActions}"); 
     return true; 
    } 
} 
+0

感谢至少有人看着我的问题。非常感谢你!主要问题仍然是订购。我现在有超过20个CUD实体,如果我使用订单,我必须遵循1,2,3,4,当我订购超过9个订单时,列表更新为1,12,13,2,3 ,4等等。意味着它按字母顺序排列。我被困住了,现在发现它非常厌恶硒元素的使用。你能否给我提供一些演示,如果我的测试数量在几百,我如何分类我的测试,我需要用我的愿望来执行它们。我甚至可以使用包装来做到这一点。我处于可以接受任何事情的地步。 – Umar