EzDevInfo.com

nose

nose is nicer testing for python Installation and quick start — nose 1.3.7 documentation

Problems using nose in a virtualenv

I am unable to use nose (nosetests) in a virtualenv project - it can't seem to find the packages installed in the virtualenv environment.

The odd thing is that i can set

test_suite = 'nose.collector'

in setup.py and run the tests just fine as

python setup.py test

but when running nosetests straight, there are all sorts of import errors.

I've tried it with both a system-wide installation of nose and a virtualenv nose package and no luck.

Any thoughts?

Thanks!!


Source: (StackOverflow)

I need a sample of python unit testing sqlalchemy model with nose

Can someone show me how to write unit tests for sqlalchemy model I created using nose.

I just need one simple example.

Thanks.


Source: (StackOverflow)

Advertisements

Preferred Python unit-testing framework [closed]

So far I've been using the built-in unittest module (pyUnit) for unit-testing Python code. However, for simple cases it seems like overkill. Being a derivative of xUnit, it appears a bit heavy for the dynamic nature of Python, where I would expect to write less to achieve the same effects. On the other hand, it is built-in, it makes you write your tests in an organized way, and it is tested by time.

The major alternatives I've seen online are:

Which of the frameworks do you prefer, and why?


Update 10.12.2011: with the recent addition of test auto-discovery and many new features in unittest (in Python 2.7 and 3.2), IMHO it makes less sense to use an external library.


Regarding doctest: I don't consider it a unit-testing framework per-se. I definitely wouldn't use it to write a large suite of tests for a sizable application. doctest is more suitable for making sure that the examples you provide in the documentation work. It has its place for this need, but it isn't a competitor for unittest, py.test and other frameworks.


Source: (StackOverflow)

How do I run a single test with Nose in Pylons

I have a Pylons 1.0 app with a bunch of tests in the test/functional directory. I'm getting weird test results and I want to just run a single test. The nose documentation says I should be able to pass in a test name at the command line but I get ImportErrors no matter what I do

For example:

nosetests -x -s sometestname

Gives:

Traceback (most recent call last):
  File "/home/ben/.virtualenvs/tsq/lib/python2.6/site-packages/nose-0.11.4-py2.6.egg/nose/loader.py", line 371, in loadTestsFromName
   module = resolve_name(addr.module)
  File "/home/ben/.virtualenvs/tsq/lib/python2.6/site-packages/nose-0.11.4-py2.6.egg/nose/util.py", line 334, in resolve_name
   module = __import__('.'.join(parts_copy))
ImportError: No module named sometestname

I get the same error for

nosetests -x -s appname.tests.functional.testcontroller

What is the correct syntax?


Source: (StackOverflow)

Nose unable to find tests in ubuntu

Is there any reason why Nose wouldn't be able to find tests in Ubuntu 9.04? I'm using nose 0.11.1 with python 2.5.4. I can run tests only if I explicitly specify the filename. If I don't specify the filename it just says 0 tests.

The same project runs tests fine on my Mac, so i'm quite stumped.


Source: (StackOverflow)

List all Tests Found by Nosetest

I use nosetests to run my unittests and it works well. I want to get a list of all the tests nostests finds without actually running them. Is there a way to do that?


Source: (StackOverflow)

Python Nose Import Error

I can't seem to get the nose testing framework to recognize modules beneath my test script in the file structure. I've set up the simplest example that demonstrates the problem. I'll explain it below.

Here's the the package file structure:

./__init__.py
./foo.py
./tests
   ./__init__.py
   ./test_foo.py

foo.py contains:

def dumb_true():
    return True

tests/test_foo.py contains:

import foo

def test_foo():
    assert foo.dumb_true()

Both init.py files are empty

If I run nosetests -vv in the main directory (where foo.py is), I get:

Failure: ImportError (No module named foo) ... ERROR

