-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Update tests, and add documentation! #7506
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7085c79
initial commit
strega-nil 2af82aa
Merge remote-tracking branch 'upstream/master' into docs
strega-nil fe6217b
testing!
strega-nil 7a26595
add testing documentation
strega-nil 466b780
add testing.md to index
strega-nil fc364cc
add maintainer guidelines to tool maintainer help
strega-nil f8f38c0
more comments yay
strega-nil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
Testing | ||
======= | ||
|
||
Testing vcpkg is important whenever one makes changes to the tool itself, and | ||
writing new tests and keeping them up to date is also very important. If one's | ||
code is subtly broken, we'd rather find it out right away than a few weeks down | ||
the line when someone complains! | ||
|
||
Running Tests | ||
------------- | ||
|
||
Before anything else, we should know whether you can actually run the tests! | ||
All you should need is a way to build vcpkg -- anything will do! All you have to | ||
do is follow the guide 😄 | ||
|
||
With `$VCPKG_DIRECTORY` being the directory where you have cloned vcpkg, create | ||
a build directory in `$VCPKG_DIRECTORY/toolsrc` (commonly named `out`), and | ||
`cd` into it. Make sure to clean it out if it already exists! | ||
|
||
```sh | ||
$ cmake .. -DCMAKE_BUILD_TYPE=Debug -G Ninja | ||
$ cmake --build . | ||
$ ./vcpkg-test # ./vcpkg-test [$SPECIFIC_TEST] for a specific set of tests | ||
$ # i.e., ./vcpkg-test [arguments] | ||
``` | ||
|
||
If you make any modifications to `vcpkg`, you'll have to do the | ||
`cmake --build .` step again. | ||
|
||
Writing Tests | ||
------------- | ||
|
||
In your journey to write new tests, and to modify existing tests, reading the | ||
[Catch2 documentation] will be very helpful! Come back after reading those 😀 | ||
|
||
You'll want to place your tests in one of the existing files, or, if it doesn't | ||
belong in any of those, in a [new file](#adding-new-test-files). | ||
|
||
The layout of these tests is as follows: | ||
|
||
```cpp | ||
// ... includes | ||
|
||
TEST_CASE("Name of test", "[filename without the .cpp]") { | ||
// setup and the like | ||
REQUIRE(some boolean expression); | ||
} | ||
|
||
// etc. | ||
``` | ||
|
||
You want to give these test cases good, descriptive, unique names, like | ||
`SourceParagraph construct minimum` -- it doesn't need to be extremely clear | ||
english, and shorthand is good, but make sure it's clear what the test is from | ||
the name. For the latter parameter, known as "tags", you should at least put the | ||
name of the file which the test case is in -- e.g., in `arguments.cpp`, you'd | ||
tag all of the test cases with `[arguments]`. | ||
|
||
If you wish to add helper functions, make sure to place them in an anonymous | ||
namespace -- this will ensure that they don't trample over anybody else's | ||
space. Additionally, there are a few helper functions that live in | ||
`<vcpkg-test/util.h>` and `src/vcpkg-test/util.cpp` -- make sure to look into | ||
them so that you're not rewriting functionality. | ||
|
||
That should be all you need to know to start writing your own tests! | ||
Remember to check out the [Catch2 documentation] | ||
if you'd like to get more advanced with your tests, | ||
and good luck on your testing journey! | ||
|
||
Adding New Test Files | ||
--------------------- | ||
|
||
Adding new test files should be easy and straightforward. All it requires is | ||
creating a new source file in `toolsrc/src/vcpkg-test`, and then rerunning | ||
`CMake` in order to pick up the glob changes. | ||
|
||
### Example | ||
|
||
Let's try writing a new test file called `example` (very creative, I know). | ||
|
||
First, we should create a file, `example.cpp`, in `toolsrc/src/vcpkg-test`: | ||
|
||
```cpp | ||
// vcpkg-test/example.cpp | ||
#include <vcpkg-test/catch.h> | ||
``` | ||
|
||
This is the minimum file needed for tests; let's rebuild our CMake directory. | ||
You'll have to clean out the existing `out` directory for CMake to rerun | ||
globbing. | ||
|
||
```sh | ||
$ cmake .. -DCMAKE_BUILD_TYPE=Debug -G Ninja | ||
# ... | ||
-- Build files have been written to: $VCPKG_DIRECTORY/toolsrc/out | ||
$ cmake --build . | ||
[80/80] Linking CXX executable vcpkg.exe | ||
``` | ||
|
||
Okay, now let's make sure this worked; add a test case to `example.cpp`: | ||
|
||
```cpp | ||
TEST_CASE("Example 1 - fail", "[example]") { | ||
REQUIRE(false); | ||
} | ||
``` | ||
|
||
Now build the tests again, and run them: | ||
|
||
```sh | ||
$ cmake --build . | ||
[2/2] Linking CXX executable vcpkg-test.exe | ||
$ ./vcpkg-test | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
vcpkg-test.exe is a Catch v2.9.1 host application. | ||
Run with -? for options | ||
|
||
------------------------------------------------------------------------------- | ||
Example 1 - fail | ||
------------------------------------------------------------------------------- | ||
$VCPKG_DIRECTORY/toolsrc/src/vcpkg-test/example.cpp(3) | ||
............................................................................... | ||
|
||
$VCPKG_DIRECTORY/toolsrc/src/vcpkg-test/example.cpp(14): FAILED: | ||
REQUIRE( false ) | ||
|
||
=============================================================================== | ||
test cases: 102 | 101 passed | 1 failed | ||
assertions: 3611 | 3610 passed | 1 failed | ||
``` | ||
|
||
Hopefully, that worked! It should compile correctly, and have one failing test. | ||
Now let's try a more complex test, after deleting the old one; | ||
|
||
```cpp | ||
// add #include <vcpkg/base/strings.h> to the top of the file | ||
namespace Strings = vcpkg::Strings; | ||
|
||
TEST_CASE("Example 2 - success", "[example]") { | ||
std::string hello = "Hello"; | ||
REQUIRE(Strings::case_insensitive_ascii_equals(hello, "hELLo")); | ||
REQUIRE_FALSE(Strings::case_insensitive_ascii_starts_with(hello, "E")); | ||
} | ||
``` | ||
|
||
Now compile and build the tests, and this time let's only run our example tests: | ||
|
||
```sh | ||
$ cmake --build . | ||
[2/2] Linking CXX executable vcpkg-test.exe | ||
$ ./vcpkg-test [example] | ||
Filters: [example] | ||
=============================================================================== | ||
All tests passed (2 assertions in 1 test case) | ||
``` | ||
|
||
Hopefully you have one test running and succeeding! If you have that, you have | ||
strega-nil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
succeeded at adding a new file to vcpkg's tests. Congratulations! Have fun on | ||
the rest of your journey 🐱👤😁 | ||
|
||
[Catch2 documentation]: https://github.com/catchorg/Catch2/blob/master/docs/tutorial.md#top |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.