# Testing



## Building Tests on Windows

To build and run the unit tests on Windows (without cross-compiling for ARM), perform the following steps:

- Start Visual Studio
- From the "Getting Started" view:
	+ Select "Open a Local Folder"
	+ Navigate to the CPF top-level directory on your machine
- From the "File" Menu
	+ Open -> CMake...
	+ Navigate to the CPF top-level directory on your machine
- Once the solution is loaded, CMake configuration will take place automatically.
- To build the tests, make sure the target options are set to:
	+ Local Machine
	+ x64-Debug (or appropriate for your machine)
	+ "Default"
- Build
- To run the tests:
	+ Next to "Default", select the startup target "CPF_build_machine_tests.exe"
	+ Hit the "Play" button
	
## Adding a new test to `CPF_build_machine_tests`

These instructions are a quick tutorial that will take you through creating, building, and running 
tests on the build machine.

For this tutorial, we wish to test the file `CPF/Tests/mocks/bsp_time.cpp`. This file is created as a mock/fake for the real `bsp_time.cpp`, which resides in the `bsp` folder. It exists because the `bsp` folder is excluded and not linked for the `CPF_build_machine_tests` target, but some of the modules, classes, etc. that we wish to test need some of the functionality provided by `bsp` (e.g. getting the time).

To test `CPF/Tests/mocks/bsp_time.cpp`, we create our test file: `CPF/Tests/mocks/bsp_time_tests.cpp`. The 
convention used in this repository is that the tests should live next to the file/module/compilation unit that is under test.

The new test must now be added to the appropriate CMakeLists.txt file, so it will be built. In this case, we add it to 
`CPF/Tests/mocks/CMakeLists.txt`, specifically in the `target_sources(cpf_mock_tests INTERFACE ...)` macro. In general,
each library that exists in the repository, has a corresponding test library, where tests are gathered and compiled, and
the build machine executable recipe is defined in `CPF/Tests/CMakeLists.txt`.

If a test library does not exist for the library in which the code you wish to test belongs, you will need to create one,
and it will need to be linked to the `CPF_build_machine_tests` executable, which is defined in `CPF/Tests/CMakeLists.txt`.

I always start my test with a simple FAIL, to ensure the test harness is working properly. There is a failing test to start you off in bsp_time_tests.cpp.
Upon running the test, you should see something like the following:

```
/home/laughlin/repos/mbari/cpf_h7a3/CPF/Tests/mocks/bsp_time_tests.cpp:20: error: Failure in TEST(BSPTimeFake, FirstTest)
        Message: Test Fail! Make this pass...
        CHECK_TRUE(false) failed
```
Once you have allowed that test to pass, move on to the next test, but removing `IGNORE_` from the from the front of the next `TEST` macro and re-run it.
At the time of writing, there are three tests contained in `bsp_time_tests.cpp`, which need to pass. Making them pass should be a simple exercise, as I've 
left you all the code needed.
