平日常用代码片段备份
matplotlib中文乱码解决
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
执行tests目录下所有测试用例
import os
import unittest
import importlib
def load_test_case_classes(file_path, module_parent='tests'):
""" load all test cases. """
classes = {}
for root, dirs, files in os.walk(file_path):
for fn in files:
name, ext = os.path.splitext(fn)
if ext != '.py' or not name.startswith('test_'):
continue
# dynamic import && filter out
module = importlib.import_module(f'{module_parent}.{name}')
for k, v in module.__dict__.items():
if (k.startswith('_') or
not isinstance(v, type) or
not issubclass(v, unittest.TestCase)):
continue
classes[k] = v
return classes
# Run all test cases.
path = os.path.dirname(os.path.abspath(__file__))
case_classes = load_test_case_classes(path)
globals().update(case_classes)
unittest.main()
本文介绍了解决Matplotlib在Python中显示中文乱码的问题,通过设置字体和Unicode参数实现中文正常显示。此外,还提供了一个自动化加载和运行测试用例的脚本,该脚本可以遍历指定目录下的所有符合命名规范的测试文件,并自动执行其中的测试案例。
1232

被折叠的 条评论
为什么被折叠?



