Getting Started With Nose In Python [Tutorial]

Himanshu Sheth

Posted On: April 16, 2021

view count96885 Views

Read time16 Min Read

A challenge that many developers face in Selenium test automation is choosing the right test framework that can help them come up with automated tests with minimal (or no) requirement of boilerplate code. Like me, most of you would have come across test code where a huge chunk of code is written to perform a simple test.

The selection of the right test automation framework can greatly simplify the job of the developer working on the test code. The framework features can be exploited to write tests that perform the job with minimal implementation. As far as Selenium Python testing is concerned, there are several test automation frameworks such as PyUnit, Pytest, Robot, Cucumber, etc. to choose from.

Python’s standard unittest module loses ground to other Python test automation frameworks, as it requires a lot of boilerplate code and tests have to be included into large test classes. Nose is a popular alternative if you still want to use the default Python unit testing framework. It has a powerful set of features that extends unittest to make testing easier. In this Python Nose tutorial, we deep-dive into the Nose framework and how Nose can be leveraged to perform Selenium test automation (using unittest) more efficiently.

Introduction to Nose framework

Nose is a popular test automation framework in Python that extends unittest to make testing easier. The other advantages of using the Nose framework are the enablement of auto discovery of test cases and documentation collection.

The Nose framework has a rich set of plugins that aid in test execution, parallel (or multi-process) testing, logging & reporting, and more. It can also run doctests, unittests, as well as, no boilerplate tests. The plugins also add support for decorators, fixtures, parameterized testing, classes, and modules.

Nose framework

The latest version of Nose is Nose2, however, we have observed that a significant percentage in the developer & test ecosystem is still making use of the older version of Nose i.e. version 1.3.7.

Hence, the Python Nose tutorial series for Selenium test automation is divided into two sets, with this set focusing on usage of Nose 1.3.7 for Selenium Python testing.

How to install the Nose framework

The Nose framework can be installed by executing the following command on the terminal:

pip install nose

As seen in the installation screenshot below, the version that is installed is 1.3.7. As nose and nose2 are two separate projects, the command for installation is not the same.

The Nose package can be imported by using import nose in the code, however this step is optional. If you are making use of specific modules in Nose, the same has to be imported using import Nose.<module_name> in the code.

How to execute Nose Tests

As the Nose module is installed for the existing Python distribution as well as nosetests.exe, tests using Nose framework can be executed by triggering either of the following commands:

Option 1

Option 2

Test Discovery using Nose framework

Here are some of the simple rules for test discovery:

  • Like other Python automation frameworks, Nose also automatically executes all the tests that are present in the parent folder (and its sub-folders)
  • The modules (or files) that are picked up by the framework should start with ‘test_’
  • The test methods have to start with ‘test_’
  • The test classes enclosing the test methods should start with ‘Test’

These are some of the naming conventions that we will use for Selenium python testing with Nose. These set of rules suffice the purpose of Test Automation, however, you could have a look at the complete set of rules in the finding tests section on the Nose website.

Example usage of Nose framework

The nomenclature followed in the unittest framework also applies for the Nose framework.

To demonstrate the Nose framework usage in this Python Nose tutorial, we use a simple Selenium test automation example where Google search for ‘LambdaTest’ is performed and a click action is performed on the first result.

As seen in the implementation, we have not imported the Nose module. The implementation is more or less the same as the one being used with other Python automation frameworks. Hence, we would not go deeper into the implementation aspects of the test. Shown below is the execution screenshot:

The major advantage of using Nose (over standard unittest module) is that it automatically collects tests. There is flexibility to write simple test functions and/or test classes that are not a subclass of unittest.TestCase.

Fixtures in Nose Framework

The Nose framework supports fixtures (setup and teardown methods) at test, package, class, and module levels. This helps eliminate the unnecessary initializations that can hamper the performance of the tests if done frequently.

Like py.test or unittest fixtures, the setup method always runs before any test (or collection of tests) and the teardown method runs if the setup method has successfully executed. This is irrespective of the status of the test run. Nose extends the unittest fixture model of setup (and teardown).

Fixtures can be achieved at the following levels:

1. Test Packages

Tests can be grouped into test packages. Hence, the setup and teardown methods for the package are run once per test run, rather than creating setup and teardown methods that are run once per module or test case.

For creating setup and teardown for a package-level execution, the respective methods should be defined in the __init__.py of the test package. The nomenclature is as below:

2. Test Modules

This lets us define setup and teardown methods at a module level. The respective methods will be executed at the beginning and end of the module. The nomenclature is as below:

3. Test Functions

This lets you define setup and teardown at a function level. As the name indicates, the setup_function & teardown_function are executed before & after a test function call.

There is no specific naming convention, except that you have to apply the setup method with the @with_setup decorator, which is imported from Nose. It is a widely used decorator and we would demonstrate its usage in the upcoming examples.

4. Test Classes

A test class is a class defined in a test module that matches test_Match or is a subclass of unittest.TestCase. The respective setup and teardown functions are run at the beginning and the end of the class of test methods. Here is the nomenclature for the class-level setup fixtures:

Apart from following the right naming convention, the setup method should be applied with the @classmethod decorator.

To demonstrate the usage of fixtures, we use Nose fixtures in different levels – class, module, and method.

The -s (or –nocapture) option in nosetests.exe is used so that any stdout output is captured immediately. The following command is used to trigger the execution:

Here is the output screenshot:

As seen in the execution screenshot, the module-level setup method is run at the start of the execution and the module-level teardown method is run at the end of the execution. For the test method test_case_2 (which is a part of test_class_1), setup_class() method is called before the test method is triggered.

Post this, the method-level setup method (that is a part of test_class_1) is executed. The corresponding teardown methods are called in a similar order (i.e. first method-level teardown method is executed, then the class-level teardown method is executed).

Demonstration of fixtures in Nose

For demonstrating the usage of fixtures in this Python Nose tutorial, we use a cross browser testing example that comprises of two test cases:

Test Case – 1

  1. Navigate to the URL https://www.google.com
  2. Search for ‘LambdaTest’
  3. Click on the search result with title – LambdaTest: Most Powerful Cross Browser Testing Tool Online
  4. Assert if the title of the newly opened window does not match with the expected title

Test Case – 2

  1. Navigate to the URL https://lambdatest.github.io/sample-todo-app/
  2. Select the first two checkboxes
  3. Send ‘Happy Testing at LambdaTest’ to the textbox with id = sampletodotext
  4. Click the Add Button and verify whether the text has been added or not

Implementation

Code WalkThrough

To get started, the with_setup method is imported from the nose.tools module. The setup_func() and teardown_func() methods are used as fixture functions that are implemented at the function level.

The Chrome WebDriver instance is initiated in the setup_func() method and its resources are freed in the teardown_func() method. The @with_setup decorator is used for adding the setup (i.e. setup_func) and teardown (teardown_func) methods to the respective test functions.

The Inspect Tool in Chrome is used for finding the details of the necessary web elements.

Also Read: SelectorsHub: The Next Gen XPath, CSS Selectors Tool

Selenium Webdriver APIs such as find_element, send_keys, etc. are used to locate the required web elements and perform required actions on those elements. In the Python Nose Tutorial, we would not go deeper into the implementation since that is independent of the test framework being used for Selenium Python testing.

Execution

The following command is used for triggering the test execution:

Shown below is the execution screenshot:

Before executing the test cases, the corresponding method for setup (i.e. setup_func) is called. Once the test function’s execution is complete, the corresponding method for teardown (i.e. teardown_func) is called.

Parameterized Testing with Nose Framework

The Nose framework (version 1.3.7) does not have direct support for parameterized testing. The parameterized (earlier known as nose-parameterized) package is used for performing parameterized testing with Nose. Apart from Nose, the package also supports all the popular test automation frameworks in Python.

The parameterized package is installed by issuing the following on the terminal:

The latest version of the parameterized package is 0.7.4.

Parameterized Testing in Nose on a local Selenium Grid

I’ll test the ToDo app on LambdaTest for this Python Nose Tutorial, which will be tested against three different browsers: Firefox, Microsoft Edge, and Chrome. Here is the overview of the test scenario:

  1. Navigate to the URL https://lambdatest.github.io/sample-todo-app/
  2. Select the first two checkboxes
  3. Send ‘Happy Testing at LambdaTest’ to the textbox with id = sampletodotext
  4. Click the Add Button and verify whether the text has been added or not

Implementation

Code WalkThrough

The parameterized and parameterized_class modules are imported from the parameterized package.

Fixture at function-level is used, the major difference is that the @with_setup decorator will only have the teardown method, as different test browsers are passed to the test function using the @parameterized decorator.

The name of the test browser is passed an argument to the test function (i.e. test_to_do_app). The test function is executed once against every browser combination and the resources used during setup are freed in the teardown method (i.e. teardown_func).

Depending on the browser against which Selenium test automation, the corresponding WebDriver instance is initiated.

The remaining implementation is the same as it is only related to Selenium automation testing. Here is the output snapshot:

In this pytest tutorial, learn how to use parameterization in pytest to write concise and maintainable test cases by running the same test code with multiple data sets.

Parameterized Testing in Nose on a cloud-based Selenium Grid

Selenium test automation on a local Selenium Grid infrastructure can hit a roadblock, as scaling the in-house infrastructure requires a huge investment. This is because the infrastructure has to be updated timely with umpteen different browsers, browser versions, and devices.

A more scalable approach to extract the full potential of Selenium Python testing is by leveraging parallelization, in conjunction with the features supported by cloud-based remote Selenium grid. LambdaTest is one such cloud-based cross browser testing platform that lets you perform Selenium test automation across 2,000+ different combinations of browsers, operating systems, and devices.
The effort involved in porting a working test implementation that is tested on a local Selenium grid to a cloud-based remote Selenium grid is minimal, as the code changes are majorly related to the infrastructure.

In order to get started with testing on LambdaTest, you have to create a profile on LambdaTest and make a note of the user-name & access-key from the Profile Section. The combination of access key and password is used for accessing the Selenium Grid on LambdaTest. The LambdaTest Dashboard gives out all the details related to the tests performed on the Selenium grid. The LambdaTest capabilities generator is used for generating the desired browser and platform capabilities required for selenium automation testing.

