diff mbox series

[RFC,2/2] unit test: add basic example

Message ID 20230427175007.902278-3-calvinwan@google.com (mailing list archive)
State New, archived
Headers show
Series add an external testing library for unit tests | expand

Commit Message

Calvin Wan April 27, 2023, 5:50 p.m. UTC
Although this commit doesn't showcase unit tests running against
anything in Git, locally, I have a smaller set of Git files compiling
into a library with unit tests running against it using this C TAP
harness.

However, you can run `make ctap` to get an idea of how the output looks
and use it as a baseline to play around with the other features of the
library.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 Makefile      | 11 +++++++++++
 t/TESTS       |  1 +
 t/unit-test.c | 14 ++++++++++++++
 3 files changed, 26 insertions(+)
 create mode 100644 t/unit-test.c

Comments

Junio C Hamano April 27, 2023, 6:47 p.m. UTC | #1
Calvin Wan <calvinwan@google.com> writes:

> Although this commit doesn't showcase unit tests running against
> anything in Git, locally, I have a smaller set of Git files compiling
> into a library with unit tests running against it using this C TAP
> harness.
>
> However, you can run `make ctap` to get an idea of how the output looks
> and use it as a baseline to play around with the other features of the
> library.

My aging eyes first read it as "make crap" ;-).

Should we ove most of it to some other Makefile that lives in t/tap
and keep it outside the primary Makefile?  This is adding about half
a dozen lines to support a sample unit-test.c file to be tested, and
if we are going to have 200 of them would we add a thousand or more
lines?

As a technology demonstration, such a hello-world test may be fine,
but I would have liked to see a more realistic sample that links
with reasonably isolated part of Git codebase, perhaps testing say
functions from <quote.h>.

THanks.
Felipe Contreras May 2, 2023, 3:58 p.m. UTC | #2
Junio C Hamano wrote:
> As a technology demonstration, such a hello-world test may be fine,
> but I would have liked to see a more realistic sample that links
> with reasonably isolated part of Git codebase, perhaps testing say
> functions from <quote.h>.

I don't know if that's the case for Calvin's proposal, but at least when I
tried to do that for my proposal, one major issue is that there's no shared
library to link to, making it difficult to play along with other shared
libraries.
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 60ab1a8b4f..b67b81e312 100644
--- a/Makefile
+++ b/Makefile
@@ -3831,3 +3831,14 @@  $(FUZZ_PROGRAMS): all
 		$(XDIFF_OBJS) $(EXTLIBS) git.o $@.o $(LIB_FUZZING_ENGINE) -o $@
 
 fuzz-all: $(FUZZ_PROGRAMS)
+
+LIB_CTAP_SOURCES = t/tap/basic.c t/tap/basic.h t/tap/macros.h
+
+PHONY: ctap
+ctap:
+	cc -It -c $(LIB_CTAP_SOURCES)
+	ar -rc libtap.a basic.o
+	mkdir -p t/unit-test
+	cc -It -o t/unit-test/unit-test-t t/unit-test.c -L. -l:libtap.a
+	cc -o t/runtests t/runtests.c
+	cd t && ./runtests -l TESTS
diff --git a/t/TESTS b/t/TESTS
index e69de29bb2..4b1899a115 100644
--- a/t/TESTS
+++ b/t/TESTS
@@ -0,0 +1 @@ 
+unit-test/unit-test-t
diff --git a/t/unit-test.c b/t/unit-test.c
new file mode 100644
index 0000000000..6eceb50ee6
--- /dev/null
+++ b/t/unit-test.c
@@ -0,0 +1,14 @@ 
+#include <tap/basic.h>
+
+int unit_test() {
+	if (1 != 1)
+		return 0;
+	return 1;
+}
+
+int main(void)
+{
+	plan(1);
+	ok(unit_test(), "unit test runs successfully");
+	return 0;
+}