Fuzzy testing with assertNumQueries

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Django 1.3 adds 'assertNumQueries'. Here is how to do fuzzy testing 
# with this function, using a clever subclass of int.

class FuzzyInt(int):
    def __new__(cls, lowest, highest):
        obj = super(FuzzyInt, cls).__new__(cls, highest)
        obj.lowest = lowest
        obj.highest = highest
        return obj

    def __eq__(self, other):
        return other >= self.lowest and other <= self.highest

    def __repr__(self):
        return "[%d..%d]" % (self.lowest, self.highest)

class MyFuncTests(TestCase):
    def test_1(self):
        # This will fail if the number of queries is outside the range 5 to 8 inclusive
        with self.assertNumQueries(FuzzyInt(5,8)):
            my_func(some_args)

More like this

  1. Improved Pickled Object Field by taavi223 3 years, 9 months ago
  2. Django and Twill by spookylukey 5 years, 1 month ago
  3. assertQuerysetEqual by coleifer 3 years ago
  4. Pagination/Filtering Alphabetically by zain 4 years, 2 months ago
  5. RequestFactory: Easily create mock request objects, for use in testing by simon 4 years, 9 months ago

Comments

dudus (on January 20, 2011):

standard unittest already has assertAlmostEqual that seems to perform the same thing with a different approach.

#

(Forgotten your password?)