LoginSignup
2
2

More than 5 years have passed since last update.

xUnitを使わない、シンプルで小さなテストコードの作り方

Last updated at Posted at 2018-04-16

昔、xUnitなどがあまりなかった頃、こうやってテストしていたなぁと思いだした。

サンプルコード

sample.c
int add(int a, int b)
{
    return a+b;
}

#ifdef TEST
#include <stdio.h>

#define ASSERT(expected, actual) ((expected==actual)?printf("OK\n"):printf("NG\n"))

int main(void)
{
    ASSERT( 3, add(1,2) );
    return 0;
}
#endif

cc -DTEST sample.c
としてコンパイルすれば、単一で実行できるようになるので、
動くコードが保証できる。
もちろん、数が増えるとこれでは破綻すると思う。
とはいえ、環境構築などの手間が省けるので、
ちょっと実行するぐらいならば今でも使えるのではないだろうかなぁ。

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2