Closed alcjzk closed 9 months ago
@lsileoni @lanximaomao This one doesn't need too thorough of a review, since the changes are mainly testing related. Do let me know however if you have anything in the functionality you think should be changed / if you find any clear issues / have any general questions about how all this works. This pr is also not against master, but stacked on the routing one
Summary
This PR adds an automated framework for unit-testing, as well as a completely redone Makefile to support it, with some additional quality-of-life features.
Makefile changes
The primary differences in the debug build are the following CFLAGS:
-g
debug symbols-O0
disable optimizations-D TEST
defines the TEST feature macro, enabling testing code & leaks call on exit-D LOGLEVEL_INFO
enables info-level log messagesRelease build in contrast sets flags as:
-O3
optimization level 3-Wpedantic
pedantic error messages-D LOGLEVEL_ERR
log only errorsPrimary rules of the Makefile are now as follows:
make
/make all
- builds all targets, (bin/debug, bin/release, bin/test, ./webserv)make webserv
- builds bin/release and copies it into the project rootmake debug
/make d
- builds bin/debug (development build)make release
- release buildmake test
/make t
- builds and runs unit testsmake run
/make r
- builds and runs the "RUN_TARGET" specified in the makefile, (debug by default)make fmt
- Runsclang-format
on all.hpp
/.cpp
files insidesrc
andtest
fclean
,clean
,re
Unit testing
The project now contains a basic python script that finds all test functions inside the project and generates a main in order to run those tests. The make rule
make test
ormake t
as a shortcut, can be used to build and run all tests in the project. This rule is also executed in the PR workflow build job.How to write a test?
There's a small working example in
Reader.hpp
/Reader.cpp
. Intended use works as follows:Foo
you'd like to unit test, inside the same source/header files & after the definitions forFoo
, insert a preprocessor feature block with#ifdef TEST
and#endif
(both in source and header). This will ensure the testcode is not included in therelease
build.FooTest
that inherits fromFoo
.testutils.hpp
.static void
functions, with a name ending with_test
. The python script will automatically find and call these functions when tests are ran.The test functions should look somewhat like this: