Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Catch unit testing framework #874

Merged
merged 4 commits into from
May 15, 2017

Conversation

thk123
Copy link
Contributor

@thk123 thk123 commented Apr 26, 2017

Adds catch.hpp - a test harness for writing and running unit tests. Updated the make file so that running make test runs all the tests. I removed all the tests that either didn't compile, failing on running or
didn't terminate without some input.

Added an example catch test to demonstrate it working.

Updated one of the unit tests to use catch as a demonstration (I can do the others in the next sprint if it is deemed important).

Copy link
Contributor

@NathanJPhillips NathanJPhillips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big request is to split the changes to sharing_map.cpp into three commits, one to indent, one to move and one to actually make the changes. That way I can review the last one properly. Understand if that feels like too much work though.

CODING_STANDARD Outdated
Unit tests:
- Unit tests are written using Catch: https://github.com/philsquared/Catch/
- For large classes:
- Each file should contain tests for one function in one class
Copy link
Contributor

@NathanJPhillips NathanJPhillips Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested clarity rewording:
Create a separate file that contains the tests for each method of each class

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just edited my suggestion again, which shows that it's hard to find clear wording for this!

CODING_STANDARD Outdated
- Each file should contain tests for one function in one class
- The file should be put inside unit/class/path/class_name/function_name.cpp
- For small classes:
- Each file should contain the tests for the class
Copy link
Contributor

@NathanJPhillips NathanJPhillips Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a separate file that contains the tests for all methods of each class

CODING_STANDARD Outdated
- Each file should contain the tests for the class
- The file should be put inside unit/class/path/class_name.cpp
- Catch supports tagging, tests should be tagged with:
- [CORE] or [LONG] if the test takes a long time to run
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps explain the difference between these tags.

unit/Makefile Outdated
# Empty last line

INCLUDES= -I ../src/
INCLUDES= -I ../src/ -I.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this? It seems like a bad thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this is needed so that include paths can be relative to the root of the unit directory, is that not true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, each of the Makefiles for each CBMC module does INCLUDE= -I .. to get the src directory included.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then that should be the INCLUDES as is; the -I. seems wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to think about what the current working directory will be when the actual compile command is run and make the include paths relative to this. But if it's working already then maybe I'm wrong. If I'm not then this setting might need to be in the makefile in each sub-folder.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I understand what is going on. This makefile is directly building source files in sub-folders that include files in this folder using #include <catch.hpp>. In that case this is the right syntax. As you were.

unit/Makefile Outdated
catch: catch_entry_point$(EXEEXT)

# Run the catch tests
test-catch: catch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule should not be named for the unit test framework. unit-test or unit might be better names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically all the tests in here are unit tests, the only difference is these are catch unit tests. We could just remove the rule since I've removed tests that don't actually pass.

/// This is an example unit test to demonstrate the build system and the
/// catch unit test framework. The source code is taken from the documentation
/// of catch.
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment block is not in a standard format. If you want to include the example you probably need to DiffBlue-ise it.


// place elements
TEST_CASE("sharing map", "[core][util][sharing_map]")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this file is impossible to diff because of the change in indenting. Can you split the changes into one that does the change and then one that indents it out correctly (or one that indents it out wrongly and then one that does the change)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just learnt the magic of ?w=1 and the diff looks fine on this file.

unit/Makefile Outdated
@@ -1,5 +1,5 @@
CATCH_TESTS = catch_example.cpp \
sharing_map.cpp \
util/sharing_map.cpp \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is in the wrong commit (will mess up git blame).

unit/Makefile Outdated
miniBDD.cpp \
osx_fat_reader.cpp \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dangling files aren't great. (Yes, effectively the entire directory had been dangling.) Any chance to get those fixed instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I don't have time to fix these at the moment, I have opened an issue to resolve this: #879. Would you prefer the files to be deleted (I'll attach them to the issue) so they aren't dangling?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for logging #879; I think it's good enough this way, no need to delete files.

@thk123 thk123 force-pushed the feature/unit-test-framework branch from 420babb to e9a6824 Compare April 28, 2017 11:38
@thk123
Copy link
Contributor Author

thk123 commented Apr 28, 2017

I have applied Nathan's suggestions and fixed the linting errors. Hopefully I've got the travis file right this time. I still suspect the Makefile could be improved as the way the CBMC libraries are linked in feels a bit clunky, but I don't know how to improve it. I will squash after review so easier to see what changes have been made.

@thk123 thk123 force-pushed the feature/unit-test-framework branch from e2de329 to 1a4a9ac Compare April 28, 2017 11:44
@NathanJPhillips
Copy link
Contributor

Do remember to ignore your executable output in a local .gitignore.

@thk123
Copy link
Contributor Author

thk123 commented Apr 28, 2017

.gitignore

Done

Rename exe

Done

Assuming I have finally got the travis and appveyor things correct - I will rebase this on Tuesday.

@thk123 thk123 force-pushed the feature/unit-test-framework branch from 48d6e70 to 22a0505 Compare May 3, 2017 11:15
@tautschnig tautschnig mentioned this pull request May 3, 2017
@thk123
Copy link
Contributor Author

thk123 commented May 4, 2017

@forejtv This is failing on Mac because of your unit test - could you investigate? https://travis-ci.org/diffblue/cbmc/jobs/228295412#L2395