======================================================================
ERROR: Failure: ImportError (No module named foo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python/site-packages/nose-0.11.1-py2.6.egg/nose/loader.py", line 379, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/lib/python/site-packages/nose-0.11.1-py2.6.egg/nose/importer.py", line 39, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/lib/python/site-packages/nose-0.11.1-py2.6.egg/nose/importer.py", line 86, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/user/nose_testing/tests/test_foo.py", line 1, in <module>
    import foo
ImportError: No module named foo

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

I get the same error when I run from inside the tests/ directory. According to the documentation and an example I found, nose is supposed to add all parent packages to the path as well as the directory from which it is called, but this doesn't seem to be happening in my case.

I'm running Ubuntu 8.04 with Python 2.6.2. I've built and installed nose manually (not with setup_tools) if that matters.


Source: (StackOverflow)

Python imports for tests using nose - what is best practice for imports of modules above current package

This is a question which is asked frequently in different forms, and often obtains "lol you're not doing it properly" responses. Pretty sure that's because there's a common sense scenario people (including me) are trying to use as an implementation, and the solution is not obvious (if you've not done it before).

Would accept an answer which "lets the fly out of the bottle".

Given

project/
    __init__.py
    /code
        __init__.py
        sut.py
    /tests
        __init__.py
        test_sut.py

Where tests_sut.py starts:

import code.sut

Running nosetests in the root dir leads to:

ImportError: No module named code.sut

Avenues traveled:

a) do a relative using

from ..code import sut

b) add root of project to PYTHONPATH

c) use the

sys.path.append

to add the .. path before the imports at the start of each test module.

d) just remember to do a

setup.py 

on the project to install the modules into the site-packages before running tests.


So the requirement is to have tests located beneath the test package root which have access to the project. Each of the above don't feel "natural" to me, have proved problematic or seem like too much hard work!

In java this works, but basically by dint of your build tool / IDE placing all your classes on the classpath. Perhaps the issue is I'm expecting "magic" from Python? Have noted in the Flask webframework tests, option d) seems to be preferred.

In any case, statements below recommending a preferred solution would remove the feeling of "unnaturalness" in my own.


Source: (StackOverflow)

Python nose2 vs. nose vs. unittest [closed]

I'd like to reiterate this old question, because much has changed in both unittest and nose (or nose2) since it's been asked.

So: What are the differences between nose and unittest?

I'm familiar with unittest, but not with nose, to the extent I'm not even sure if I should be asking about nose, nose2 or both (because "nose2 is not nose").

(I've read through this page describing the differences between nose and nose2, but for someone unfamiliar with either, it wasn't very helpful.)

I'm using python 2.7, but python 3 is also of interest.


Source: (StackOverflow)

How to exclude mock package from python coverage report using nosetests

I currently try to use the mock library to write some basic nose unittests in python.

After finishing some basic example I now tried to use nosetests --with-coverage and now I have the mock package and the package I tried to 'mock away' are shown in the coverage report. Is there a possibility to exclude these?

Here is the class I want to test:

from imaplib import IMAP4

class ImapProxy:
    def __init__(self, host):
        self._client = IMAP4(host)

And the testcase: from mock import patch

from ImapProxy import ImapProxy

class TestImap:
    def test_connect(self):
        with patch('ImapProxy.IMAP4') as imapMock:
            proxy = ImapProxy("testhost")
            imapMock.assert_called_once_with("testhost")

I now get the following output for nosetests --with-coverage

.
Name         Stmts   Miss  Cover   Missing
------------------------------------------
ImapProxy        4      0   100%   
imaplib        675    675     0%   23-1519
mock          1240    810    35%   [ a lot of lines]

Is there any way to exclude the mock package and the imaplib package without having to manually whitelisting all but those packages by --cover-package=PACKAGE

Thanks to Ned Batchelder I now know about the .coveragerc file, thanks for that!

I created a .coveragerc file with the following content:

[report]
omit = *mock*

Now my output for mock in the coverage report is:

mock                     1240   1240     0%   16-2356

It does not cover the mock package any longer but still shows it in the report.

I use Coverage.py, version 3.5.2 if this is any help.


Source: (StackOverflow)

Nose ignores test with custom decorator

I have some relatively complex integration tests in my Python code. I simplified them greatly with a custom decorator and I'm really happy with the result. Here's a simple example of what my decorator looks like:

def specialTest(fn):

    def wrapTest(self):
        #do some some important stuff
        pass
    return wrapTest

Here's what a test may look like:

class Test_special_stuff(unittest.TestCase):

    @specialTest
    def test_something_special(self):
        pass

This works great and is executed by PyCharm's test runner without a problem. However, when I run a test from the commandline using Nose, it skips any test with the @specialTest decorator. I have tried to name the decorator as testSpecial, so it matches default rules, but then my FN parameter doesn't get passed.

How can I get Nose to execute those test methods and treat the decorator as it is intended?


SOLUTION

Thanks to madjar, I got this working by restructuring my code to look like this, using functools.wraps and changing the name of the wrapper:

def specialTest(fn):

    @wraps(fn)
    def test_wrapper(self,*args,**kwargs):
        #do some some important stuff
        pass
    return test_wrapper


class Test_special_stuff(unittest.TestCase):

    @specialTest
    def test_something_special(self):
        pass

