Test IBANs for unit tests: valid numbers per country, checked in .NET

•4 min read
Image for Test IBANs for unit tests: valid numbers per country, checked in .NET

A random string with DE at the front is a poor payment fixture. A real customer’s IBAN is worse.

Useful IBAN test data has a defined country format and a valid checksum. It also stays behind an integration boundary that cannot send real payments. Mathematical validity does not make an account a sandbox account.

TL;DR

  • Check country-specific length and character positions as well as mod-97.
  • Germany and Ireland both use 22 characters, but their layouts differ.
  • Keep fixtures stable and use provider-issued sandbox accounts for payment integration tests.
  • A generated or published example IBAN is not guaranteed to be unassigned.

Country formats belong in the test cases

These are published example-style fixtures, not payment destinations or universally reserved test accounts:

CountryLengthExample IBANBBAN layout after the first four characters
Germany22DE893704004405320130008 bank-code digits and 10 account digits
Ireland22IE29AIBK931152123456784 bank letters, 6 branch digits and 8 account digits
Belgium16BE6853900754703412 digits, including domestic check digits

Use the German test IBAN generator, Irish test IBAN generator or Belgian page when preparing more fixtures.

Freeze the chosen values in unit tests. Generating new random values on each run usually makes failures harder to reproduce without testing anything useful about your payment workflow.

Compute the remainder without a huge integer

IBAN validation moves the first four characters to the end and converts letters to numbers: A becomes 10, through Z becoming 35. The resulting decimal number must have remainder 1 when divided by 97.

You do not need to construct that number. Keep a running remainder. For a letter, append two decimal digits by multiplying the remainder by 100.

This helper deliberately supports only Germany, Ireland and Belgium. It checks their layouts and the international checksum, not account existence or every domestic banking rule.

using System.Text.RegularExpressions;

public static class IbanChecks
{
    public static bool HasValidFormatAndChecksum(string? value)
    {
        if (value is null)
        {
            return false;
        }

        string iban = value.Replace(" ", "").ToUpperInvariant();
        if (iban.Length < 4)
        {
            return false;
        }

        string? pattern = iban[..2] switch
        {
            "DE" => @"\ADE[0-9]{20}\z",
            "IE" => @"\AIE[0-9]{2}[A-Z]{4}[0-9]{14}\z",
            "BE" => @"\ABE[0-9]{14}\z",
            _ => null
        };

        if (pattern is null || !Regex.IsMatch(iban, pattern))
        {
            return false;
        }

        int remainder = 0;
        for (int i = 0; i < iban.Length; i++)
        {
            char character = iban[(i + 4) % iban.Length];
            remainder = character is >= '0' and <= '9'
                ? (remainder * 10 + character - '0') % 97
                : (remainder * 100 + character - 'A' + 10) % 97;
        }

        return remainder == 1;
    }
}

The method accepts ordinary display spaces and lower-case letters. Tabs, line breaks and punctuation are rejected. That normalization policy is part of the contract; do not strip arbitrary characters until bad input starts passing.

Unsupported countries return false here because this is a bounded helper. A multi-country product should distinguish an unsupported country from an invalid number in its user-facing result.

Unit tests should include the wrong layout

Use xUnit with a project reference to the helper:

using Xunit;

public sealed class IbanChecksTests
{
    [Theory]
    [InlineData("DE89370400440532013000")]
    [InlineData("IE29AIBK93115212345678")]
    [InlineData("BE68539007547034")]
    [InlineData("de89 3704 0044 0532 0130 00")]
    public void Accepts_supported_fixtures(string value)
    {
        Assert.True(IbanChecks.HasValidFormatAndChecksum(value));
    }

    [Theory]
    [InlineData(null)]
    [InlineData("")]
    [InlineData("DE89370400440532013001")]
    [InlineData("IE29AIBK9311521234567")]
    [InlineData("IE31A1BK93115212345678")]
    [InlineData("BE68539007547034\n")]
    [InlineData("GB82WEST12345698765432")]
    public void Rejects_bad_or_unsupported_input(string? value)
    {
        Assert.False(IbanChecks.HasValidFormatAndChecksum(value));
    }
}

IE31A1BK93115212345678 passes the international mod-97 calculation, but has a digit where Ireland requires a bank-code letter. It is the case that catches a checksum-only implementation. The UK fixture is valid for its country but outside this helper’s supported set.

Run dotnet test. Add business-level tests separately: a valid IBAN with an unsupported currency, a beneficiary awaiting approval or an account change requiring review.

The bank still owns the account truth

A checksum cannot tell you whether an account is open, accepts your transfer scheme or belongs to the intended beneficiary. It also does not replace domestic account checks where your integration requires them.

For integration tests, use the test accounts and response scenarios documented by your payment provider. Block production credentials and endpoints in that environment. Mocking the payment boundary is safer than hoping a generated account does not exist.

The generator helps you test input handling. Your payment provider’s sandbox tests the payment contract. Keep those responsibilities separate.

Thank you for reading.

-Bruno

Author's note: I wrote the recipe and cooked the food, AI just helped with the plating.

Read more