Nose makes Testing with Python wonderful. It basically extends the Unittest and provides features such as running only failed test, skip Test cases, running test based on priorities, REGEX pattern that makes Tester’s life easy.
Installing Nose:
[code]pin install -U nose[/code]
Creating a Selenium Test Cases for Nose:
- Test 1 – Registration of the user in the http://demoaut.com/mercuryregister.php.
- Test 2 – Login of the user to the http://demoaut.com/mercurysignon.php
Project Structure :
Project Structure used with Nose in PythonLets write some code for the Login and Registration test cases in the Selenium with Nose in Python.
1 | <span style="text-decoration: underline;">Login Test Case</span> |
[python]
import nose
from selenium import webdriver
import unittest
from nose.plugins.attrib import attr
class LoginTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
cls.driver.implicitly_wait(20)
cls.driver.maximize_window()
cls.driver.get("http://newtours.demoaut.com/mercurysignon.php")
@attr(priority="high")
def test_LoginTest(self):
## Login in the Application ##
self.driver.find_element_by_xpath("//input[@name=’userName’]").send_keys("techdutta")
self.driver.find_element_by_xpath("//input[@name=’password’]").send_keys("test123")
self.driver.find_element_by_xpath("//input[@name=’login’]").click()
## Verify that the Profile is Comming , Means Login ##
self.assertTrue(self.driver.find_element_by_xpath("//html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[1]/a").is_displayed())
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if __name__ == "__main__":
nose.main(verbosity=2)
[/python]
1 | <span style="text-decoration: underline;">Registration Test Case</span> |
[python]
import nose
from selenium import webdriver
import unittest
from nose.plugins.attrib import attr
class RegTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
cls.driver.implicitly_wait(20)
cls.driver.maximize_window()
cls.driver.get("http://demoaut.com/mercuryregister.php")
@attr(priority="low")
def test_RegTest(self):
## Login in the Application ##
self.driver.find_element_by_xpath("//input[@name=’email’]").send_keys("techdutta")
self.driver.find_element_by_xpath("//input[@name=’password’]").send_keys("test123")
self.driver.find_element_by_xpath("//input[@name=’confirmPassword’]").send_keys("test123")
self.driver.find_element_by_xpath("//input[@name=’register’]").click()
## Verify that the Profile is Comming , Means Login ##
self.assertTrue(self.driver.find_element_by_xpath("//html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[1]/a").is_displayed())
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if __name__ == "__main__":
nose.main(verbosity=2)
[/python]
Now Lets see what all options we can use with Nose to Test these Test Cases.
Case 1:
Run all Test Cases in the Folder. Navigate to the Folder where all test cases are present and execute below command.This will execute every Test case that match with the TestMatch Regex
[code] >nosetests –verbosity=3[/code]
Case 2:
Run all Test Cases in the Folder based on the Low or High priority. You can mention the priority of the test function by importing attr from nose.plugins.attrib
[code] from nose.plugins.attrib import attr[/code]
Adding the attr annotations above Test Function.
[code]@attr(priority="low")[/code]
1 | Command line |
[code] >nosetests –verbosity=3[/code]
Case 3:
Running multiple test cases by Name:
Case 4:
Running Failed Test. If you have any Test case failed in the previous run. It will run only that cases(Failed).
Case 5:
You can create a config file and run it directly by entering the command line argument in it. e.g. Config.cfg
[code]
[nosetests]
verbosity=3
with-doctest=1
[/code]
Case 6:
Running test with Regex Pattern
[code](Default: (?:^|[b_./-])[Tt]est [NOSE_TESTMATCH])[/code]
Command Line:
[code]nosetests –testmatch=REGEX[/code]
Case 7:
Arguments to see all plugins installed
[code]nosetests –plugins[/code]
More about Nose can find here.
SourceCode on Github
You can study a lot about new modules . Thanks for the post
Thanks a alot for the great tutorial
Good pоst. I will be facing some of these issues as well..
my blog post :: led lommelygte tilbud
Way too hard to maintain. A lot of companies just trying to get rid of it
Yeah but still people are using it extensively. 🤤
Yes but you need to look at all the pluses and negatives. Many companies are just trying to get rid of it. Too much to just maintain these type of nose framewotks