Skip to content

Commit

Permalink
#186 调整测试样例 可以使用unittest命令覆盖
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorReid committed Apr 3, 2024
1 parent 8a29ea9 commit 301f789
Show file tree
Hide file tree
Showing 102 changed files with 427 additions and 695 deletions.
1 change: 1 addition & 0 deletions src/sr/const/traing_mission_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, id_cn: str, desc_cn: str, able: bool):


MISSION_DAILY_MISSION = DailyTrainingMission(id_cn='日常任务', desc_cn='完成1个日常任务', able=False)
MISSION_PATH = DailyTrainingMission(id_cn='侵蚀隧洞', desc_cn='完成1次侵蚀隧洞', able=False) # 1.6之后没了
MISSION_TRA_POWER = DailyTrainingMission(id_cn='消耗开拓力', desc_cn='累计消耗120点开拓力', able=False)
MISSION_FORGOTTEN_HALL = DailyTrainingMission(id_cn='忘却之庭', desc_cn='完成1次忘却之庭', able=False)
MISSION_SYNTHESIZE_CONSUMABLE = DailyTrainingMission(id_cn='合成', desc_cn='使用1次万能合成机', able=True)
Expand Down
Binary file removed test/resources/sr/app/test_destroy_objects/1.png
Binary file not shown.
Binary file removed test/resources/sr/app/test_destroy_objects/2.png
Binary file not shown.
Binary file removed test/resources/sr/app/test_destroy_objects/3.png
Binary file not shown.
Binary file removed test/resources/sr/app/test_destroy_objects/4.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_get_sim_uni_power_and_qty(self):

op = TrailblazePower(ctx)

screen = self.get_test_image('sim_uni_power')
screen = self.get_test_image_new('sim_uni_power.png')
x, y = op._get_sim_uni_power_and_qty(screen)

self.assertEqual(92, x)
Expand Down
137 changes: 0 additions & 137 deletions test/src/test/sr/cal_pos/test_cal_pos/__init__.py
Original file line number Diff line number Diff line change
@@ -1,137 +0,0 @@
import os

import cv2

import test
from basic import Point, cal_utils
from basic.img.os import get_debug_image
from basic.log_utils import log
from sr import cal_pos, performance_recorder
from sr.cal_pos import VerifyPosInfo
from sr.const.map_const import get_region_by_prl_id
from sr.context import get_context
from sr.image.image_holder import ImageHolder
from sr.image.sceenshot import mini_map, large_map
from test.sr.cal_pos.cal_pos_test_case import TestCase, read_test_cases, save_test_cases


class TestCalPos(test.SrTestBase):

def __init__(self, *args, **kwargs):
test.SrTestBase.__init__(self, *args, **kwargs)

ih = ImageHolder()
# 预热 方便后续统计耗时
ih.preheat_for_world_patrol()
mini_map.preheat()

@property
def cases_path(self) -> str:
return os.path.join(self.sub_package_path, 'test_cases.yml')

def test_cal_pos(self):
fail_cnt = 0
self.cases = read_test_cases(self.cases_path)
for case in self.cases:
result = self.run_one_test_case(case, show=False)
if not result:
fail_cnt += 1
log.info('%s 计算坐标失败', case.unique_id)

performance_recorder.log_all_performance()
self.assertEqual(0, fail_cnt)

def test_specified_one(self):
fail_cnt = 0
self.cases = read_test_cases(self.cases_path)
for case in self.cases:
if case.unique_id != 'P04_PNKN_R06_CSSH_02':
continue
result = self.run_one_test_case(case, show=True)
if not result:
fail_cnt += 1
log.info('%s 计算坐标失败', case.unique_id)

performance_recorder.log_all_performance()
self.assertEqual(0, fail_cnt)

def test_init_case(self):
"""
从debug中初始化
:return:
"""
ctx = get_context()
file_name: str = '_1711787600419'
mm = get_debug_image(file_name)

str_list = file_name.split('_')
is_running = (bool)(str_list.pop())
pp_r = (int)(str_list.pop())
pp_y = (int)(str_list.pop())
pp_x = (int)(str_list.pop())
region_prl_id = '_'.join(str_list)

region = get_region_by_prl_id(region_prl_id)

self.cases = read_test_cases(self.cases_path)
idx: int = 1
while True:
existed: bool = False
for c in self.cases:
if c.region.prl_id != region_prl_id:
continue
if c.num == idx:
existed = True
break
if existed:
idx += 1
else:
break

case = TestCase(region, Point(pp_x, pp_y), idx, is_running, [pp_x, pp_y, pp_r])
self.cases.append(case)
self.cases = sorted(self.cases, key=lambda x: x.unique_id)
save_test_cases(self.cases, self.cases_path)
self.save_test_image(mm, case.image_name)
log.info('新增样例 %s', case.unique_id)

def run_one_test_case(self, case: TestCase, show: bool = False) -> bool:
"""
执行一个测试样例
:param case: 测试样例
:param show: 显示
:return: 是否与预期一致
"""
ctx = get_context()
ctx.init_image_matcher()

mm = self.get_test_image_new(case.image_name)

lm_info = ctx.ih.get_large_map(case.region)
possible_pos = tuple(case.possible_pos)
lm_rect = large_map.get_large_map_rect_by_pos(lm_info.gray.shape, mm.shape[:2], possible_pos)

mm_info = mini_map.analyse_mini_map(mm, ctx.im)
verify = VerifyPosInfo(last_pos=Point(case.possible_pos[0], case.possible_pos[1]),
max_distance=case.possible_pos[1])

