test: add automatic unit test coverage generation

Using lcov, generate an HTML report via "make coverage-report"
describing the coverage of each file. Ideally, this will get added to
an automated testing pipeline.

Signed-off-by: Max Regan <mgregan2@gmail.com>
This commit is contained in:
2017-04-02 22:28:03 -07:00
parent d9bf91fc5a
commit d25d2d8549

View File

@@ -1,4 +1,5 @@
OUT=build
COV_DIR=$(OUT)/coverage/
SRC_DIR=src
APP_DIR=src/apps/
@@ -55,10 +56,8 @@ gbasm: $(OUT)/apps/gbasm
.PHONY: gbasm-test sample-test tests
gbasm-test: $(OUT)/apps/gbasm-test
sample-test: $(OUT)/apps/sample-test
tri-test: $(OUT)/apps/tri-test
tests: gbasm-test sample-test tri-test
TESTS=$(OUT)/apps/gbasm-test $(OUT)/apps/tri-test
tests: $(TESTS)
$(ALL_APPS) $(ALL_OBJS): $(CONFIG)
@@ -73,7 +72,48 @@ $(APPS): $(OBJS) $$@.o
@$(shell mkdir -p $(@D))
@$(CC) $^ $(LDFLAGS) -o $@
.PHONY: clean realclean
.PHONY: check cov-report
define run_tests =
@for i in $(TESTS); do \
$$i; \
if [ $$? -ne 0 ]; then \
exit 1; \
fi; \
done;
endef
.PHONY: check coverage coverage-report
check: $(TESTS)
$(run_tests)
COV_BASE=$(COV_DIR)/cov_base.info
.PHONY: $(COV_BASE)
$(COV_BASE):
@mkdir -p $(COV_DIR)
@lcov -c -i -d $(OUT) -o $(COV_BASE)
$(COV_DIR)/coverage.info coverage: $(TESTS) $(COV_BASE)
@for test in $(TESTS); do \
$$test; \
lcov -c -d $(OUT) -o $(COV_DIR)/$$(basename $$test).info -t $$(basename $$test); \
done
lcov $$(for i in $$(ls -1 $(COV_DIR)/*.info); do echo "-a $$i"; done) -o $(COV_DIR)/coverage.info
coverage-report: coverage
@mkdir -p $(COV_DIR)/html/
@genhtml --prefix $(PWD) --output-directory=$(COV_DIR)/html $(COV_DIR)/coverage.info
.PHONY: clean realclean cov-clean
cov-clean:
@find $(OUT)/ \( -iname *.gcno -o -iname *.gcda \) -delete
@rm
clean:
@rm -f $(ALL_OBJS) $(APPS)