In TeamCity you can adjust the conditions when a build should be marked as failed.

Common build failure conditions

To do so, in the Fail build if area specify how TeamCity should fail builds:

Advanced build failure conditions

Starting from TeamCity 7.0 you can instruct TeamCity to mark a build as failed if it has become "worse" by some of the metrics generated by the build, for example, code coverage, or artifacts size, etc. For instance, you can mark build as failed if code coverage or code duplicates number is worse than in the previous build. Another advanced build failure condition allows to mark build as fail when a certain line is met in build log.
To add such build failure condition to your build configuration click the Add build failure condition link and select it from the list:

You can disable a build failure condition temporarily or permanently at any time, even if it is inherited from a build configuration template.

Fail build on metric change

When using code examining tools in your build, like code coverage, duplicates finders, inspections and so on, your build generates various numeric metrics. For these metrics you can specify a threshold which will fail the build upon exceeding them. For example, you can instruct TeamCity to mark build as failed, if code coverage or code duplicates number is worse than in the previous build.

In general there are two ways to configure this build fail condition:

  • A build metric exceeds or is less than the specified threshold. For example: Fail build if build duration (secs) is more than 3000.
  • A build metric has become "worse" comparing to specific build by specified value. For example: Fail build if build duration (secs) is more than 300 compared to Last successful build. In this case a build will fail if it runs 300 seconds longer than the last successful build.

To compare a build metric to, you can select last successful build, last pinned build, last finished build, build with specified build number, last finished build with specified tag.

By default, TeamCity provides the wide range of build metrics: artifacts size(bytes), build duration (secs), number of classes, number of code duplicates, number of covered classes, number of covered lines, number of covered methods, number of failed tests, number of ignored tests, number of inspection errors, number of inspection warnings, number of lines of code, number of methods, number of tests, percent of block-level coverage, percent of class-level coverage, percent of line-level coverage, percent of method-level coverage, test duration (secs).

Moreover you can add your own build metric. To do so, you need to modify TeamCity's configuration file <TeamCity Data Directory>/config/main-config.xml and add the following section there:

<statisticValue key="myMetric" description="build metric for number of files"/>

So, if your build will publish value myMetric, you can use it as a criteria for a build failure.

Fail build on specific text in build log

TeamCity can inspect all lines in build log for some particular text occurrence that indicates build failure.
Lines are matched fair without time and block name prefixes which precede each message in build log representation.

To configure this build failure condition you need to specify:

Note that as well as exact text you can specify a Java Regular Expression regexp pattern to be looked for in build log.