pos = cal_pos.cal_character_pos(ctx.im,
lm_info=lm_info,
mm_info=mm_info,
running=case.running,
real_move_time=case.real_move_time,
lm_rect=lm_rect,
retry_without_rect=False,
show=show,
verify=verify
)
if show:
cv2.waitKey(0)

if pos is None:
log.error('%s 当前计算坐标为空', case.unique_id)
return False
else:
dis = cal_utils.distance_between(pos.center, case.pos)
log.info('%s 当前计算坐标为 %s 与目标点 %s 距离 %.2f', case.unique_id, pos.center, case.pos, dis)
return dis < 5
68 changes: 68 additions & 0 deletions test/src/test/sr/cal_pos/test_cal_pos/debug_cal_pos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from basic import Point
from basic.img.os import get_debug_image
from basic.log_utils import log
from sr import performance_recorder
from sr.const.map_const import get_region_by_prl_id
from sr.context import get_context
from test.sr.cal_pos.cal_pos_test_case import read_test_cases, TestCase, save_test_cases
from test.sr.cal_pos.test_cal_pos.test_cal_pos import TestCalPos


class DebugCalPos(TestCalPos):

def __init__(self, *args, **kwargs):
TestCalPos.__init__(self, *args, **kwargs)

def test_specified_one(self):
fail_cnt = 0
self.cases = read_test_cases(self.cases_path)
for case in self.cases:
if case.unique_id != 'P04_PNKN_R06_CSSH_02':
continue
result = self.run_one_test_case(case, show=True)
if not result:
fail_cnt += 1
log.info('%s 计算坐标失败', case.unique_id)

performance_recorder.log_all_performance()
self.assertEqual(0, fail_cnt)

def test_init_case(self):
"""
从debug中初始化
:return:
"""
ctx = get_context()
file_name: str = '_1711787600419'
mm = get_debug_image(file_name)

str_list = file_name.split('_')
is_running = (bool)(str_list.pop())
pp_r = (int)(str_list.pop())
pp_y = (int)(str_list.pop())
pp_x = (int)(str_list.pop())
region_prl_id = '_'.join(str_list)

region = get_region_by_prl_id(region_prl_id)

self.cases = read_test_cases(self.cases_path)
idx: int = 1
while True:
existed: bool = False
for c in self.cases:
if c.region.prl_id != region_prl_id:
continue
if c.num == idx:
existed = True
break
if existed:
idx += 1
else:
break

case = TestCase(region, Point(pp_x, pp_y), idx, is_running, [pp_x, pp_y, pp_r])
self.cases.append(case)
self.cases = sorted(self.cases, key=lambda x: x.unique_id)
save_test_cases(self.cases, self.cases_path)
self.save_test_image(mm, case.image_name)
log.info('新增样例 %s', case.unique_id)
81 changes: 81 additions & 0 deletions test/src/test/sr/cal_pos/test_cal_pos/test_cal_pos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import os

import cv2

import test
from basic import Point, cal_utils
from basic.log_utils import log
from sr import cal_pos, performance_recorder
from sr.cal_pos import VerifyPosInfo
from sr.context import get_context
from sr.image.image_holder import ImageHolder
from sr.image.sceenshot import mini_map, large_map
from test.sr.cal_pos.cal_pos_test_case import TestCase, read_test_cases


class TestCalPos(test.SrTestBase):

def __init__(self, *args, **kwargs):
test.SrTestBase.__init__(self, *args, **kwargs)

ih = ImageHolder()
# 预热 方便后续统计耗时
ih.preheat_for_world_patrol()
mini_map.preheat()

@property
def cases_path(self) -> str:
return os.path.join(self.sub_package_path, 'test_cases.yml')

def test_cal_pos(self):
fail_cnt = 0
self.cases = read_test_cases(self.cases_path)
for case in self.cases:
result = self.run_one_test_case(case, show=False)
if not result:
fail_cnt += 1
log.info('%s 计算坐标失败', case.unique_id)

performance_recorder.log_all_performance()
self.assertEqual(0, fail_cnt)

def run_one_test_case(self, case: TestCase, show: bool = False) -> bool:
"""
执行一个测试样例
:param case: 测试样例
:param show: 显示
:return: 是否与预期一致
"""
ctx = get_context()
ctx.init_image_matcher()

mm = self.get_test_image_new(case.image_name)

lm_info = ctx.ih.get_large_map(case.region)
possible_pos = tuple(case.possible_pos)
lm_rect = large_map.get_large_map_rect_by_pos(lm_info.gray.shape, mm.shape[:2], possible_pos)

mm_info = mini_map.analyse_mini_map(mm, ctx.im)
verify = VerifyPosInfo(last_pos=Point(case.possible_pos[0], case.possible_pos[1]),
max_distance=case.possible_pos[1])

pos = cal_pos.cal_character_pos(ctx.im,
lm_info=lm_info,
mm_info=mm_info,
running=case.running,
real_move_time=case.real_move_time,
lm_rect=lm_rect,
retry_without_rect=False,
show=show,
verify=verify
)
if show:
cv2.waitKey(0)

if pos is None:
log.error('%s 当前计算坐标为空', case.unique_id)
return False
else:
dis = cal_utils.distance_between(pos.center, case.pos)
log.info('%s 当前计算坐标为 %s 与目标点 %s 距离 %.2f', case.unique_id, pos.center, case.pos, dis)
return dis < 5
Loading

0 comments on commit 301f789

Please sign in to comment.