- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 python 中使用 PYCharm 和 unittest 来测试我的类。
逐一运行每个测试方法,所有测试都顺利通过。
一起运行所有测试失败并出现 assertionErrors。设置和拆卸方法已设置。
这是我的测试类的代码:
# -*- coding: utf-8 -*-
import unittest
import numpy as np
import os
import sys
from interpolation.interpolation import Interpolation
from StringIO import StringIO
class InterpolationTests(unittest.TestCase):
_interpolation = None
@classmethod
def setUpClass(cls):
cls._interpolation = Interpolation()
@classmethod
def tearDownClass(cls):
cls._interpolation = None
def test_load_valid_file(self):
self._interpolation.from_file('./testfiles/valid.json')
self.assertEqual('kriging', self._interpolation._method)
self.assertEqual(1.0, self._interpolation._xMin)
self.assertEqual(1.2, self._interpolation._xMax)
self.assertEqual(2.1, self._interpolation._yMin)
self.assertEqual(0.2, self._interpolation._yMax)
self.assertEqual(12, self._interpolation._nX)
self.assertEqual(13, self._interpolation._nY)
self.assertEqual(2, len(self._interpolation._points))
points = self._interpolation._points
self.assertEqual(1.1, points[0]['x'])
self.assertEqual(2.2, points[0]['y'])
self.assertEqual(3.4, points[0]['value'])
self.assertEqual(4.4, points[1]['x'])
self.assertEqual(5.5, points[1]['y'])
self.assertEqual(6.6, points[1]['value'])
def test_load_invalid_JSON_format(self):
self._interpolation.from_file('./testfiles/invalid.json')
saved_stdout = sys.stdout
try:
out = StringIO()
sys.stdout = out
self._interpolation.render_output()
output = out.getvalue().strip()
self.assertEqual('{"error":"Something went wrong with the json decoding"}',
output.encode('ascii', 'ignore'))
finally:
sys.stdout = saved_stdout
def test_load_empty_file(self):
self._interpolation.from_file('./testfiles/empty.json')
saved_stdout = sys.stdout
try:
out = StringIO()
sys.stdout = out
self._interpolation.render_output()
output = out.getvalue().strip()
self.assertEqual('{"error":"Something went wrong with the json decoding"}',
output.encode('ascii', 'ignore'))
finally:
sys.stdout = saved_stdout
def test_idw_with_file(self):
self._interpolation.from_file('./testfiles/valid_idw.json')
self._interpolation.calculate()
self.assertEqual(50, len(self._interpolation._output))
self.assertEqual(50, len(self._interpolation._output[0]))
def test_kriging(self):
self._interpolation.from_file('./testfiles/valid.json')
self._interpolation.calculate()
self.assertEqual(13, len(self._interpolation._output))
self.assertEqual(12, len(self._interpolation._output[0]))
def test_mean_with_file(self):
self._interpolation.from_file('./testfiles/valid_mean.json')
self._interpolation.calculate()
self.assertEqual(60, len(self._interpolation._output))
self.assertEqual(50, len(self._interpolation._output[0]))
def test_mean(self):
mean = Interpolation.mean(10, 20, np.array([1.2, 1.3, 1.4, 1.6, 1.9]))
self.assertEqual(20, len(mean))
self.assertEqual(10, len(mean[1]))
self.assertEqual(1.48, mean[1][2])
def test_gaussian(self):
self._interpolation.from_file('./testfiles/valid_gaussian.json')
self._interpolation.calculate()
self.assertEqual(50, len(self._interpolation._output))
self.assertEqual(50, len(self._interpolation._output[0]))
def test_with_two_points(self):
self._interpolation.from_string(json_input='{"bounding_box":{"x_min":0,"x_max":10,"y_min":0,"y_max":10},"grid_size":{"n_x":10,"n_y":11},"point_values":[{"x":1,"y":5,"value":800},{"x":2,"y":8,"value":3}],"type":"gaussian"}')
self._interpolation.calculate()
self.assertEqual('Exception raised in calculation of method gaussian', self._interpolation._error_message)
if __name__ == '__main__':
unittest.main()
以下是失败测试的部分结果:
/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin/python2.7 "/Applications/PyCharm CE.app/Contents/helpers/pycharm/noserunner.py" /Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py
Testing started at 09:52 ...
..
..
Failure
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 77, in test_kriging
self.assertEqual(13, len(self._interpolation._output))
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 513, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 506, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 13 != 50
-------------------- >> begin captured stdout << ---------------------
<type 'exceptions.Exception'> Matrix is not positive definite
--------------------- >> end captured stdout << ----------------------
F
..
..
..
..
Failure
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 83, in test_mean_with_file
self.assertEqual(60, len(self._interpolation._output))
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 513, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 506, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 60 != 50
F
Failure
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 101, in test_with_two_points
self.assertEqual('Exception raised in calculation of method gaussian', self._interpolation._error_message)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 513, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 506, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 'Exception raised in calculation of method gaussian' != 'Something went wrong with the json decoding'
F
======================================================================
FAIL: test_kriging (test_interpolation.InterpolationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 77, in test_kriging
self.assertEqual(13, len(self._interpolation._output))
AssertionError: 13 != 50
-------------------- >> begin captured stdout << ---------------------
<type 'exceptions.Exception'> Matrix is not positive definite
--------------------- >> end captured stdout << ----------------------
======================================================================
FAIL: test_mean_with_file (test_interpolation.InterpolationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 83, in test_mean_with_file
self.assertEqual(60, len(self._interpolation._output))
AssertionError: 60 != 50
======================================================================
FAIL: test_render_to_file (test_interpolation.InterpolationTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Ralf/Projekte/inowas/inowas/py/pyprocessing/tests/test_interpolation.py", line 122, in test_render_to_file
self.assertTrue(os.path.isfile(output_filename))
AssertionError: False is not true
-------------------- >> begin captured stdout << ---------------------
{"error":"Something went wrong with the json decoding"}
--------------------- >> end captured stdout << ----------------------
Ran 11 tests in 0.826s
FAILED (failures=5)
Process finished with exit code 0
最后是插值类:
#! /usr/env python
from pyKriging.krige import kriging
from sklearn.gaussian_process import GaussianProcess
import demjson
import numpy as np
class Interpolation:
"""The interpolation class"""
_method = ""
_xMin = 0.0
_xMax = 0.0
_yMin = 0.0
_yMax = 0.0
_nX = 0
_nY = 0
_dX = 0.0
_dY = 0.0
_X = []
_Y = []
_points = []
_output = ""
_json_output = ""
_error = False
_error_message = ""
def __init__(self):
pass
def from_file(self, input_file):
try:
_file = open(input_file, 'r')
json_input = _file.read()
except IOError as exc:
self._error = True
self._error_message = str(exc)
return
except Exception as exc:
self._error = True
self._error_message = str(exc)
return
self.decode_json(json_input)
def from_string(self, json_input):
self.decode_json(json_input)
def decode_json(self, json_input):
try:
json_dict = demjson.decode(json_input)
except Exception as exc:
self._error = True
self._error_message = "Something went wrong with the json decoding"
return
if 'type' in json_dict:
self._method = json_dict['type']
if 'bounding_box' in json_dict:
bounding_box = json_dict['bounding_box']
if 'x_min' in bounding_box:
self._xMin = float(bounding_box['x_min'])
if 'x_max' in bounding_box:
self._xMax = float(bounding_box['x_max'])
if 'y_min' in bounding_box:
self._yMin = float(bounding_box['y_min'])
if 'y_max' in bounding_box:
self._yMax = float(bounding_box['y_max'])
if 'grid_size' in json_dict:
grid_size = json_dict['grid_size']
if 'n_x' in grid_size:
self._nX = grid_size['n_x']
if 'n_y' in grid_size:
self._nY = grid_size['n_y']
self._dX = (self._xMax - self._xMin) / self._nX
self._dY = (self._yMax - self._yMin) / self._nY
if 'point_values' in json_dict:
self._points = json_dict['point_values']
for point in self._points:
if 'x' in point and 'y' in point:
self._X.append([point['x'], point['y']])
if 'value' in point:
self._Y.append(point['value'])
def calculate(self):
if not self._error:
try:
if self._method == 'kriging':
self._output = self.kriging(self._nX, self._nY, self._X, self._Y, self._xMin, self._yMin, self._dX, self._dY)
elif self._method == 'mean':
self._output = self.mean(self._nX, self._nY, self._Y)
elif self._method == 'gaussian':
self._output = self.gaussian_process(self._nX, self._nY, self._X, self._Y, self._xMin, self._yMin, self._dX, self._dY)
elif self._method == 'idw':
self._output = self.inverse_distance_weighting(self._nX, self._nY, self._X, self._Y, self._xMin, self._yMin, self._dX, self._dY)
else:
self._error = True
self._error_message = 'Method %s is not supported' % self._method
except:
self._error = True
self._error_message = 'Exception raised in calculation of method %s' % self._method
def render_output(self, output_file=''):
if self._error:
self.render_error()
return
output = self.render(self._method, self._output)
if output_file == '':
print output
else:
try:
output_file = open(output_file, 'w')
output_file.truncate()
output_file.write(output)
output_file.close()
except IOError as exc:
self._error = True
self._error_message = str(exc)
self.render_error()
return
self.render_success()
def render_success(self):
result = {"success": self._method}
print demjson.encode(result)
def render_error(self):
result = {"error": self._error_message}
print demjson.encode(result)
@staticmethod
def render(method, output):
if (method == 'kriging') or (method == 'mean') or (method == 'gaussian') or (method == 'idw'):
result = {"raster": output, "method": method}
return demjson.encode(result)
@staticmethod
def kriging(nx, ny, x, y, x_min, y_min, dx, dy):
grid = np.zeros((ny, nx))
k = kriging(np.array(x), np.array(y))
k.train()
for i in range(ny):
for j in range(nx):
cell = np.array([y_min + dy * j + .5 * dy, x_min + dx * i + .5 * dx])
grid[i][j] = k.predict(cell)
return grid
@staticmethod
def mean(nx, ny, values):
mean_value = np.mean(values)
grid = mean_value * np.ones((ny, nx))
return grid
@staticmethod
def gaussian_process(nx, ny, X, y, x_min, y_min, dx, dy):
"""
Gausian process method. To replace kriging.
Description:
http://scikit-learn.org/stable/modules/generated/sklearn.gaussian_process.GaussianProcess.html#sklearn.gaussian_process.GaussianProcess.predict
The scikit learn python library should be installed
Should be tested
"""
# Prediction is very sensetive to the parameters below, method to be used carefully!
gp = GaussianProcess(regr='quadratic', corr='cubic', theta0=0.1, thetaL=.001, thetaU=1., nugget=0.01)
gp.fit(X, y)
X_grid_x = np.linspace(x_min, x_min+dx*nx, nx)
X_grid_y = np.linspace(y_min, y_min+dy*ny, ny)
xv, yv = np.meshgrid(X_grid_x, X_grid_y)
X_grid = np.dstack(( xv.flatten(), yv.flatten()))[0]
grid = np.reshape(gp.predict(X_grid, eval_MSE=False, batch_size=None), (ny, nx))
return grid
@staticmethod
def inverse_distance_weighting(nx, ny, X, y, x_min, y_min, dx, dy):
"""
Inverse-distance weighting interpolation method
"""
def pointValue(x, y, power, smoothing, xv, yv, values):
""" This function is used inside the inverse_distance_weighting method. """
from math import pow
from math import sqrt
nominator=0
denominator=0
for i in range(0,len(values)):
dist = sqrt((x-xv[i])*(x-xv[i])+(y-yv[i])*(y-yv[i])+smoothing*smoothing)
#If the point is really close to one of the data points, return the data point value to avoid singularities
if(dist<0.0000000001):
return values[i]
nominator=nominator+(values[i]/pow(dist,power))
denominator=denominator+(1/pow(dist,power))
#Return NODATA if the denominator is zero
if denominator > 0:
value = nominator/denominator
else:
value = -9999
return value
power, smoothing = 5, 0
xv = [i[0] for i in X]
yv = [i[1] for i in X]
grid = np.zeros((ny, nx))
for i in range(nx):
for j in range(ny):
grid[j][i] = pointValue((x_min + dx/2)+dx*i, (y_min + dy/2)+dy*j, power, smoothing, xv, yv, y)
return grid
最佳答案
您正在使用 setUpClass()
和 tearDownClass()
,整个类只调用一次。这导致您的不同测试方法通过更改 _interpolation
属性相互干扰。
您可以改用 setUp()
和 tearDown()
,它们会在每个测试方法之前/之后调用一次。
更好的是,由于您是在每个测试中从一个文件中初始化这个对象,所以只需让每个测试方法在开始时创建自己的 Interpolation
对象即可。
关于python unittest 无法运行所有测试方法,而每个测试方法运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37853023/
在开发 API 时,我经常在 main 函数中编写测试代码,但因为 D 已经集成了 unittest,所以我想开始使用它们。 我当前的工作流程如下,我有一个脚本可以监视任何 .d 文件中的文件更改,如
我无法使用 pub 包语法导入文件,如下所示: #import("package:unittest/unittest.dart"); 我收到以下编译时错误: 找不到引用的源:包:unittest/un
我想问一下我是否应该将我正在测试的函数包含在 unittest 文件中(这样我就会有一个文件 unittest.py),或者我应该只将它导入到 unittest 文件中(我会有两个文件、unittes
我正在尝试使用 C++/Codelite 进行单元测试。我从 codelite-plugins 包(Ubuntu 18.04)安装了 UnitTest++ 插件。我也可以看到这个: $ ls -la
我正在用 python 进行单元测试。我没有使用任何自动测试发现。我正在手动将 TestCases 组装到 TestSuite 中。 我可以用 unittest.TextTestRunner().ru
我正在尝试学习 Python 中的单元测试,特别是 unittest 模块。 考虑以下几行: import unittest class abc(unittest.TestCase): def
我正在开发一个修改测试套件的程序。 在最初的实现中,只支持 unittest 框架。我现在正在尝试添加对 Pytest 的支持。 使用默认的 unitest 模块,我可以将修改后的测试作为 AST 保
我有一个带有两种不同方法的单元测试测试用例。如果第一个方法失败,我希望跳过我的第二个方法。 我正在使用装饰器 @unittest.skipIf 但我找不到合适的条件。 class myTest(uni
根据文档,我可以在调用 unittest.main 时设置 python unittest 的详细级别,例如 unittest.main(verbosity=2) 如何在 unittest.TestC
有没有人遇到过这样的情况,他们将自己代码的单元测试写到一个名为unittest.py的文件中,发现它与NumPy的unittest.py模块冲突?换句话说,如果我将其写入本地目录中的 unittest
似乎有两种使用方式 unittest.mock.patch : 有更好的方法吗? 使用上下文管理器和 with 语句: class MyTest(TestCase): def test_som
在装有 PyCharm 的两台不同机器上,我有相同的项目。我有简单的代码: import unittest from tests import test unittest.makeSuite(test
我正在尝试执行我的以下测试套件: import unittest from Login_Page import LoginPageAndLogout def test_suite(): # g
当我正常运行应用程序并在浏览器中登录时,它可以正常工作。但是使用 Unittest 它不会让我登录....,它会再次返回登录页面。 “print rv.data”都只是打印登录页面的内容,但它应该打印
我仍在使用 Django 1.2.1,我认为对于较新的 Django,我们不会 import unittest然后做unittest.TestCase . 插图 import unittest cla
基于上一篇文章,这篇文章是关于使用coverage来实现代码覆盖的操作实例,源代码在上一篇已经给出相应链接。 本篇文章字用来实现代码覆盖的源代码,整个项目的测试框架如下: 就是在源代码的基础
当被测试的模块需要导入其他模块时,我们的 Python 3.10 单元测试会中断。当我们使用其他帖子和文章推荐的打包技术时,要么单元测试导入模块失败,要么直接调用运行应用程序导入模块失败。我们读过的其
我已经定义了一个自定义错误,但是如果我测试是否得到了自定义错误 提出来,它失败了。 我的models.py: class CustomError(Exception): """ Thi
我目前正在做一个项目,其结构是: my_package │ README.md | setup.py │ └───my_package | | __init__.py │ │
这是我的项目设置: my_project ./my_project ./__init__.py ./foo ./__init__
我是一名优秀的程序员,十分优秀!