No Literal Bool In Assertion

ID

csharp.no_literal_bool_in_assertion

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

CSharp

Tags

assertion, reliability, testing

Description

Reports a boolean assertion whose condition is a literal that makes it always pass — Assert.True(true), Assert.False(false), Assert.IsTrue(true), Assert.That(true). The condition is fixed at compile time, so the assertion says nothing about the code under test and the test reports success unconditionally.

Only the static assertion facades of xUnit, NUnit and MSTest are considered, and only their condition-taking forms, so a helper of one’s own named IsTrue and the unrelated Debug.Assert(true) are not reported.

The opposite literal — Assert.True(false), Assert.False(true), Assert.That(false) — is not reported: it always fails instead, which is the idiomatic stand-in for Assert.Fail() inside a catch block or a branch that should be unreachable, typically paired with a message explaining why execution should never get there.

This rule applies to test code only.

Rationale

Assert.True(true) always passes, so it occupies the place where the real check should be and makes the test report success unconditionally regardless of what the code under test did. The usual causes are a condition replaced by a constant while narrowing down a failure and never restored, and a generated or copied test whose condition was left as a placeholder.

[Test]
public void AccountIsActive()
{
    var account = new Account();
    account.Activate();
    Assert.True(true);                          // FLAW - passes whatever Activate did
}

[Test]
public void AccountIsNotFrozen()
{
    Assert.False(false);                        // FLAW - the same defect written the other way
}

[Test]
public void AccountStartsActive()
{
    var account = new Account();
    Assert.True(account.IsActive);              // OK
}

[Test]
public void PerThread()
{
    try
    {
        RunOnAnotherThread();
    }
    catch (Exception ex)
    {
        Assert.True(false, ex.ToString());      // OK - deliberate failure marker, not a defect
    }
}

[Test]
public void BalanceMatches()
{
    var account = new Account();
    Assert.That(account.Balance, Is.EqualTo(0)); // OK - the literal is the expected value
}

[Test]
public void InvariantHolds()
{
    var account = new Account();
    Debug.Assert(true);                          // OK - not a test assertion
    Assert.True(account.IsActive);
}

Remediation

Replace the literal with the condition the test was written to check — the value the code under test returned, or the state it left behind. If the assertion was a placeholder in an unfinished test, either finish it or mark the test as pending so the suite stops counting it as coverage.