TDD_UT测试总结
编写测试用例
访问私有方法:
#define private public
#include "xxx.h"
#undef private
#define private public
#define protected public
#include "xxx.h"
#undef protected
#undef private
访问.c文件的静态方法:
#ifdef _cplusplus
extern "C" {
#include "xxx.c"
}
#endif
或
直接添加:
#include "xxx.c"
cpp文件:
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
using namespace testing::ext;
using namespace OHOS::XXX;
using namespace OHOS::Security::AccessToken;
class AVSessionControllerTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp() override;
void TearDown() override;
std::shared_ptr<AVSession> avsession_ = nullptr;
std::shared_ptr<AVSessionController> controller_ = nullptr;
static constexpr int64_t TestMicroSecond = 1000;
static constexpr int SESSION_LEN = 64;
};
void AVSessionControllerTest::SetUpTestCase()
{
g_selfTokenId = GetSelfTokenID();
AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(g_info, g_policy);
SetSelfTokenID(tokenIdEx.tokenIdExStruct.tokenID);
}
void AVSessionControllerTest::TearDownTestCase()
{
SetSelfTokenID(g_selfTokenId);
auto tokenId = AccessTokenKit::GetHapTokenID(g_info.userID, g_info.bundleName, g_info.instIndex);
AccessTokenKit::DeleteToken(tokenId);
}
void AVSessionControllerTest::SetUp()
{
OHOS::AppExecFwk::ElementName elementName;
elementName.SetBundleName("demo");
elementName.SetAbilityName("demo");
avsession_ = AVSessionManager::GetInstance().CreateSession("Application", AVSession::SESSION_TYPE_AUDIO,
elementName);
ASSERT_NE(avsession_, nullptr);
auto ret = AVSessionManager::GetInstance().CreateController(avsession_->GetSessionId(), controller_);
ASSERT_EQ(ret, AVSESSION_SUCCESS);
ASSERT_NE(controller_, nullptr);
}
void AVSessionControllerTest::TearDown()
{
avsession_->Destroy();
controller_->Destroy();
}
class XXX1: public XXX2{
public:
void XXX() override;
};
AVControllerCallbackImpl::~AVControllerCallbackImpl()
{
}
void AVControllerCallbackImpl::XXX()
{
isDestory_ = true;
}
/**
* @tc.name: Destroy001
* @tc.desc: XXX
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(AVSessionControllerTest, Destroy001, TestSize.Level1)
{
EXPECT_EQ(controller_->Destroy(), AVSESSION_SUCCESS);
EXPECT_EQ(controller_->Destroy(), ERR_CONTROLLER_NOT_EXIST);
}
gn文件:
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/test.gni")
module_output_path = "xxx/xxx"
###############################################################################
config("module_private_config") {
visibility = [ ":*" ]
include_dirs = [
"../../include/",
"../../../../../../interfaces/xxx/xxx/xxx/include/",
]
}
common_deps = [
"//third_party/googletest:gtest_main",
]
common_external_deps = [
]
ohos_unittest("AVSessionManagerTest") {
module_out_path = module_output_path
sources = [ "xxx_test.cpp" ]
configs = [ ":module_private_config" ]
deps = common_deps
external_deps = common_external_deps
}
ohos_unittest("XXXTest") {
module_out_path = module_output_path
sources = [ "avsession_controller_test.cpp" ]
configs = [ ":module_private_config" ]
deps = common_deps
external_deps = common_external_deps
}
###############################################################################
group("unittest") {
testonly = true
deps = [
":XXXTest",
]
}
###############################################################################
外部gn文件:
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
import("//foundation/xxx/xxx/config.gni")
config("xxx_client_config") {
include_dirs = [
"//xxx/include",
]
}
ohos_shared_library("xxx_client") {
install_enable = true
ipc_sources = [
"//xxx/xxx.cpp",
]
client_sources = [
]
sources = ipc_sources + client_sources
public_configs = [ ":xxx_client_config" ]
public_deps = [
"//foundation/multimedia/av_session/frameworks/common:avsession_common",
"//foundation/multimedia/av_session/utils:avsession_utils",
]
external_deps = [
]
}
编译测试用例
编译用例:
32位:
./build.sh --product-name rk3568 --build-target make_test
./build.sh --product-name rk3568 --build-target xxx_test
64位:
./build.sh --product-name rk3568 --ccache --target-cpu arm64 --build-target xxx_test
#单独生成group的测试用例
./build.sh --product-name rk3568 --build-target XXXTest
生成测试用例位置
/out/rk3568/tests/unittest/xxx
创建testcase\unittest\xxx\xxx位置如下:
D:\file_work\22.6.13(test)\testcase\unittest\xxx\xxx
将测试用例copy到session文件夹
烧镜像流程
编译镜像:
./build.sh --product-name rk3568 --ccache
镜像路径:
\out\rk3568\packages\phone\images\
rk3568烧录
D:\file_work\22.5.23(3568烧录流程)
复制测试工具文件到本地
/home/openharmony/test$ ls
将developertest和xdevice复制到D:\file_work\22.6.13(test)
修改配置文件
修改D:\file_work\22.6.13(test)\developertest\config下的user_config.xml文件,如下图
<test_cases>
<dir>D:\file_work\22.6.13(test)\testcase</dir>
</test_cases>
手动拉起av_session进程
蓝区selinux临时关闭及手动拉起av_session进程方法:
hdc_std shell setenforce 0
hdc_std shell service_control start av_session
查看进程是否起来:
hdc_std shell pgrep "av_session"
运行start.bat
双击developertest下的start.bat
输入3,回车
跑测试用例
-t [TESTTYPE]: 指定测试用例类型,有UT,MST,ST,PERF,FUZZ,BENCHMARK等。(必选参数)
-tp [TESTPART]: 指定部件,可独立使用。
-tm [TESTMODULE]: 指定模块,不可独立使用,需结合-tp指定上级部件使用。
-ts [TESTSUITE]: 指定测试套,可独立使用。
-tc [TESTCASE]: 指定测试用例,不可独立使用,需结合-ts指定上级测试套使用。
-h : 帮助命令。
run -t UT -ts AvsessionManagerTest -tc CreatSession_001
run -t UT -ts AvsessionManagerTest
run -t UT
查看运行结果
在developertest下的reports中查看运行结果
查看summary_report.html
测试结果
测试用例的结果会直接显示在控制台上,执行一次的测试结果根路径如下:
reports/xxxx-xx-xx-xx-xx-xx
测试用例格式化结果
result/
测试用例日志
log/plan_log_xxxx-xx-xx-xx-xx-xx.log
测试报告汇总
summary_report.html
测试报告详情
details_report.html
最新测试报告
reports/latest