In this Python Nose tutorial I’ll demonstrate parameterized testing on LambdaTest. To get started we execute the test cases used in the earlier section on these browser + OS combinations:

Implementation

Code Walkthrough

As the tests are executed on a cloud-based Selenium Grid, the credentials consisting of the combination of user-name and pass-key is used for accessing the LambdaTest grid URL – @hub.lambdatest.com/wd/hub

The remote WebDriver Ausese of remote URL and browser capabilities generated using the capabilities generator.

The test case takes three input arguments – browser name, browser version, and platform name. These entries make up the desired capabilities which are passed as arguments to the WebDriver API.

The rest of the implementation is self-explanatory as it uses relevant Selenium WebDriver APIs to locate the required web elements and perform relevant actions on the same.

The execution screenshot is below:

Parallel testing on a cloud-based Selenium Grid

Like other popular test frameworks like Python, Nose also supports parallel testing. The nose.plugins.multiprocess plugin can be used to parallelize the test run across a configurable number of worker processes.

Though parallelization in execution expedites CPU-bound test runs, it greatly benefits IO-bound tests where most of the time is spent on waiting for the arrival of data. The official documentation on nose has in-depth information related to parallel testing.

In this particular Python Nose tutorial, we would focus on parallel testing on a Cloud-based Selenium Grid. For use cases related to selenium grid for cross browser testing, the command-line option (–processes) in nosetests can be used to spread the test execution across multiple cores.

The following command is useful for achieving parallelization when using Nose for Selenium test automation:

Here is the detailed description of –processes = NUM option that can be used to parallelize tests that use the Nose framework.

Though parallel testing can reap significant benefits when used on a local Selenium Grid, it will be multiplied if used on a cloud-based Selenium grid. Hence, we decided to demonstrate parallel testing in Nose on a cloud-based Selenium grid for this Python Nose tutorial, as code changes are self-explanatory.

Users on the popular website StackOverflow have been looking to leverage parallel testing in Nose (here and here) and this section of Python Nose Tutorial will help you get started with parallel testing in Nose.

Here are the three tests that have to be executed in parallel on LambdaTest’s Selenium grid.

Test Case – 1 (Browser – Chrome, Version – 71.0, Platform – Windows 10)

  1. Navigate to the URL https://lambdatest.github.io/sample-todo-app/
  2. Select the first two checkboxes
  3. Send ‘Happy Testing at LambdaTest’ to the textbox with id = sampletodotext
  4. Click the Add Button and verify whether the text has been added or not

Test Case – 2 (Browser – Firefox, Version – 64.0, Platform – Windows 10)

  1. Navigate to the URL https://www.lambdatest.com/blog/
  2. The expected title is LambdaTest | A Cross Browser Testing Blog
  3. Assert if the title of the opened window does not match with the expected title

Test Case – 3 (Browser – Safari, Version – 12.0, Platform – macOS Mojave)

  1. Navigate to the URL https://www.google.com
  2. Search for “Lambdatest”
  3. Locate the first search result and click on the same
  4. Assert if the title of the opened window does not match with the expected title

To get started, the desired browser and platform capabilities are generated using the LambdaTest capabilities generator. For example, shown below is the desired capabilities for Test Case 1

The same sequence is repeated for the remaining two browser and OS combinations. Three separate test cases are created and the corresponding browser capabilities are used for the test scenarios.

Implementation

Code Walkthrough

As the required browser capabilities are used in the individual test cases, there is no requirement for the setup function. The teardown function terminates the WebDriver instance.

The @with_setup decorator is used for adding teardown (teardown_func) method to the respective test functions.

There are no changes required in the core implementation as the changes are only related to the infrastructure.

Also Read: Parallel Testing With Selenium & Python Using unittest

Execution

The following command is used for executing the three test cases in parallel on the cloud-based Selenium Grid:

The reason for selecting 3 is that my current billing plan enables the execution of 5 tests in parallel. Hence, all the 3 test cases were simultaneously executed on the platform.

Here is the screenshot of the execution which indicates that the three tests are running in parallel on the cloud-based Selenium Grid:

The tests were successfully executed on the Selenium Grid:

Start your Python web automation testing today.

Wrapping It Up

In this Python Nose tutorial series , I gave you a brief look at the Nose (version 1.3.7), a test framework for Selenium Python testing. The framework is an extension to unittest that makes testing easier. The main advantage of choosing Nose over unittest is that it eliminates the requirement of boilerplate code.

It also has an extensive set of plugins that adds support for decorators, fixtures, parameterized testing, and more. These features enhance the usability of the framework. The latest version of Nose is Nose2 and we would have an in-depth look in our next blog on Python Nose tutorial series.

I hope this tutorial has helped you understand and perform selenium python testing with Nose. In case you have any questions, please feel free to reach out to us and would love to have your feedback.

Author Profile Author Profile Author Profile

Author’s Profile

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Blogs: 132



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free