How to add new test cases to TEMPEST Test suite

asked 2013-12-16 10:19:52 -0600

vinayd gravatar image

updated 2013-12-18 14:29:29 -0600

larsks gravatar image

Hi,

I have some test cases to my project specific, wanted to use TEMPEST to run those tests on cinder API's, now i searched every where on google, i got very little documentation on adding new tests to TEMPEST.

Following Lars's suggestion, I tried this:

  1. Created an Folder named Mycode parallel to tempest folder and other files like README.rst.
  2. added an __init__.py under it.
  3. Now i created an .py file with below code block

    #!/usr/bin/env python 
    import Mycode 
    import sys 
    import os 
    #sys.path.append("\home\vinay\Mycode") 
    print "This is system path " 
    def func1(): 
        print "This is vinay here positive case" 
        assert (True) 
    
    def func2(): 
      print "This is vinay here negative case failed" 
      assert (Fail)
    
  4. Now when i execute the test "nosetests Mycode.code.py", its throwing error as below:

    ERROR: Failure: ImportError (No module named Mycode)
    ----------------------------------------------------------------------
    Traceback (most recent call last): File "/usr/lib/python2.6/site-packages/nose/loader.py", line 364, in loadTestsFromName addr.filename, please help
    
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-12-16 13:19:36 -0600

larsks gravatar image

updated 2013-12-18 14:34:22 -0600

Tempest uses the "nose" testing framework for Python; you can all about it here. Here's a quick example:

Let's assume we're going to be writing tests for something called "myproject". We'll start by creating a new myproject package in the top-level tempest directory:

mkdir myproject
touch myproject/__init__.py

Now we're going to add a module call test_myproject.py:

vim myproject/test_myproject.py

Inside this file will be a simple test:

def test_something_that_works():
    assert(True)

def test_something_that_fails():
    assert(False)

Now I can run just these tests by running, from the top-level directory:

$ nosetests myproject

Which will result in:

myproject.test_myproject.test_something_that_works ... ok
myproject.test_myproject.test_something_that_fails ... FAIL

======================================================================
FAIL: myproject.test_myproject.test_something_that_fails
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/lkellogg/src/tempest/myproject/test_myproject.py", line 7, in test_something_that_fails
    assert(False)
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)

These tests would also run automatically if you were to run the run_tests.sh script or tox. Note that the tempest framework can use various attributes to determine which tests are run. For example, you can elect to run only "smoke tests", which are identified in the Tempest code base like this:

from tempest.test import attr

@attr(type='smoke')
def a_simple_test():
    pass

You'll note that the actual tests are usually a little more complex than what I've shown here, and may include setup/teardown code. Look through the existing tests for examples, and read through the Nose documentation to understand how everything works.

Tempest actually uses the tox framework to run the tests; this ensure that everything runs using a Python virtualenv for packages, and makes it easy to select among different groups of tests. For example, to run just the "smoke" tests:

$ tox smoke

Or just the pep8 tests:

$ tox pep8

I hope this helps you get started.

Update

Looking at your first attempt, there are several issues you need to address:

  1. The argument to nosetests needs to be a package or module name (so, for example, nosetests Mycode.
  2. Your tests need to be named test_something (so, for example, test_func1 and test_func2).
  3. Your module needs to be named test_something also (e.g., in my example it was called test_myproject).
  4. Your package appears to be trying to import itself, which is a little unusual.

Before trying again, you should probably spend some time reading the Nosetest documentation, which explains how all of this works and includes a number of examples.

edit flag offensive delete link more

Comments

Thanks a lot for your quick suggestion, this really helps, only thing i wanted to know is that, your saying that add your project at toplevel tempest directly, which is that top-level directory your are talking about, for example if my directory is "C:\RMC\Tempest_code\temptest_working\tempest\", in these there are parallel folders such as "api","cli","Commom" etc, so need to add my folder here.....?

vinayd gravatar imagevinayd ( 2013-12-16 22:37:17 -0600 )edit

It should be the directory containing the "README.rst" file, the "tox.ini" file, the "run_tests.sh" script, etc.

larsks gravatar imagelarsks ( 2013-12-16 22:39:24 -0600 )edit

I should also note that it only makes sense to add your tests here if you expect to run them *along with all the other temptest tests*. If you expect to always run your tests independently there's no particular reason to colocate them with the tempest source.

larsks gravatar imagelarsks ( 2013-12-16 22:40:49 -0600 )edit

Thanks a lot larsks for your suggestion, i have another question, if we want to reuse some of the python classes such as "RestClient.py" in my test case execution, as i have to make some REST calls, is that ok to import those classes in my project namespace or what do u suggest...?

vinayd gravatar imagevinayd ( 2013-12-16 23:43:44 -0600 )edit

I think that's fine.

larsks gravatar imagelarsks ( 2013-12-17 08:25:45 -0600 )edit
0

answered 2013-12-19 03:33:08 -0600

darragh-oreilly gravatar image

This is a good guide.

edit flag offensive delete link more

Comments

Note that this guide is slightly outdated. Not everything in there is still up-to-date.

rahmu gravatar imagerahmu ( 2014-02-21 08:27:41 -0600 )edit

Thanks a lot for the informative and helpful post. We have written UTs and FTs for our new service say 'abc' These tests are present at /opt/stack/abc/abc/tests/functional and /opt/stack/abc/abc/tests/unit We need to run these testcases as part of tempest now !

So, we have copied our test cases at /opt/stack/tempest/tempest/tests/abc and we are trying to execute the same from nossetests. nosetest is not considering our testcases, can you please guide us here ! Yes, we have also tried to keep the same under /opt/stack/tempest/tempest/tests/cli and execute but still no use. On a liter note, please let us know, how to disable the pylint kind of errors or warnings Thanks a lot for your patience

drajsingh2304 gravatar imagedrajsingh2304 ( 2014-07-08 15:14:29 -0600 )edit

Get to know Ask OpenStack

Resources for moderators

Question Tools

3 followers

Stats

Asked: 2013-12-16 10:19:52 -0600

Seen: 1,721 times

Last updated: Dec 19 '13