Source: (StackOverflow)

How should I verify a log message when testing Python code under nose?

I'm trying to write a simple unit test that will verify that, under a certain condition, a class in my application will log an error via the standard logging API. I can't work out what the cleanest way to test this situation is.

I know that nose already captures logging output through it's logging plugin, but this seems to be intended as a reporting and debugging aid for failed tests.

The two ways to do this I can see are:

  • Mock out the logging module, either in a piecemeal way (mymodule.logging = mockloggingmodule) or with a proper mocking library.
  • Write or use an existing nose plugin to capture the output and verify it.

If I go for the former approach, I'd like to know what the cleanest way to reset the global state to what it was before I mocked out the logging module.

Looking forward to your hints and tips on this one...


Source: (StackOverflow)

How do I tell django-nose where my tests are?

I have my tests for a Django application in a tests directory:

my_project/apps/my_app/
├── __init__.py
├── tests
│   ├── __init__.py
│   ├── field_tests.py
│   └── storage_tests.py
├── urls.py
├── utils.py
└── views.py

The Django test runner requires that I put a suite() function in the __init__.py file of my application's tests directory. That function returns the test cases that will run when I do $ python manage.py test

I installed django-nose. When I try to run the tests with django-nose, 0 tests are run:

$ python manage.py test <app_name>

If I point directly at the test module, the tests are run:

$ python manage.py test my_project.apps.my_app.tests.storage_tests

Why does django-nose's test runner not find my tests? What must I do?


Source: (StackOverflow)

Run all Tests in Directory Using Nose

I need to be able to run all tests in the current directory by typing one line in the Linux shell. In some directories this works fine. But in others, when I type "nosetests" no tests are run. The tests will run if I call for them individually but I need them to all run automatically. Here is one of the directories that won't work:

/extwebserver
    __init__.py
    test_Detection.py
    test_Filesystem.py
    test_Hardware.py
    ...

When I run "nosetests" in the parent directory, all tests in a certain subdirectory are run but no tests from /extwebserver or other subdirectories or the parent directory itself are run.

EDIT Here's the output:

matthew@Matthew-Laptop:~/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing$ nosetests -vv --collect-only
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/baseTestCase.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/run.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Detection.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Filesystem.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Hardware.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Mode.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_System.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_View.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Webserver.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/mocks/bottle.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/utils/test_timestamps.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testCamera.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testCameraManager.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testMainControllerServer.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testMode.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testPASEServerClient.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testView.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testViewsManager.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/test_commission/test_FSM.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/test_commission/test_Webserver.py is executable; skipped
test_add (testing.utils.test_config.test_config) ... ok
test_add_set (testing.utils.test_config.test_config) ... ok
test_load (testing.utils.test_config.test_config) ... ok
test_load_valid (testing.utils.test_config.test_config) ... ok
test_modify_set (testing.utils.test_config.test_config) ... ok
test_save (testing.utils.test_config.test_config) ... ok
test_update (testing.utils.test_config.test_config) ... ok
test_get_full_version (testing.utils.test_controller.test_controller) ... ok
test_get_hwaddr (testing.utils.test_controller.test_controller) ... ok
test_get_ip (testing.utils.test_controller.test_controller) ... ok
test_get_netmask (testing.utils.test_controller.test_controller) ... ok
test_get_version (testing.utils.test_controller.test_controller) ... ok
test_FloatEncoder (testing.utils.test_fileio.test_fileio) ... ok
test_read_file (testing.utils.test_fileio.test_fileio) ... ok
test_read_json_string (testing.utils.test_fileio.test_fileio) ... ok
test_system_to_text (testing.utils.test_fileio.test_fileio) ... ok
test_write_file (testing.utils.test_fileio.test_fileio) ... ok
test_get_hwaddr (testing.utils.test_netdev.test_netdev) ... ok
test_get_ip (testing.utils.test_netdev.test_netdev) ... ok
test_get_netmask (testing.utils.test_netdev.test_netdev) ... ok
test_set_datetime (testing.utils.test_syncdatetime.test_syncdatetime) ... ok
test_cast (testing.utils.test_tools.test_tools) ... ok
test_get_image (testing.testdetection.test_CameraGuardian.test_CameraGuardian) ... ok

----------------------------------------------------------------------
Ran 23 tests in 0.003s

Source: (StackOverflow)

How do you run nosetest from pycharm?

How do you execute nosetest from pycharm to run all unit tests?

I know that pycharm supports python's unittest and py.test and that they will properly support nosetests in pycharm 1.1 but I was wondering if there was a work around.


Source: (StackOverflow)