DeepSource has joined Harness. Learn more
Python logoPython/
PYL-W1503

Constant passed to unittest `assert<Bool>`PYL-W1503

Major severityMajor
Anti-pattern categoryAnti-pattern

The first argument of assertTrue and assertFalse should be a condition. If a constant is passed as a parameter, the condition will be always True, making the test meaningless.

Bad practice

import unittest


class Tests(unittest.TestCase):
    def test_something(self):
        ''' Some test '''
        self.assertTrue('My String')
import unittest


class Tests(unittest.TestCase):
    def test_something(self):
        ''' Simple test '''
        string = 'My String'
        self.assertTrue('My String' == string)

You also might accidentally use assertTrue instead of assertEqual, which also causes this issue to be raised.

Incorrect:

import unittest


class Tests(unittest.TestCase):
    def test_something(self):
        ''' Simple test '''
        some_var = 'It should be assertEqual'
        self.assertTrue('I meant assertEqual not assertTrue', some_var)

Correct:

import unittest


class Tests(unittest.TestCase):
    def test_something(self):
        ''' Simple test '''
        some_var = 'It should be assertEqual'
        self.assertEqual('I meant assertEqual not assertTrue', some_var)