testing/README_UT.md
For the C++ unit tests of Lynx, test cases are written using the Google Test testing framework. The build dependencies are managed through GN, and the test process is managed using the RTF tool.
Select an appropriate location for the test code.
Lynx follows the specification that the test code should be in the same directory as the source file. Meanwhile, it requires that the name of the test file should be named by adding the "unittests" suffix to the name of the file being tested.
Therefore, if you want to add tests to a certain source file, first check whether there is a corresponding test file in the directory where the source file is located. If it exists, you can just add tests in the corresponding file. If it doesn't exist, you need to create the corresponding test file in the same directory.
For example, if you want to test the hello.cc file, you should create a hello_unittests.cc file in the same directory.
Write test cases based on Google Test
#include "third_party/googletest/googletest/include/gtest/gtest.h"
// optional: If the capabilities of gmock need to be used.
#include "third_party/googletest/googlemock/include/gmock/gmock.h"
TEST(HelloTest, HelloWorld) {
EXPECT_EQ(1, 1);
}
unittest_set("example_testset") {
# ...
sources = [
# ....
"hello_unittests.cc",
]
# ...
}
unittest_exec("example_unittest_exec") {
deps = [ ":hello_testset" ]
}
Sometimes, you may think that hello_unittests doesn't belong to any existing test modules. In that case, please be brave and create your own testset and exec.
unittest_set("hello_testset") {
#...
sources = [
"hello_unittests.cc",
]
#...
}
unittest_exec("hello_unittest_exec") {
deps = [ ":hello_testset" ]
}
The build and run process of Lynx unit tests is managed by the RTF tool. For more details, please refer to the RTF tool README.md.
#.rtf/native-ut-lynx.template
targets({
#....
"hello_unittest_exec": {
"cwd": ".",
"owners":["YourName"],
"coverage": True,
"enable_parallel":True,
},
#....
}
)
cd your_project_path
tools/rtf/rtf native-ut run --names lynx --target hello_unittest_exec
Unit tests for all Java and Kotlin files within the Lynx project are implemented using instrumentation testing.
The Android unit test framework is based on JUnit4. You can learn how to write test cases on its official website.
IDE tools like Android Studio support running instrumentation tests with just one click. For specific solutions, you can refer to the introductions on the official website.
The RTF tool also supports the management and running of Android unit tests, and its configuration file is located in the project_root_path/.rtf/android-ut-lynx.template file.
tools/rtf/rtf android-ut run --name lynx
tools/rtf/rtf android-ut run --name lynx --rmd
The iOS unit tests of Lynx are built based on the native XCTest unit test and the Xcode compilation system of iOS, targeting all *.m files in the Lynx project.
You can check the official website and use XCTest to write test cases.
The running of Lynx iOS unit tests is implemented based on the Xcode system. Based on this, we have integrated all Lynx-related iOS unit tests in the form of source code dependencies in the LynxExplorer App. You only need to use the Xcode project of LynxExplorer to run all the tests.
source tools/envsetup.sh
tools/hab sync .
cd explorer/darwin/ios/lynx_explorer
./bundle_install.sh
open LynxExplorer.xcworkspace
View->Navigators->Tests
UTTest