Home >Backend Development >Python Tutorial >How to get started with Nose in Python
Translator | Li Rui
Reviewer | Sun Shujuan
In this Python Nose tutorial, we will study the Nose framework in depth. Nose is a test automation framework that extends unittest and further leverages Nose to perform Selenium test automation.
One challenge that many developers face in Selenium test automation is how to choose the right testing framework to help them complete automated testing with minimal (or no need) boilerplate code. Most people come across test code and have to write a lot of code to perform a simple test.
Choosing the right test automation framework can significantly simplify a developer’s job of handling test code. Tests can be written using framework features to perform work with minimal implementation. As far as Selenium Python testing is concerned, there are several test automation frameworks to choose from such as PyUnit, Pytest, Robot, and Cucumber.
Python's standard unittest module was superseded by other Python test automation frameworks because it required a lot of boilerplate code and its tests had to be contained in large test classes. If developers still want to use the default Python unit testing framework, Nose is a popular alternative. It has a powerful set of features that extend unittest to make testing easier. In this Python Nose tutorial, take a deep dive into the Nose framework and how to leverage Nose to perform Selenium test automation (using unittest) more efficiently.
Nose is a popular test automation framework in Python that extends unittest to make testing easier. Other advantages of using the Nose framework are support for automatic discovery of test cases and documentation collection.
The Nose framework has a rich set of plugins that can help with test execution, parallel (or multi-process) testing, logging and reporting, and more. It can also run documentation tests, unit tests, and non-boilerplate tests. These plugins also add support for decorators, fixtures, parameterized tests, classes, and modules.
The latest version of Nose is Nose 2, however, a large part of the developer and testing ecosystem is still Use an older version of Nose, version 1.3.7.
So, the Python Nose tutorial series for Selenium test automation is divided into two parts, this part focuses on Selenium Python testing using Nose1.3.7.
You can install the Nose framework by executing the following command on the terminal:
pip install nose
As shown in the installation screenshot below, the installed version is 1.3.7. Since nose and nose 2 are two independent projects, the installation commands are different.
You can use import nose in the code to import the Nose package, but this step is optional. If you use a specific module in Nose, you must import the same module in your code using import Nose.
Since the Nose module is installed for existing Python distributions as well as nosetests.exe, it can be passed Trigger any of the following commands to execute tests using the Nose framework:
(1) Option 1
Shell 1 nosetests <file_name.py>
(2) Option 2
Shell 1 python -m nose <file_name.py>
The following are some simple rules for test discovery:
These are some naming conventions that will be used for Selenium python testing using Nose. This set of rules is sufficient for test automation, and one can view the full set of rules in the Find a Test section of the Nose website.
The nomenclature followed in the unittest framework also applies to the Nose framework.
To demonstrate the use of the Nose framework in this Python Nose tutorial, a simple Selenium test automation example is used where a Google search for "LambdaTest" is performed and a click is performed on the first result operate.
Python 1 from selenium import webdriver 2 import time 3 from time import sleep 4 from selenium.webdriver.common.by import By 5 6 def test_google_search(): 7driver = webdriver.Chrome() 8driver.get('https://www.google.com') 9 driver.maximize_window() 10 title = "Google" 11 assert title == driver.title 12 13 search_text = "LambdaTest" 14 # search_box = driver.find_element_by_xpath("//input[@name='q']") 15 search_box = driver.find_element(By.XPATH, "//input[@name='q']") 16 search_box.send_keys(search_text) 17 18 # Using Sleep is not a good programming practice 19 # Only used here for demonstration purpose 20 time.sleep(5) 21 search_box.submit() 22 23 time.sleep(5) 24 25 # Click on the LambdaTest HomePage Link 26 # This test will fail as the titles will not match 27 title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest" 28 # lt_link = driver.find_element_by_xpath("//h3[.='LambdaTest: Most Powerful Cross Browser Testing Tool Online']") 29 lt_link = driver.find_element(By.XPATH, "//h3[.='LambdaTest: Most Powerful Cross Browser Testing Tool Online']") 30 lt_link.click() 31 32 time.sleep(10) 33 assert title == driver.title 34 time.sleep(2) 35 36 # Release the resources in the teardown function 37 print("TearDown initiated") 38 driver.quit() 39
Swipe left and right to view the complete code
As can be seen from the implementation, the Nose module is not imported. The implementation is more or less the same as that used by other Python automation frameworks. Therefore, the implementation aspects of the tests will not be delved into more deeply. The following picture is an execution screenshot:
使用Nose(超过标准unittest模块)的主要优点是它自动收集测试,可以灵活地编写不是unittest.TestCase子类的简单测试函数或测试类。
Nose框架在测试、包、类和模块级别支持Fixtures(setup和teardown方法)。这有助于消除不必要的初始化,如果频繁执行这些初始化可能会妨碍测试的性能。
与py.test或unittest Fixtures一样,setup方法总是在任何测试(或测试集合)之前运行,并且如果setup方法成功执行,则运行teardown方法。这与测试运行的状态无关。Nose扩展了设置(和拆卸)的单元测试Fixtures模型。
Fixtures可以在以下级别实现:
可以将测试分组到测试包中。因此,测试包的setup和teardown方法在每次测试运行时运行一次,而不是创建每个模块或测试用例运行一次的setup和teardown方法。
要为测试包执行创建setup和teardown,应在测试包的__init__.py中定义相应的方法。其命名法如下:
Python 1 Setup - setup, setup_package, setUp, or setUpPackage 2 TearDown - teardown, teardown_package, tearDown or tearDownPackage
这可以在测试包级别定义setup和teardown方法。相应的方法将在模块的开头和结尾执行。其命名法如下:
Python 1 Setup - setup, setup_module, setUp or setUpModule 2 TearDown - teardown, teardown_module, or tearDownModule
这使开发人员可以在函数级别定义setup和teardown。顾名思义,setup_function和teardown_function在测试函数调用之前和之后执行。
没有特定的命名约定,除了必须使用从Nose导入的@with_setupdecorators应用setup方法。它是一种广泛使用的decorators,以下将在接下来的示例中演示它的用法。
测试类是在测试模块中定义的与test_Match匹配的类,或者是unittest.TestCase的子类。各自的setup和teardown函数在测试方法类的开始和结束时运行。以下是类级设置Fixtures的命名法:
Python 1 Setup - setup_class, setupClass, setUpClass, setupAll (or setUpAll) 2 TearDown - teardown_class, teardownClass, tearDownClass, teardownAll (or tearDownAll)
除了遵循正确的命名约定之外,setup方法应该与@classmethoddecorators一起应用。
为了演示Fixtures的用法,在不同的层次上使用了Nose fixtures——类、模块和方法。
Python 1 import nose 2 from nose.tools import with_setup 3 4 # Can also be setup, setup_module, setUp or setUpModule 5 def setUpModule(module): 6 print ("") 7 print ("%s" % (setUpModule.__name__,)) 8 9 # Can also be teardown, teardown_module, or tearDownModule 10 def tearDownModule(module): 11 print ("%s" % (tearDownModule.__name__,)) 12 13 def setup_func(): 14 print ("%s" % (setup_func.__name__,)) 15 16 def teardown_func(): 17 print ("%s" % (teardown_func.__name__,)) 18 19 @with_setup(setup_func, teardown_func) 20 def test_case_1(): 21 print ("%s" % (test_case_1.__name__,)) 22 23 class test_class_1: 24 25 def setup(self): 26 print ("%s called before each test method" % (test_class_1.setup.__name__,)) 27 28 def teardown(self): 29 print ("%s called after each test method" % (test_class_1.teardown.__name__,)) 30 31 @classmethod 32 def setup_class(cls): 33 print ("%s called before any method in this class is executed" % (test_class_1.setup_class.__name__,)) 34 35 @classmethod 36 def teardown_class(cls): 37 print ("%s called after methods in this class is executed" % (test_class_1.teardown_class.__name__,)) 38 39 def test_case_2(self): 40 print ("%s" % (test_class_1.test_case_2.__name__,))
使用nosetests.exe中的-s(或-nocapture)选项,以便立即捕获任何stdout输出。以下命令用于触发执行:
Python 1 nosetests --verbose --nocapture Nose_Fixture_Example.py
这是输出屏幕截图:
如进行的截图所示,模块级setup方法在执行开始时运行,而模块级teardown方法在执行结束时运行。对于测试方法test_case_2(它是test_class_1的一部分),setup_class()方法在测试方法被触发之前被调用。
在发布这一消息之后,将执行方法级设置方法(即test_class_1的一部分)。相应的teardown方法以类似的顺序调用(即首先执行方法级teardown方法,然后执行类级teardown方法)。
为了演示这一Python Nose教程中Fixtures的用法,使用了一个包含两个测试用例的跨浏览器测试示例:
(1)导航到URL:Google.com。
(2)搜索“LambdaTest”。
(3)点击标题为-LambdaTest的搜索结果:强大的跨浏览器在线测试工具
(4)确定新打开的窗口的标题是否与预期的标题不匹配
(1)导航到URL;Lambdatest GitHub
(2)选中前两个复选框
(3)将“在LambdaTest进行快乐测试”发送到id=sampletodotext的文本框
(4)单击添加按钮,并验证是否已添加文本
执行
Python 1 from nose.tools import with_setup 2 from selenium import webdriver 3 import time 4 from time import sleep 5 from selenium.webdriver.common.by import By 6 7 def setup_func(): 8 global driver 9print("setup_func: setUp Method called") 10 driver = webdriver.Chrome() 11 driver.maximize_window() 12 13 def teardown_func(): 14 global driver 15 print("teardown_func: Teardown Method called") 16 driver.quit() 17 18 @with_setup(setup_func, teardown_func) 19 def test_1(): 20 global driver 21 print("test_1: Initiated") 22 driver.get('https://www.google.com') 23 title = "Google" 24 assert title == driver.title 25 26 search_text = "LambdaTest" 27 search_box = driver.find_element(By.XPATH,"//input[@name='q']") 28 search_box.send_keys(search_text) 29 30 # Using Sleep is not a good programming practice 31 # Only used here for demonstration purpose 32 time.sleep(5) 33 search_box.submit() 34 35 time.sleep(5) 36 37 # Click on the LambdaTest HomePage Link 38 # This test will fail as the titles will not match 39 title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest" 40 lt_link = driver.find_element(By.XPATH,"//h3[.='LambdaTest: Most Powerful Cross Browser Testing Tool Online']") 41 lt_link.click() 42 43 time.sleep(10) 44 assert title == driver.title 45 time.sleep(2) 46 47 @with_setup(setup_func, teardown_func) 48 def test_2(): 49 global driver 50 print("test_2: Initiated") 51driver.get('https://lambdatest.github.io/sample-todo-app/') 52 53driver.find_element(By.NAME,"li1").click() 54driver.find_element(By.NAME,"li2").click() 55 56 title = "Sample page - lambdatest.com" 57 assert title == driver.title 58 59 sample_text = "Happy Testing at LambdaTest" 60 email_text_field =driver.find_element(By.ID, "sampletodotext") 61 email_text_field.send_keys(sample_text) 62 time.sleep(5) 63 64 driver.find_element(By.ID, "addbutton").click() 65 time.sleep(5) 66 67 assert driver.find_element(By.XPATH, "//span[.='Happy Testing at LambdaTest']").text == sample_text
代码演练
首先,从nose.tools模块导入with_setup方法。setup_func()和teardown_func()方法用作在函数级别实现的Fixture函数。
Python 1 from nose.tools import with_setup 2 ................................. 3 4 def setup_func(): 5global driver 6print("setup_func: setUp Method called") 7driver = webdriver.Chrome() 8driver.maximize_window() 9 10 def teardown_func(): 11 global driver 12 print("teardown_func: Teardown Method called") 13 driver.quit()
Chrome Web Driver实例在setup_func()方法中启动,其资源在teardown_func()方法中释放。@with_setupdecorators用于将设置(即setup_func)和拆卸(teardown_func)方法添加到相应的测试函数中。
Python 1 @with_setup(setup_func, teardown_func) 2 def test_1(): 3 .......................... 4 .......................... 5 .......................... 6 7 @with_setup(setup_func, teardown_func) 8 def test_2(): 9 .......................... 10 .......................... 11 ..........................
Chrome中的检查工具用于查找必要Web元素的详细信息。
Selenium Webdriver API,例如find_element、send_keys等,用于定位所需的Web元素并对这些元素执行所需的操作。在Python Nose教程中,不会深入研究实现,因为它独立于用于Selenium Python测试的测试框架。
执行
以下命令用于触发测试执行:
Shell 1 nosetests --verbose --nocapture <file-name.py>
下图为执行截图:
在执行测试用例之前,会调用相应的setup方法(即setup_func)。一旦测试函数的执行完成,就会调用相应的teardown方法(即teardown_func)。
Nose框架(1.3.7版)不直接支持参数化测试。参数化(以前称为Nose参数化)包用于使用Nose执行参数化测试。除了Nose,该包还支持Python中所有流行的测试自动化框架。
通过在终端上发出以下命令来安装参数化包:
Shell 1 pip install parameterized
参数化包的最新版本是0.7.4。
将针对这个Python Nose教程在Lambda Test上测试ToDo应用程序,面向三种不同的浏览器进行测试:Firefox、Microsoft Edge和Chrome。以下是测试场景的概述:
(1)导航到URL https://lambdatest.github.io/sample-todo-app/
(2)选中前两个复选框
(3)将“在Lambda Test进行快乐测试”发送到id=sampletodotext的文本框
(4)单击添加按钮并验证是否已添加文本
执行
Python 1 # test_math.py 2 from nose.tools import assert_equal 3 from parameterized import parameterized, parameterized_class 4 import unittest 5 from nose.tools import with_setup 6 from selenium import webdriver 7 import time 8 from time import sleep 9 from selenium.webdriver.common.by import By 10 11 def teardown_func(): 12 global driver 13 print("teardown_func: Teardown Method called") 14 driver.quit() 15 16 @parameterized([ 17 ("Firefox"), 18 ("Chrome"), 19 ("MicrosoftEdge"), 20 ]) 21 22 @with_setup(None, teardown_func) 23 def test_to_do_app(browserName): 24 global driver 25 26 if (browserName == "Chrome"): 27 print("Test on Chrome Browser initiated") 28 driver = webdriver.Chrome() 29 elif (browserName == "MicrosoftEdge"): 30 print("Test on Edge Browser initiated") 31 # Set the Path accordingly 32 driver = webdriver.Edge("C:\EdgeDriver\MicrosoftWebDriver.exe") 33 elif (browserName == "Firefox"): 34 print("Test on Firefox initiated") 35 driver = webdriver.Firefox() 36 driver.get('https://lambdatest.github.io/sample-todo-app/') 37 driver.maximize_window() 38 39 driver.find_element(By.NAME, "li1").click() 40 driver.find_element(By.NAME, "li2").click() 41 42 title = "Sample page - lambdatest.com" 43 assert title == driver.title 44 45 sample_text = "Happy Testing at LambdaTest" 46 email_text_field =driver.find_element(By.ID, "sampletodotext") 47 email_text_field.send_keys(sample_text) 48 time.sleep(5) 49 50 driver.find_element(By.ID, "addbutton").click() 51 time.sleep(5) 52 53 assert driver.find_element(By.XPATH, "//span[.='Happy Testing at LambdaTest']").text == sample_text
代码演练
parameterized和parameterized_class模块是从参数化包中导入的。
Python 1 from parameterized import parameterized, parameterized_class
使用了函数级别的Fixture,主要区别在于@with_setup decorators将只有teardown方法,因为不同的测试浏览器使用@parameterized decorators传递给测试函数。
Python 1 def teardown_func(): 2 global driver 3 print("teardown_func: Teardown Method called") 4 driver.quit() 5 6 @parameterized([ 7 ("Firefox"), 8 ("Chrome"), 9("MicrosoftEdge"), 10 ])
测试浏览器的名称作为参数传递给测试函数(即test_to_do_app)。测试函数针对每个浏览器组合执行一次,设置期间使用的资源在teardown方法(即teardown_func)中释放。
根据Selenium测试自动化所针对的浏览器,启动相应的WebDriver实例。
Python 1 @with_setup(None, teardown_func) 2 ef test_to_do_app(browserName): 3 lobal driver 4 5 f (browserName == "Chrome"): 6 rint("Test on Chrome Browser initiated") 7iver = webdriver.Chrome() 8 lif (browserName == "MicrosoftEdge"): 9 print("Test on Edge Browser initiated") 10 # Set the Path accordingly 11 driver = webdriver.Edge("C:\EdgeDriver\MicrosoftWebDriver.exe") 12 lif (browserName == "Firefox"): 13 rint("Test on Firefox initiated") 14 river = webdriver.Firefox()
其余实现相同,仅与Selenium自动化测试相关。这是输出快照:
本地Selenium网络基础设施上的Selenium测试自动化可能会遇到障碍,因为扩展内部基础设施需要大量投资。这是因为必须使用大量不同的浏览器、浏览器版本和设备及时更新基础设施。
一种更具可扩展性的方法来充分发挥Selenium Python测试的潜力,是利用并行化以及基于云的远程Selenium网格支持的功能。LambdaTest就是这样一种基于云的跨浏览器测试平台,可让跨3,000多种不同的浏览器、操作系统和设备组合执行Selenium测试自动化。
将在本地Selenium网格上测试的工作测试实现移植到基于云的远程Selenium网格所涉及的工作量很小,因为代码更改主要与基础架构相关。
为了开始在LambdaTest上进行测试,必须在LambdaTest上创建一个配置文件,并记下配置文件部分中的用户名和访问密钥。访问密钥和密码的组合用于访问LambdaTest上的Selenium网格。LambdaTest仪表板提供了与在Selenium网格上执行的测试相关的所有详细信息。LambdaTest功能生成器用于生成Selenium自动化测试所需的浏览器和平台功能。
在这个Python Nose教程中,将在LambdaTest上演示参数化测试。首先,在这些浏览器+操作系统组合上执行前面部分中使用的测试用例:
Python 1 @parameterized([ 2("Chrome", "83.0", "Windows 10"), 3("MicrosoftEdge", "81.0", "macOS High Sierra"), 4 ("Safari", "12.0", "macOS Mojave"), 5("Firefox", "76.0", "Windows 10"), 6 ]) 实施 Python 1 # test_math.py 2 from nose.tools import assert_equal 3 from parameterized import parameterized, parameterized_class 4 from nose.tools import with_setup 5 from selenium import webdriver 6 import time 7 from time import sleep 8 import urllib3 9 import warnings 10 from selenium.webdriver.common.by import By 11 12 def setup_func(): 13 global driver 14 global remote_url 15 print("setup_func: SetUp Method called") 16 # Details can be sourced from https://automation.lambdatest.com/ 17 user_name = "user-name" 18 app_key = "pass-key" 19 remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub" 20urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) 21 22 def teardown_func(): 23 global driver 24 print("teardown_func: Teardown Method called") 25 driver.quit() 26 27 @parameterized([ 28 ("Firefox", "76.0", "Windows 10"), 29 ("Chrome", "83.0", "Windows 10"), 30 ("MicrosoftEdge", "81.0", "macOS High Sierra"), 31 ("Safari", "12.0", "macOS Mojave"), 32 ]) 33 34 @with_setup(setup_func, teardown_func) 35 def test_to_do_app(browserName, version, platform): 36 global driver 37 global remote_url 38 39 capabilities = {} 40 # Set the desired capabilities from the supplied parameters 41 capabilities["browserName"] = browserName 42 capabilities["version"] = version 43 capabilities["platform"] = platform 44 45 driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = capabilities) 46 driver.get('https://lambdatest.github.io/sample-todo-app/') 47 driver.maximize_window() 48 49 driver.find_element(By.NAME, "li1").click() 50 driver.find_element(By.NAME, "li2").click() 51 52 title = "Sample page - lambdatest.com" 53 assert title ==driver.title 54 55 sample_text = "Happy Testing at LambdaTest" 56 email_text_field = driver.find_element(By.ID, "sampletodotext") 57 email_text_field.send_keys(sample_text) 58 time.sleep(5) 59 60 driver.find_element(By.ID, "addbutton").click() 61 time.sleep(5) 62 63 assert driver.find_element(By.XPATH, "//span[.='Happy Testing at LambdaTest']").text == sample_text
代码演练
由于测试是在基于云的Selenium网格上执行的,因此由用户名和密码组合组成的凭据用于访问LambdaTest网格URL–@hub.lambdatest.com/wd/hub
Python 1 remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
远程 WebDriver 使用功能生成器生成的远程URL 和浏览器功能。
Python 1 driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = capabilities)
测试用例采用三个输入参数——浏览器名称、浏览器版本和平台名称。这些条目构成了作为参数传递给WebDriver API的所需功能。
Python 1 @with_setup(setup_func, teardown_func) 2 def test_to_do_app(browserName, version, platform): 3 ......................... 4 ......................... 5 capabilities = {} 6 # Set the desired capabilities from the supplied parameters 7 capabilities["browserName"] = browserName 8 capabilities["version"] = version 9 capabilities["platform"] = platform 10 11 driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = capabilities) 12 driver.get('https://lambdatest.github.io/sample-todo-app/') 13 ......................... 14 .........................
实现的其余部分是不言自明的,因为它使用相关的Selenium WebDriver API来定位所需的Web元素并对其执行相关操作。
执行截图如下:
与Python等其他流行的测试框架一样,Nose也支持并行测试。nose.plugins和多进程插件可用于在可配置数量的工作进程中并行化测试运行。
虽然执行中的并行化加快了CPU密集型测试运行,但它有利于IO密集型测试,因为大部分时间都花在等待数据的到达上。Nose上的官方文档有与并行测试相关的深入信息。
在这个特定的Python Nose教程中,将专注于在基于云的Selenium网格上进行并行测试。对于与用于跨浏览器测试的selenium网格相关的用例,nosetests中的命令行选项(-processes)可用于将测试执行分布在多个内核上。
在使用Nose进行Selenium测试自动化时,以下命令对于实现并行化很有用:
Shell 1 nosetests --process-timeout=<optional-process-timeout> --processes=<num-processes> file-name.py
这是可用于并行化使用Nose框架的测试的–processes=NUM选项的详细说明。
虽然在本地Selenium网格上使用并行测试可以获得显著的好处,但如果在基于云的Selenium网格上使用,它会成倍增加。因此,决定在这个Python Nose教程的基于云的Selenium网格上演示Nose中的并行测试。
流行网站StackOverflow上的用户一直在寻求利用Nose中的并行测试,Python Nose教程的这一部分将帮助开始在Nose中进行并行测试。
以下是必须在LambdaTest的Selenium网格上并行执行的三个测试。
(1)导航到URL https://lambdatest.github.io/sample-todo-app/
(2)选中前两个复选框
(3)将“在LambdaTest进行快乐测试”发送到id=sampletodotext的文本框
(4)单击添加按钮并验证是否已添加文本
(1)导航到URLhttps://www.lambdatest.com/blog/
(2)预期的标题是LambdaTest跨浏览器测试博客
(3)断言打开的窗口的标题是否与预期的标题不匹配
(1)导航到URLhttps://www.google.com
(2)搜索“Lambdatest”
(3)找到第一个搜索结果并单击相同
(4)断言打开的窗口的标题是否与预期的标题不匹配
首先,使用LambdaTest功能生成器生成所需的浏览器和平台功能。例如,下面显示的是测试用例1所需的功能。
Python 1 ch_caps = { 2 "build" : "Nose Testing using Chrome on Windows Environment", 3"name" : "Nose Testing on Chrome using Selenium Grid Environment", 4 "platform" : "Windows 10", 5 "browserName" : "Chrome", 6"version" : "71.0", 7"selenium_version" : "3.13.0", 8 "chrome.driver" : 2.42 9}
其余两个浏览器和操作系统组合重复相同的顺序。创建了三个单独的测试用例,并为测试场景使用了相应的浏览器功能。
执行
Python 1 # test_math.py 2 import nose 3 from nose.tools import <some p 4 from nose.tools import assert_equal 5 from parameterized import parameterized, parameterized_class 6import unittest 7 import math 8 from nose.tools import with_setup 9 from selenium import webdriver 10 import time 11 from time import sleep 12 import urllib3 13 import warnings 14 15 user_name = "user-name" 16 app_key = "pass-key" 17 18 # @parameterized([ 19 # ("Chrome", "83.0", "Windows 10"), 20 # ("MicrosoftEdge", "81.0", "macOS High Sierra"), 21 # ("Safari", "12.0", "macOS Mojave"), 22 # ("Firefox", "76.0", "Windows 7"), 23 #]) 24 25 #Set capabilities for testing on Chrome 26 ch_caps = { 27 "build" : "Nose Testing using Chrome on Windows Environment", 28 "name" : "Nose Testing on Chrome using Selenium Grid Environment", 29 "platform" : "Windows 10", 30 "browserName" : "Chrome", 31 "version" : "71.0", 32 "selenium_version" : "3.13.0", 33 "chrome.driver" : 2.42 34 } 35 36 #Set capabilities for testing on Firefox 37 ff_caps = { 38 "build" : "Nose Testing using Firefox on Windows Environment", 39 "name" : "Nose Testing on Firefox using Selenium Grid Environment", 40 "platform" : "Windows 10", 41 "browserName" : "Firefox", 42 "version" : "64.0", 43 } 44 45 #Set capabilities for testing on Safari 46 saf_caps = { 47 "build" : "Nose Testing using Safari on macOS Mojave Environment", 48 "name" : "Nose Testing on Safari using Selenium Grid Environment", 49 "platform" : "macOS Mojave", 50 "browserName" : "Safari", 51 "version" : "12.0", 52 } 53 54 # _multiprocess_can_split_ = True 55 56 def teardown_func(): 57 global driver 58 print("Inside TearDown") 59 driver.quit() 60 61 @with_setup(None, teardown_func) 62 def test_verify_todo_app(): 63 global driver 64urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) 65 # Details can be sourced from https://automation.lambdatest.com/ 66 remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub" 67 68 driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = ch_caps) 69 driver.get('https://lambdatest.github.io/sample-todo-app/') 70 driver.maximize_window() 71 72 driver.find_element_by_name("li1").click() 73 driver.find_element_by_name("li2").click() 74 75 title = "Sample page - lambdatest.com" 76 assert title == driver.title 77 78 sample_text = "Happy Testing at LambdaTest" 79 email_text_field =driver.find_element_by_id("sampletodotext") 80 email_text_field.send_keys(sample_text) 81 time.sleep(5) 82 83 driver.find_element_by_id("addbutton").click() 84 time.sleep(5) 85 86 assert driver.find_element_by_xpath("//span[.='Happy Testing at LambdaTest']").text == sample_text 87 88 @with_setup(None, teardown_func) 89 def test_lt_blog(): 90 global driver 91urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) 92 # Details can be sourced from https://automation.lambdatest.com/ 93 remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub" 94 95 driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = ff_caps) 96driver.get('https://www.lambdatest.com/blog/') 97 driver.maximize_window() 98 99 expected_title = "LambdaTest | A Cross Browser Testing Blog" 100 assert expected_title == driver.title 101 time.sleep(5) 102 103 @with_setup(None, teardown_func) 104 def test_verify_google(): 105 global driver 106urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) 107 # Details can be sourced from https://automation.lambdatest.com/ 108 remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub" 109 110 driver = webdriver.Remote(command_executor = remote_url, desired_capabilities = saf_caps) 111 driver.get('https://www.google.com/') 112 driver.maximize_window() 113 title = "Google" 114 assert title == driver.title 115 116 search_text = "LambdaTest" 117 search_box = driver.find_element_by_xpath("//input[@name='q']") 118 search_box.send_keys(search_text) 119 120 time.sleep(5) 121 search_box.submit() 122 123 time.sleep(5) 124 125 # Click on the LambdaTest HomePage Link 126 # This test will fail as the titles will not match 127 title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest" 128 lt_link = driver.find_element_by_xpath("//h3[.='LambdaTest: Most Powerful Cross Browser Testing Tool Online']") 129 lt_link.click() 130 131 time.sleep(10) 132 assert title == driver.title 133 time.sleep(2)
代码演练
由于在各个测试用例中使用了所需的浏览器功能,因此不需要设置功能。teardown函数终止WebDriver实例。
Python 1 # _multiprocess_can_split_ = True 2 def teardown_func(): 3global driver 4print("Inside TearDown") 5 driver.quit()
@with_setupdecorators用于将拆解(teardown_func)方法添加到相应的测试函数中。
Python 1 @with_setup(None, teardown_func) 2 def test_verify_todo_app(): 3 ............ 4............ 5 ............ 6 @with_setup(None, teardown_func) 7 def test_lt_blog(): 8 ............ 9............ 10 ............ 11 @with_setup(None, teardown_func) 12 def test_verify_google(): 13 ............ 14............ 15 ............
核心实现不需要更改,因为更改仅与基础设施相关。
执行
以下命令用于在基于云的Selenium Grid上并行执行三个测试用例
Shell 1nosetests --process-timeout=60 --processes=3 Nose_Parallel_Test.py
左右滑动查看完整代码
选择3的原因是当前的计费计划允许并行执行5个测试。因此,所有三个测试用例都在平台上同时执行。
This is a screenshot of the execution showing three tests running in parallel on a cloud-based Selenium grid:
Test executed successfully on Selenium Grid:
In this Python Nose tutorial series, the testing framework Nose (version 1.3.7) for Selenium Python testing is briefly introduced. This framework is an extension to unittest to make testing easier. The main advantage of choosing Nose over unittest is that it eliminates the requirement for boilerplate code.
It also has a large number of plug-ins, adding support for decorators, Fixtures, parameterized tests, etc. These features enhance the usability of the framework.
Hope this article can help people understand and use Nose to perform selenium python testing.
Original link: https://dzone.com/articles/getting-started-with-nose-in-python-tutorial
The above is the detailed content of How to get started with Nose in Python. For more information, please follow other related articles on the PHP Chinese website!