GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Imprecise assert — CodeQL query help documentation

Imprecise assert¶

ID: py/imprecise-assert
Kind: problem
Security severity: 
Severity: recommendation
Precision: very-high
Tags:
   - quality
   - maintainability
   - readability
Query suites:
   - python-code-quality.qls
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

The class unittest.TestCase provides a range of assertion methods. As well as the general forms assertTrue() and assertFalse() more specific forms such as assertGreaterEquals() and assertNotIn() are provided. By using the more specific forms it is possible to get more precise and informative failure messages in the event of a test failing. This can speed up the debugging process.

Recommendation¶

Replace all calls to assertTrue() and assertFalse() that do not provide a custom failure message with a more specific variant. Alternatively, provide a tailored failure message using the assertTrue(condition, message) form.

Example¶

In this example, assertTrue() and assertFalse() are used.

from unittest import TestCase

class MyTest(TestCase):
    
    
    def testInts(self):
        self.assertTrue(1 == 1)
        self.assertFalse(1 > 2)
        self.assertTrue(1 in []) #This will fail

This will make it more difficult to determine what has gone wrong when self.assertTrue(1 in []) fails. The failure message “AssertionError: False is not true” is not very helpful.

A more useful error message can be generated by changing the asserts to the more specific forms as in the following example.

from unittest import TestCase

class MyTest(TestCase):
    
    
    def testInts(self):
        self.assertEqual(1, 1)
        self.assertLessEqual(1, 2)
        self.assertIn(1, []) #This will fail

In this case, the failure message “AssertionError: 1 not found in []” is much more informative.

References¶

  • Python library reference: TestCase.assertEqual.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.