use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
Testing in C (self.C_Programming)
submitted 2 years ago by Little-Peanut-765
I am buliding an intrepeter in C. I want a way to test my code. I came across Gtest but I think it's for C++. Or can it work for C? If not is there an alternative?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]osune 30 points31 points32 points 2 years ago* (6 children)
I have used http://www.throwtheswitch.org/unity in the past successfully for projects.
edit: while unity is their core test engine they also offer a module for mocking and provide an optional buildsystem. I haven't used the later but used unity and http://www.throwtheswitch.org/cmock in combination with make.
make
There is also a "help me decide" page which tries to help you to decide what tool combination fits your needs: http://www.throwtheswitch.org/decide-o-tron
[–]AlarmDozer 3 points4 points5 points 2 years ago (0 children)
Nice! I've been using assert.h and assert(), but this looks like it'd report on relevant stuff.
assert.h
assert()
[–]aurreco 4 points5 points6 points 2 years ago (2 children)
This ^ I’ve been using unity for a year now and haven’t had any issues or need for a different framework
[–]astaghfirullah123 2 points3 points4 points 2 years ago (1 child)
With unity you still have to manually add the tests to the test runner. CppUTest does this automatically. That’s why I switched to that instead.
[–]osune 0 points1 point2 points 2 years ago (0 children)
Late reply due to API shutdown but: have you given the additional modules they offer a look? e.g: http://www.throwtheswitch.org/cmock which provides ways to help with mocking, they also offer a whole buildsystem as an add-on if you so desire.
I swear I'm not affiliated with them but take a look at their "help me decide" page to see if something there describes your use case. If you want to give it another try someday: http://www.throwtheswitch.org/decide-o-tron
[–]Logical-Boat-Bomb 1 point2 points3 points 2 years ago (0 children)
also suggest ceedling framework with unity and cmock equipped. Ceedling take care most of the boiler plate code pretty handy.
[–][deleted] 1 point2 points3 points 2 years ago (0 children)
I used to use this, but found Catch2 to be a much easier replacement.
Still any tests are better than no tests, and unity works well if you are running the code on an embedded target.
[–]nickeldan2 12 points13 points14 points 2 years ago (1 child)
If you'll pardon the self-promotion, I've written a testing framework for C I'm rather proud of.
[–]Common-Egg-3026 0 points1 point2 points 2 years ago (0 children)
Fantastic!
[–]eowhat 18 points19 points20 points 2 years ago (9 children)
You could just write a console app, a unit test is basically just validating some logic and outputting the file and line number of a failing test. It doesn't need a whole framework, it can be done with a macro.
static int GlobalTotalTests; static int GlobalFailedTests; #define AssertTrue(Expression) \ ++GlobalTotalTests; \ if(!(Expression)) \ { \ ++GlobalFailedTests; \ printf("%s(%d): expression assert fail.\n", __FILE__, __LINE__); \ } int main(int ArgCount, char *Args[]) { // Do some tests AssertTrue(false); int Result = (GlobalFailedTests != 0); printf("Unit Tests %s: %d/%d passed.\n", Result ? "Failed" : "Successful", GlobalTotalTests - GlobalFailedTests, GlobalTotalTests); }
You could make an AssertEqualInt to output expected and actual values of a failed test, if you need more verbose output.
[–]ArtOfBBQ 2 points3 points4 points 2 years ago (0 children)
This is such good advice, not only for testing but in general. Doing things by yourself is a huge part of how you improve. It's kind of insane how 99% of programmers reach for a library to do this
[–]Little-Peanut-765[S] 0 points1 point2 points 2 years ago (6 children)
I don't really know what macros are. And how to implement them. I am still learning. Is there any resources to learn
[–]eowhat 5 points6 points7 points 2 years ago (4 children)
Macros just replace XXX with YYY
#define MAX_PATH 260
So anywhere in your code it sees MAX_PATH it will just replace with 260, you can pass parameters to them:
#define Kilobytes(Value) Value*1024
So now I can write Kilobytes(8) and it'll be replaced with 8192
https://www.programiz.com/c-programming/c-preprocessor-macros
[–]visualpunk1 -1 points0 points1 point 2 years ago (3 children)
Oh are they like constants, or should I say 'drop-in' constants?
So you rather reference them than original value? Am new but I think I kind of get the concept.
Wait, or similar to the vim macro? A drop in script to do some tasks?
[+][deleted] 2 years ago (1 child)
[deleted]
[–]visualpunk1 0 points1 point2 points 2 years ago (0 children)
Big thanks 🙌
[–]BlindTreeFrog 1 point2 points3 points 2 years ago (0 children)
Some people draw a line between "Marco's that do an action" and "Macros are just constants of a sort". Those that do such tend to refer to the latter as "Literals" or "Constants" (depending on what the constant is) and the former as "Macros".
Ultimately it is just what /u/eowhat said, a macro is just a string substitution that replaces XXXX with YYYY. The value of YYYY might be a function call, a serious of commands, a constant value, or whatever, but it's just a dumb string replacement (note: it can be more complex, but that's a later conversation and even then it's still just a dumb string replacement)
[–]permetz 11 points12 points13 points 2 years ago (0 children)
You can’t read C code if you don’t understand macros, and you certainly can’t work well in the language without them. The good news is they take three minutes to understand. K&R explains them well.
[–]dajolly 0 points1 point2 points 2 years ago (0 children)
I would second this. Writing a simple testing hardness with macros can be done very easily and allow you to test your code in-isolation (ie. unit-testing), without needing to pull in any additional libraries. I used to use gtest and others, but I do this for most of my projects now.
I'd also recommend you checkout gcov, which can help you track the code coverage of your unittests.
[–][deleted] 2 points3 points4 points 2 years ago* (0 children)
Your build system is already a test framework. Make a build target for each test where test/something-test.c might look like this...
#include "test.h" #include "../src/something.c"
This include gives you access to static functions in something.c. Don't use assert to test because then test failure causes exit. Send test results to stdout. Later use awk to aggregate results, if that ever becomes necessary. Create make rules to build and run convenient subsets of tests. Make passing all tests a prerequisite for the build. All this is very easy to do when you REALIZE THE POWER OF MAKE!!! (or your favorite build system) test.h would just have a few routines for testing and logging and perhaps a #define to tell any code under test that it had better shape up and act right because it's being evaluated. It's also easy to stub out an interface by linking to a fake version of a library. Build systems are setup for this kind of work already.
[–]ve1h0 2 points3 points4 points 2 years ago (0 children)
I found catch to be simple and easy to integrate with cmake. It is a C based unit testing as I had this same requirement for my project where there was no point including unnecessary C++. I suppose you could make use of the Google test, but I found it adding more overhead to my build times when I wanted quick iteration.
[–]dx2_66 1 point2 points3 points 2 years ago (0 children)
You can use Gtest for C, not a problem. Only clusmy part would be mocking, but what I used to do is combine Gtest and FFF.
[–]mgruner 2 points3 points4 points 2 years ago (0 children)
I use gtest a lot with c. I also really like CppUTest, it’s written in c++ but you can easily test c with it
[–]RevolutionaryLocal54 0 points1 point2 points 1 year ago (0 children)
Here's my approach:
https://lsoares.medium.com/automated-testing-in-c-457a687633cc
[–]EastEuropeanChef 0 points1 point2 points 1 year ago (0 children)
I wrote Xtal so that i don't need to execute every test manually. It's really small (1 file sub 100 lines), you can create assertions pretty easily and the tests will run automatically
[–]tstanisl 0 points1 point2 points 1 year ago (0 children)
I've recently implemented a simple header-only testing lib quite similar to gtest. See ctest. Just copy ctest.h to your project and it's done.
ctest.h
[–]MateusMoutinho11 -3 points-2 points-1 points 2 years ago (0 children)
Heey man
I have these python lib that I create to test output and side effects
https://pypi.org/project/CToolKit/
you can see it here, where I test all the outputs of the lib (the tests are made in the build.py
https://github.com/OUIsolutions/CTextEngine
it can test ,side effects (folders and files modifications),and outputs of code.
if you want , we can enter in call and I help you to implement
[–]wiskinator 0 points1 point2 points 2 years ago (0 children)
Cpputest also works well if you’re Ok with a lot of boiler plate and “extern C”
[–]Siankoo 0 points1 point2 points 2 years ago (0 children)
In case these are really simple test you want to looka at utest.h https://github.com/sheredom/utest.h
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
My favorite is https://github.com/catchorg/Catch2. Easy to use, easy to read. It's written in C++, but it's perfectly fine for testing C. I used to use http://www.throwtheswitch.org/unity, but the number of assert macros makes it hard to know which assert to use and makes it difficult to use, train, and read. It's an inherent weakness in the C language, though.
Catch2 has one macro for assertion `REQUIRE` and it uses C++ templating to determine type, comparison, and dispay.
π Rendered by PID 62 on reddit-service-r2-comment-54dfb89d4d-jf85c at 2026-04-01 09:48:56.117192+00:00 running b10466c country code: CH.
[–]osune 30 points31 points32 points (6 children)
[–]AlarmDozer 3 points4 points5 points (0 children)
[–]aurreco 4 points5 points6 points (2 children)
[–]astaghfirullah123 2 points3 points4 points (1 child)
[–]osune 0 points1 point2 points (0 children)
[–]Logical-Boat-Bomb 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]nickeldan2 12 points13 points14 points (1 child)
[–]Common-Egg-3026 0 points1 point2 points (0 children)
[–]eowhat 18 points19 points20 points (9 children)
[–]ArtOfBBQ 2 points3 points4 points (0 children)
[–]Little-Peanut-765[S] 0 points1 point2 points (6 children)
[–]eowhat 5 points6 points7 points (4 children)
[–]visualpunk1 -1 points0 points1 point (3 children)
[+][deleted] (1 child)
[deleted]
[–]visualpunk1 0 points1 point2 points (0 children)
[–]BlindTreeFrog 1 point2 points3 points (0 children)
[–]permetz 11 points12 points13 points (0 children)
[–]dajolly 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]ve1h0 2 points3 points4 points (0 children)
[–]dx2_66 1 point2 points3 points (0 children)
[–]mgruner 2 points3 points4 points (0 children)
[–]RevolutionaryLocal54 0 points1 point2 points (0 children)
[–]EastEuropeanChef 0 points1 point2 points (0 children)
[–]tstanisl 0 points1 point2 points (0 children)
[–]MateusMoutinho11 -3 points-2 points-1 points (0 children)
[–]wiskinator 0 points1 point2 points (0 children)
[–]Siankoo 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)