Still investigating why the Windows build is failing, it is something to do with the sharing map so I might just pull that into a separate PR so can get this merged.

@thk123 thk123 force-pushed the feature/unit-test-framework branch from 22a0505 to 2d5bac3 Compare May 4, 2017 11:59
@thk123 thk123 mentioned this pull request May 4, 2017
@thk123
Copy link
Contributor Author

thk123 commented May 4, 2017

I have dropped the commits relating to the sharing_map unit tests into a separate PR: #887

Assuming @forejtv can address the Mac issue this should now pass CI.

@thk123 thk123 force-pushed the feature/unit-test-framework branch from 2d5bac3 to 66bccf0 Compare May 5, 2017 13:33
@thk123
Copy link
Contributor Author

thk123 commented May 5, 2017

The windows one is now also failing on the unicode test

@thk123 thk123 force-pushed the feature/unit-test-framework branch 2 times, most recently from fb3ccdf to ca6e2b3 Compare May 8, 2017 09:17
@thk123
Copy link
Contributor Author

thk123 commented May 8, 2017

As discussed with @forejtv I have removed the unicode tests (that failed on Mac, noted on #879) and this now builds so I think should be mergable.

Copy link
Contributor

@NathanJPhillips NathanJPhillips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of minor suggestions but otherwise good to merge.

CODING_STANDARD Outdated
- For large classes:
- Create a separate file that contains the tests for each method of each
class
- The file should be put inside unit/class/path/class_name/function_name.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file should be named according to unit/class/path/class_name/function_name.cpp

CODING_STANDARD Outdated
- Create a separate file that contains the tests for all methods of each
class
- The file should be put inside unit/class/path/class_name.cpp
- Catch supports tagging, tests should be tagged with:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with all the following tags:

unit/.gitignore Outdated
float_utils
json
miniBDD
osx_fat_reader
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all of these still created?

@thk123 thk123 force-pushed the feature/unit-test-framework branch from ca6e2b3 to a4af717 Compare May 8, 2017 10:29
@thk123
Copy link
Contributor Author

thk123 commented May 8, 2017

@NathanJPhillips Thanks, fixed.

@thk123
Copy link
Contributor Author

thk123 commented May 9, 2017

I think the Makefile might still not be correct as if I clean CBMC then do a clean build of the unit, it appears to try and build the unit tests in parallel to the CBMC directory. I think the all rule should be:

all: cprover.dir
	$(TESTS)

Does this make sense to someone who know's make files?(!) It appears to be work for a clean rebuild.

As an aside, would you expect make -C unit clean to clean the src directory as well?

@tautschnig
Copy link
Collaborator

A rule all: cprover.dir $(TESTS) will make all depend on both cprover.dir and $(TESTS), but will not yield any dependency or order between them. You'll likely need an additional rule $(TESTS): cprover.dir.

@thk123 thk123 force-pushed the feature/unit-test-framework branch from a4af717 to 3b79d6c Compare May 9, 2017 11:48
@thk123
Copy link
Contributor Author

thk123 commented May 9, 2017

@tautschnig This didn't work exactly (by adding that dependency, the $(LINKBIN) was trying to compile cprover.dir which makes no sense. In the end we did:

all: cprover.dir
	$(MAKE) $(MAKEARGS) $(TESTS)

thk123 added 4 commits May 10, 2017 09:12
Adds catch.hpp - a test harness for writing and running unit tests.
Updated the make file so that running `make test` runs all the tests. I
removed all the tests that either didn't compile, failing on running or
didn't terminate without some input.

Added an example catch test to demonstrate it working.
Added running unit tests to the Travis and AppVeyor yml files
Moved the sharing map test into the appropriate folder
@thk123 thk123 force-pushed the feature/unit-test-framework branch from 3b79d6c to 760e1fc Compare May 10, 2017 08:12

Module: Example Catch Tests

Author: DiffBlue Limited. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to put your name as author until we have a common copyright header.

unit/Makefile Outdated

CLEANFILES = $(SRC:.cpp=$(EXEEXT))
TESTS = catch_entry_point$(EXEEXT) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to specify which tests (not) to run?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the Catch tests get compiled into one executable, so the only way to remove them will be to modify the source of the test or remove them from the SRC list above I believe.

unit/Makefile Outdated
@@ -1,6 +1,6 @@
.PHONY: all cprover.dir test

SRC = catch_entry_point.cpp \
SRC = unit_tests.cpp \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to specify the tests to build and execute in a way that does not require to add them in three different places?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that the test is catch_example, which is only specified once, in SRC. That's where extra tests will be added. The other executables here will be migrated to the Catch framework over time and be removed from this makefile.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the plan to put all tests into a single cpp file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple cpp files, since executable is my understanding.

@peterschrammel
Copy link
Member

Ok, #887 should then replace the dummy catch_example. Or better integrate #887 into this PR to have at least one real use case. The dummy example is then obsolete and need not be committed into the code base.

@peterschrammel
Copy link
Member

Since the cleanup of #887 will take a considerable amount of time, I suggest to get this merged as is, and do the porting of the existing tests in follow-up PRs.

@kroening kroening merged commit 7e1ba16 into diffblue:master May 15, 2017
@thk123 thk123 deleted the feature/unit-test-framework branch May 16, 2017 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants