-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Unit testing #2536
Unit testing #2536
Conversation
Wow! This is wonderful. Thanks for figuring all of this out. I have not approved all yet -- I want to read and understand all of AudacityTesting.cmake. Very likely those CMake functions will need enhancement with more options. Journal tests may need to run with a certain prior environment setup, such as having a particular audacity.cfg file already in place at startup, and then the environment would need cleanup after the test. CTest gives you the means to do such things by defining setup and teardown commands as "test" too in a certain dependency graph. We may also want to associate labels with test so we can filter them and run subsets. But all that can be future work and no reason to delay this. |
39177e4
to
6d58de4
Compare
#[[ | ||
add_unit_test(NAME name SOURCES file1 ... LIBRARIES lib1 ...) | ||
|
||
Creates an executable called ${name}-test from the sources files ${file1}, ... and linked |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"source files"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be useful to add PROPERTIES prop1 ...
and pass those to: https://cmake.org/cmake/help/latest/command/set_tests_properties.html?highlight=set_tests_properties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here are all the possible properties https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#test-properties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the function will have other keyword arguments to abbreviate other combinations of properties. Don't ask me yet, what exactly. Experience will tell what is useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be easily added in the future in case we find a need for any additional ctest properties
Creates an executable called ${name}-test from the sources files ${file1}, ... and linked | ||
to libraries ${lib1}, ... Catch2 is linked implicitly. | ||
|
||
Create a CTest test called ${name}, that expects 0 code on success |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... unless one uses one of the properties:
- FAIL_REGULAR_EXPRESSION
- PASS_REGULAR_EXPRESSION
- SKIP_REGULAR_EXPRESSION
- SKIP_RETURN_CODE
- WILL_FAIL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a very strong opinion that unit test should return 0 on success
# Create test executable | ||
|
||
add_executable( ${test_executable_name} ${ADD_UNIT_TEST_SOURCES} "${CMAKE_SOURCE_DIR}/tests/Catch2Main.cpp") | ||
target_link_libraries( ${test_executable_name} ${ADD_UNIT_TEST_LIBRARIES} Catch2::Catch2 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catch2 needs to link something? I thought it is just one amalgamated header file. Or is that precisely what this line does, just adding in include path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Modern" CMake uses target_link_libraries
for header-only libraries as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it
NAME | ||
${ADD_UNIT_TEST_NAME} | ||
COMMAND | ||
${test_executable_name} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will we want arguments for COMMAND that can be given to add_unit_test?
Will we ever need to reuse a common test executable with different arguments?
Should there be a lower-level CMake function taken out of this, for invoking add_test with such options?
I'm thinking we might in future need a more complicated thing like setup and cleanup of fixtures for a test or a group of tests, in which each setup or cleanup step is itself described as a "test." https://cmake.org/cmake/help/latest/prop_test/FIXTURES_REQUIRED.html#prop_test:FIXTURES_REQUIRED
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And still, I have very that if you need to it for unit tests you have serious design issues and should reconsider them. add_unit_test
is expected to be used only to create unit tests.
${ADD_UNIT_TEST_NAME} | ||
PROPERTIES | ||
ENVIRONMENT "DYLD_FALLBACK_LIBRARY_PATH=$<SHELL_PATH:${_SHARED_PROXY_BASE_PATH}/$<CONFIG>>" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if these fixes you figured out for dynamic libraries should be reused with image-compiler too. Or have you done that already?
Maybe a common cmake function for such steps then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can be reused, but the image-compiler is a) rarely built, b) does not test the functionality of a library. For these reasons, it is left as-is for now.
${test_name} | ||
COMMAND | ||
${audacity_target} --journal ${journal_file} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again maybe use a common low function for add_test, and consider how to pass other command arguments and test options into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have finished my review.
Just some spelling mistakes in comments in cmake scripts that should be fixed immediately. Otherwise, suggestions for improvements, but nothing that needs to be done before merging this.
I'm trying it in the Xcode build but I don't yet see how to build the tests. |
Wait, there is a configuration error
I couldn't build the tests because reconfiguring at the command line failed:
|
Journal does not handle the case, when a modal dialog is displayed during the startup. In this case `CM,Exit` will fail to close Audacity.
6d58de4
to
ef3921d
Compare
Oh forget that! The directory tests was a bit of junk just on my own file system, from my own experiements with tests. |
This looks to be your local issue, lib-strings doesn't have tests directory. I think this is a leftover from the time you explored CTest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reapproval
OK now I see tests in the Xcode browser. Waiting to build and run them. |
There are all those CI build failures however |
Gosh Python world is sad :-( |
"Error: The process '/Users/runner/hostedtoolcache/Python/3.10.2/x64/bin/conan' failed with exit code 1" I was glancing quickly at that, and misread the word "headache" in there |
Some third-party library broke half of the world again... |
I have built the tests in XCode, and confirmed that build fails if I edit the test program to have a syntax error. Now I added |
Aha, I see RUN_TESTS Great, I see test outputs. Now we might consider what other CTest options or stderr messages from Audacity might make this display more understandable. |
There are different ways:
|
The journal sanity test makes a lot of spam, and so will every journal test:
Let's have an issue to clean that up |
For unit tests, avoid fixtures for sure, but for the high level journal tests, fixtures could make sense. |
But again, consider journal tests. Maybe we will want those to write stuff to stderr to be filtered, and we will want to define success and failure some other way based on outputs. @Penikov might use capabilities like that. |
Well, Like we can say |
|
🥳 |
Resolves: #2478
This PR introduces a framework for automated testing in Audacity. It is now possible to define the following types of tests:
Unit Tests
add_unit_test(NAME name SOURCES file1 ... LIBRARIES lib1 ...)
This function creates an executable called
${name}-test
from the sources files (${file1}
, ...). and linked to libraries (${lib1}
, ...).Catch2 is linked implicitly. There is no need to define a main function in the unit test.
Journal Tests
add_journal_test(journal_file)
Adds a test, that runs Audacity with the journal
${journal_file}
. The test name is based on the name component of the${journal_file}
.Both unit and journal tests are registered with the CTest framework. This PR implements unit tests for FromChars set of functions and a journal test that checks that we can run Audacity at all.
To be able to run journal tests Audacity now suppresses a few modal dialogs, shown during the startup:
For libraries, if the
libraries/(name)/tests
directory is present - CMake will include the unit tests for the library automatically.The
Build Audacity
workflow will run both unit and journal tests for each build. In the future, journal tests can be separated into a different nightly workflow.Examples
Adding a unit test:
Adding a journal test:
Running tests:
Recommended: