Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:21:49+00:00 2026-06-17T11:21:49+00:00

I have the following test that does not fail when running an especially long

  • 0

I have the following test that does not fail when running an especially long fib assert.

Tests that don’t fail properly

#!/usr/env/bin python2.7

import unittest
from fib import fib
from nose.tools import timed


def test_gen(expected, actual):
    @timed(.001)
    def test_method(self):
        return self.assertEqual(expected, actual)
    return test_method

if __name__ == '__main__':
    all_cases = {
        'user': ((fib(40), 102334155), (fib(2), 1), (fib(5), 5)),
    }
    fails = {}
    for username, cases in all_cases.items():
        class FibTests(unittest.TestCase):
            pass

        for index, case in enumerate(cases):
            test_name = 'test_{0}_{1}'.format(username, index)
            test = test_gen(case[1], case[0])
            setattr(FibTests, test_name, test)

        suite = unittest.TestLoader().loadTestsFromTestCase(FibTests)
        result = unittest.TextTestRunner(verbosity=2).run(suite)
        fails[username] = len(result.failures)
    print fails

(Slow) Fib.py Implementation

def fib(x):
    if x == 0:
        return 0
    elif x == 1:
        return 1

    return fib(x - 2) + fib(x - 1)

Tests that fail properly

import unittest
from fib import fib
from nose.tools import timed


def test_gen(expected, actual):
    @timed(.001)
    def test_method(self):
        time.sleep(.2)
        return self.assertEqual(expected, actual)
    return test_method
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:21:49+00:00Added an answer on June 17, 2026 at 11:21 am

    You are timing the wrong thing, and never actually calling your test method. You are also going to an awful lot of effort to dynamically create and add your cases to your class that does nothing but act as a container for tests when nose supports generator test cases, which would be much easier to read and follow than what you have here. Also, is this a test file or a piece of product code? If it’s a test file, then having all of that code in if __name__ == '__main__' is kind of odd, and if it is a product code file, then having the test_gen function and the unittest and nose import statements in the uncoditionally run part doesn’t make much sense. I’d recommend doing it the following way, and not trying to make the test script self-runnable; just launch it with nose.

    from fib import fib
    from nose.tools import timed
    
    fib = timed(.001)(fib)
    
    def execute(username, fib_arg, expected_output):
        result = fib(fib_arg)
        assert result == expected_output, ('%s fib(%d) got %d, expected %d'
                                           % (username, fib_arg, result, expected_output))
    
    def test_fib():
        for name, datasets in (('user', ((40, 102334155), (2, 1), (5, 5))),):
            for arg, expected in datasets:
                yield execute, name, arg, expected
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple python test script that uses Suds to call a
Suppose I have something like the following in test.cxx (and that I do the
I have some code like the following that I'm running in a debugger in
I have the following sample / test configuration that has Hudson create a directory
I have the following test that is constantly failing and I cant work out
I have a script that does something like the following... import socket hostIP=sys.argv[1] if
I have following test.pyx cdef public class Foo[object Foo, type FooType]: cdef public char*
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
suppose I have following test/ Main.py test/one/ One.py test1.txt test/two/ Two.py test2.txt What I
I have following databases: test table1 fields: id, password, name, lastname test2 table2 fields:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.