2011-08-25 115 views
0

我有5个测试套件(即多个类),每个套件都有自己单独的server.start()和stop()以及selenium.start()并停止()。现在,我想要的是:我希望我的脚本在不停止服务器的情况下运行所有​​测试套件。换句话说,如果你想说,我需要在一个会话中运行多个测试套件。任何人都可以帮助我如何做到这一点?如何通过运行一台硒服务器运行多个测试用例

+0

你正在使用什么单元测试框架? Junit或TestNG,你目前如何执行你的个案? –

+0

感谢您的回复。我正在使用Junit框架。目前,我使用TestSuite类来运行测试,但每个测试套件都作为单独的服务器运行。 – Rank

回答

0

我真的很想知道你正在使用什么样的测试框架,像Varun问的。如果你使用的是Python,你可以看看这个例子,它以_test.py结尾运行任何测试文件。

#!/usr/bin/python27 

import os 
import sys 

failure = False 
f = open('test.log', 'w') 
xmlHeader = """ 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> """ 
f.write(xmlHeader) 

for dirname, dirnames, filenames in os.walk('.'): 
    for filename in filenames: 
     if filename.endswith("_test.py"): 
      filename = "python27 %s/%s" % (dirname, filename) 
    testLine = "Running test: " + filename 
    seperator = "---------------------------------------------------------" 
    print seperator 
      print testLine 
      print seperator 
    f.write("<testsuite name=\"" +testLine + "\">") 
      out = os.popen(filename) 
    outputString = out.read() 
      print outputString 
    f.write("""<system-out><![CDATA[""" + outputString + "]]></system-out>") 
      retval = out.close() 
      if retval: 
       failure = True 
     failString = "FAILURE - test file: " + filename 
     print failString 
     f.write("<failure desc=\"" + failString +"\"/>") 
    f.write("</testsuite>") 
f.write("</html>") 


if failure: 
    sys.exit(1) 

您可能还需要寻找到设立类似詹金斯http://jenkins-ci.org/一些东西,可以运行不同的测试套件。

0

请尝试以下操作。我正在讲这个Junit。

  1. 创建一个基类,您可以使用批注@ BeforeClass,@ AfterClass,@ Before,@ After定义4个方法。
  2. 在Before/AfterClass中添加您的服务器启动和停止。在BeforeClass中检查一次,如果服务器已经运行或没有运行。如果没有,那么只能开始。
  3. 在@Before和@After中添加硒启动和停止命令。
  4. 将此BaseClass扩展到所有Test类中。尝试运行套件。

在出现任何问题时回复。 它更好地使用TestNg,因为它可以更灵活地处理和执行测试用例。

0

您可以从您的套房 我使用硒没有任何JUnit和测试套件 删除JUnit和测试套件扶养添加类包含的主要功能,并添加函数调用您的套房 的所有类定义硒对象这样selenium = new DefaultSelenium(serverHost,serverPort,browserStartCommand,browserURL); selenium.start(); 执行全部测试后 add selenium.stop();

+0

@ Abhinav-我不认为它是不使用测试框架的好主意。原因是你需要给你写的每个测试方法添加一个调用。对于小号码。这很好,但实际上测试案例总是会很大。并为每个电话写一个不是一个好方法。你也需要明确地写出这种方法的报告框架。 –

+0

好吧我认为你的方法是不同的我正在执行从xml文件使用java反射API我的测试用例,我设计基于硒的自动化套件和使用反射硒调用 –

0

要以编程方式执行此操作,您需要创建一个静态变量来保存SeleniumServer实例。

public class SeleniumServerManager { 

    private static SeleniumServer seleniumServer; 

    public static void attemptToStartSeleniumServer() throws Exception { 
     if (null == seleniumServer) { 
      seleniumServer = new SeleniumServer(); 
      seleniumServer.start(); 
     } 
    } 
} 

你必须在所有的测试套件来引用此,以确保服务器启动

public class StackOverflowTest extends TestCase { 

    private Selenium selenium; 

    @Override 
    public void setUp() { 
     SeleniumServerManager.attemptToStartSeleniumServer(); 
     this.selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.stackoverflow.com"); 
     this.selenium.start(); 
    } 

    public void testStackoverflow() { 
     this.selenium.open("/"); 
    } 
} 

如果你不喜欢#attemptToStartSeleniumServer重复呼叫,并使用Ant或者Maven在你的项目中,看看可以管理你的服务器实例的可用Ant任务/ Maven插件。

0

添加到讨论中,您可以将Testng框架用于自动化测试。在所有测试完成后,你可以有一个Class将设置和拆除硒实例。

class something{ 
    public static Selenium selenium; 
    public static Selenium globalSeleniumInstance 

@BeforeSuite(alwaysRun = true) 
public void init(){ 
selenium = new DefaultSelenium() 
    globalSeleniumInstance = selenium; 
     selenium.start(); 
} 
@AfterSuite(alwaysRun = true) 
    public void destroy() throws Exception { 
     selenium.stop(); 
    } 

Now for you test class you can 

@Test(groups = {"myWebsite"}, alwaysRun = true) 
public class MyWebsite { 
    private Selenium selenium; 

    @BeforeClass(alwaysRun = true) 
    public void init() { 
    selenium = ResourceManager.globalSeleniumInstance; 
    selenium.windowFocus(); 
    selenium.windowMaximize(); 
    } 

    @Test(alwaysRun = true) 
    public void lookForRecentPosts() throws Exception { 
do something with selenium here 
} 
     } 

so in you testng xml file you can add the classes like these 
<suite thread-count="1" verbose="1" name="Test Automation Suite" 
    annotations="JDK" parallel="false"> 
<test name="Tests" junit="false"> 
classes> 
      <class name="com.test.managerclass" /> 
</classes> 
    </test> 

</suite>