Python Forum
problem with nosetests in run
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with nosetests in run
#1
hi
we have below codes:
# finctions.py
def add(a,b):
    return a+b
def subtract(a,b):
    return a-b
def multiply(a,b):
    return a*b
def division(a,b):
    if b==0:
        raise ZeroDivisionError(" can't division by zero...")
    return a/b
and
#test_func.py

'''
from:https://www.mongard.ir/courses/unittest/episode/520/unittest-module/
usage of unittest for tesing
'''

import unittest
import functions


class Functionstest(unittest.TestCase):
    def test_add(self):
        self.assertEqual(functions.add(5,4),9)
        
    def test_subtract(self):
        self.assertEqual(functions.subtract(3,5),-2)
        
    def test_multiply(self):
        self.assertEqual(functions.multiply(3,-6),-18)
        self.assertEqual(functions.multiply(0,-6),0)
        
    def test_division(self):
        self.assertEqual(functions.division(30,6),5)
        self.assertRaises(ZeroDivisionError,functions.division,-7.5,0)
        
if __name__=='__main__':
    unittest.main()
I wrote in cmd ( or Thonny system shell):
nosetests 
or
nosetests test_func.py
, but i encountered error.
for example in cmd, i have:
Error:
D:\akb_python\akb_py_projects\python_test\nose>nosetests Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "C:\python_3_11_5\Scripts\nosetests.exe\__main__.py", line 7, in <module> File "C:\python_3_11_5\Lib\site-packages\nose\core.py", line 118, in __init__ unittest.TestProgram.__init__( File "C:\python_3_11_5\Lib\unittest\main.py", line 101, in __init__ self.parseArgs(argv) File "C:\python_3_11_5\Lib\site-packages\nose\core.py", line 179, in parseArgs self.createTests() File "C:\python_3_11_5\Lib\site-packages\nose\core.py", line 193, in createTests self.test = self.testLoader.loadTestsFromNames(self.testNames) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\loader.py", line 481, in loadTestsFromNames return unittest.TestLoader.loadTestsFromNames(self, names, module) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\unittest\loader.py", line 220, in loadTestsFromNames suites = [self.loadTestsFromName(name, module) for name in names] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\unittest\loader.py", line 220, in <listcomp> suites = [self.loadTestsFromName(name, module) for name in names] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\loader.py", line 454, in loadTestsFromName return LazySuite( ^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\suite.py", line 53, in __init__ super(LazySuite, self).__init__() File "C:\python_3_11_5\Lib\unittest\suite.py", line 22, in __init__ self._tests = [] ^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\suite.py", line 106, in _set_tests if isinstance(tests, collections.Callable) and not is_suite: ^^^^^^^^^^^^^^^^^^^^ AttributeError: module 'collections' has no attribute 'Callable' D:\akb_python\akb_py_projects\python_test\nose>nosetests test_func.py Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "C:\python_3_11_5\Scripts\nosetests.exe\__main__.py", line 7, in <module> File "C:\python_3_11_5\Lib\site-packages\nose\core.py", line 118, in __init__ unittest.TestProgram.__init__( File "C:\python_3_11_5\Lib\unittest\main.py", line 101, in __init__ self.parseArgs(argv) File "C:\python_3_11_5\Lib\site-packages\nose\core.py", line 179, in parseArgs self.createTests() File "C:\python_3_11_5\Lib\site-packages\nose\core.py", line 193, in createTests self.test = self.testLoader.loadTestsFromNames(self.testNames) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\loader.py", line 481, in loadTestsFromNames return unittest.TestLoader.loadTestsFromNames(self, names, module) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\unittest\loader.py", line 220, in loadTestsFromNames suites = [self.loadTestsFromName(name, module) for name in names] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\unittest\loader.py", line 220, in <listcomp> suites = [self.loadTestsFromName(name, module) for name in names] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\loader.py", line 431, in loadTestsFromName return self.loadTestsFromModule( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\loader.py", line 359, in loadTestsFromModule return self.suiteClass(ContextList(tests, context=module)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\suite.py", line 428, in __call__ return self.makeSuite(tests, context, **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\suite.py", line 475, in makeSuite suite = self.suiteClass( ^^^^^^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\suite.py", line 159, in __init__ super(ContextSuite, self).__init__(tests) File "C:\python_3_11_5\Lib\site-packages\nose\suite.py", line 53, in __init__ super(LazySuite, self).__init__() File "C:\python_3_11_5\Lib\unittest\suite.py", line 22, in __init__ self._tests = [] ^^^^^^^^^^^ File "C:\python_3_11_5\Lib\site-packages\nose\suite.py", line 106, in _set_tests if isinstance(tests, collections.Callable) and not is_suite: ^^^^^^^^^^^^^^^^^^^^ AttributeError: module 'collections' has no attribute 'Callable' D:\akb_python\akb_py_projects\python_test\nose>
before, I had installed nose module with the command pip install nose.
what is the problem?
thanks for any guidance
Reply
#2
collections.Callable has now become collections.abc.Callable

On the nose site, I read this
Quote:Nose has been in maintenance mode for the past several years and will likely cease without a new person/team to take over maintainership. New projects should consider using Nose2, py.test, or just plain unittest/unittest2.
This is probably the cause of your problems.
akbarza likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
The nose project has not been actively maintained for several years,
and its last official release might/will not be fully compatible with newer versions as Python 3.11.5 as you use.

Better to use pytest.
Example pytest.
# functions.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

def division(a, b):
    if b == 0:
        raise ZeroDivisionError("can't division by zero...")
    return a / b
# test_func.py
import pytest
import functions

def test_add():
    assert functions.add(5, 4) == 9

def test_subtract():
    assert functions.subtract(3, 5) == -2

def test_multiply():
    assert functions.multiply(3, -6) == -18
    assert functions.multiply(0, -6) == 0

def test_division():
    assert functions.division(30, 6) == 5
    # Using pytest.raises to check for exceptions
    with pytest.raises(ZeroDivisionError):
        functions.division(-7.5, 0)
Output:
G:\div_code\pytest_stuff\test_numbers λ ls functions.py test_func.py G:\div_code\pytest_stuff\test_numbers λ pytest -v ====================================================== test session starts ====================================================== platform win32 -- Python 3.12.0, pytest-8.0.0, pluggy-1.4.0 -- C:\Python312\python.exe cachedir: .pytest_cache rootdir: G:\div_code\pytest_stuff\test_number collected 4 items test_func.py::test_add PASSED [ 25%] test_func.py::test_subtract PASSED [ 50%] test_func.py::test_multiply PASSED [ 75%] test_func.py::test_division PASSED [ 100%] ======================================================= 4 passed in 0.26s =======================================================
akbarza likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020