我正在尝试测试是否使用某些值调用 pandas 方法。
然而,仅仅通过应用 @patch 装饰器就会导致修补的方法在 pandas 中抛出 a ValueError,而实际方法不会。我只是想测试正在Stock.calc_sma调用底层pandas.rolling_mean函数。
我假设@patch装饰器基本上向我正在修补的东西添加了一些“神奇”方法,这些方法允许我检查函数是否被调用。如果是这种情况,为什么pandas.rolling_mean无论修补与未修补该函数的行为不一样?
应用程序/模型.py
import pandas as pd
class Stock: # i've excluded a bunch of class methods, including the one that sets self.data, which is a DataFrame of stock prices.
def calc_sma(self, num_days)
if self.data.shape[0] > num_days: # Stock.data holds a DataFrame of stock prices
column_title = 'sma' + str(num_days)
self.data[column_title] = pd.rolling_mean(self.data['Adj Close'], num_days)
Run Code Online (Sandbox Code Playgroud)
应用程序/测试/TestStockModel.py
def setUp(self):
self.stock = MagicMock(Stock)
self.stock.ticker …Run Code Online (Sandbox Code Playgroud) 假设我们有以下结构:
class A():
class __A():
def __to_be_mocked(self):
#something here
def __init__(self):
with A.lock:
if not A.instance:
A.instance = A.__A()
def __getattr__(self,name):
return getattr(self.instance,name)
Run Code Online (Sandbox Code Playgroud)
现在我们要模拟函数。__to_be_mocked我们如何模拟它作为接受的目标mock.patch.object是package.module.ClassName。我已经尝试了所有方法,例如
target = A.__A
target = A.___A
Run Code Online (Sandbox Code Playgroud)
还有很多。
编辑:
我解决了它使用
target=A._A__A and attribute as '_A__to_be_mocked`
Run Code Online (Sandbox Code Playgroud)
现在的问题是__to_be_mocked在里面,__A所以不应该是___A__to_be_mocked。
是因为setattributeinA还是__init__in A?
我是 python 新手,一直在使用 python 3 进行学习。我正在使用 python 的单元测试框架来测试我的代码。
问题 :-
我需要单元测试的函数以下列方式接受输入:-
def compare():
a, b, c = input().strip().split(' ')
d, e, f = input().strip().split(' ')
# other code here
Run Code Online (Sandbox Code Playgroud)
我正在使用以下测试用例来模拟输入:-
class TestCompare(unittest.TestCase):
@patch("builtins.input", lambda: "1 2 3")
@patch("builtins.input", lambda: "4 5 6")
def test_compare(self):
self.assertEqual(compare(), "1 1")
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,当测试用例运行时,变量三元组 a,b,c 和 d,e,f 具有相同的值 - 1,2,3
我一直试图找到一种方法来注入第二组输入来运行我的测试,但徒劳无功。
非常感谢有关上述任何帮助。
解决方案环境:- Python 3
使用 curl 将 PATCH 发送到我的 Web 服务并出现错误:
Bad Request, The request sent by the client was syntactically incorrect. Apache Tomcat/8.0.37
Run Code Online (Sandbox Code Playgroud)
使用 curl 发送 GET 和 POST 时没有问题。使用 Swagger 发送相同的 PATCH 没有问题。所以使用 WireShark 来比较 Swagger(好)情况和 curl(坏)情况。
在好的情况下,在“{”和“,”之后插入一个 0xA。curl 不会这样做,并且缺少双引号。
这是我在 Windows 命令提示符上运行的 curl 命令

任何建议表示赞赏。
我正在使用带有Jersey 2.1 的Spring Boot
和 Ionic 来开发一个应用程序,我已经尝试了我找到的每一个帖子,但没有一个帖子为我解决了这个问题,我得到的错误,包括那些关于@PATCH自己创建界面注释的内容。
因此,问题是在执行PATCH请求时在浏览器上进行测试时,我在客户端收到此错误:
CORS 策略已阻止从源 ' http://localhost:8100 '访问 XMLHttpRequest :预检响应中的 Access-Control-Allow-Methods 不允许方法 PATCH。
我用于响应的过滤器如下,如果我有PATCH一个允许的方法并且它在 Postman 上完美运行,我不明白为什么我会得到这个:
筛选:
@Provider
public final class ResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
headers.putSingle("Accept-Patch", "*");
headers.putSingle("Access-Control-Allow-Origin", "*");
headers.putSingle("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE, PATCH");
headers.putSingle("Access-Control-Allow-Credentials", "true");
headers.putSingle("Access-Control-Max-Age", "3600");
headers.putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
}
}
Run Code Online (Sandbox Code Playgroud)
使用的方法PATCH:
import javax.ws.rs.PATCH; …Run Code Online (Sandbox Code Playgroud) 我正在创建一个 Web API 服务,但在调用 HTTP 补丁请求时遇到一些问题。尽管我知道如何创建它们。如果您能帮助我,我将不胜感激,这是我的代码:
HTTP 补丁代码:
[HttpPatch("{username}")]
public async Task<ActionResult> Patch(string username, [FromBody] JsonPatchDocument<User> patchDocument)
{
//If no info has been passed, this API call will return badrequest
if (patchDocument == null)
return BadRequest();
var theUser = await connection.GetAUser(username);
var originalUser = await connection.GetAUser(username);
if (theUser == null)
return NotFound();
//Changes specified in the patchdocument are applied to the user
patchDocument.ApplyTo(theUser, ModelState);
//check if the patching has been successful or not
bool isValid = TryValidateModel(theUser);
if (!isValid)
return …Run Code Online (Sandbox Code Playgroud) 我一直在这个网站上阅读很多相关/类似的问题,但它们都不起作用,而且我似乎没有看到相同类型的错误,所以我决定就此提出一个新问题。
我正在尝试学习更多关于 git 的知识,特别是如何应用补丁并从某些分支中提取提交并将其应用到其他分支。我最初想做一个虚拟测试,其中包括从分支中选择一些提交(直到过去的某个时间点)并将这些提交重新应用到过去的同一时间点,以使我回到初始点。
但是,我收到了大量此类“错误:补丁不适用”的错误消息。
我不明白为什么它不起作用。我尝试添加诸如 --whitespace=fix 等选项(在本网站的其他问题中建议),但无济于事。我还尝试使用 -3 来希望我可以手动合并文件,但这只是将错误消息更改为“错误:补丁失败:文件名”,几乎所有文件都再次出现。
为了重现此错误,我使用以下 git 存储库: https: //git.evlproject.org/linux-evl.git
具体来说,有提交的分支是evl/v5.4,没有提交的分支是master。我当时尝试过:
git diff evl/v5.4 master > ../patchfile
git checkout master
git apply ../patchile
Run Code Online (Sandbox Code Playgroud) A_script.py
from uuid import uuid4
def get_unique_identifier(env, customer_id):
return env + '-' + customer_id + '-' + str(uuid4())[0:8]
Run Code Online (Sandbox Code Playgroud)
测试_A_脚本.py
import unittest
from unittest.mock import patch
import src.A_script as a_script
class MyTestCase(unittest.TestCase):
@patch('uuid.uuid4')
def test_get_unique_identifier(self, mock_uuid4):
mock_uuid4.return_value = 'abcd1234'
expected = 'test_env-test_cust-abcd1234'
unique_identifier = a_script.get_unique_identifier('test_env', 'test_cust')
self.assertEqual(expected, unique_identifier)
Run Code Online (Sandbox Code Playgroud)
如何让 uuid4 返回“abcd1234”?
我正在尝试使用如下补丁将相同的作业历史记录限制应用于多个 CronJobs ,名为kubeJobHistoryLimit.yml:
apiVersion: batch/v1beta1
kind: CronJob
spec:
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
Run Code Online (Sandbox Code Playgroud)
我的kustomization.yml样子:
bases:
- ../base
configMapGenerator:
- name: inductions-config
env: config.properties
patches:
- path: kubeJobHistoryLimit.yml
target:
kind: CronJob
patchesStrategicMerge:
- job_specific_patch_1.yml
- job_specific_patch_2.yml
...
resources:
- secrets-uat.yml
Run Code Online (Sandbox Code Playgroud)
在我的 CI 管道中的某个时刻,我有:
kubectl --kubeconfig $kubeconfig apply --force -k ./
Run Code Online (Sandbox Code Playgroud)
版本kubectl是1.21.9.
问题是工作历史限制值似乎没有被拾取。我使用的 K8s 配置或版本有问题吗?
正如我们所假设的那样,类方法不支持直接设置颜色或圆形补丁;
circle.set_color('b')
Run Code Online (Sandbox Code Playgroud)
^^这是不正确的.
patch ×10
python ×5
mocking ×3
unit-testing ×3
api ×1
c# ×1
cors ×1
curl ×1
git ×1
httprequest ×1
java ×1
jax-rs ×1
json ×1
kubectl ×1
kubernetes ×1
lambda ×1
matplotlib ×1
pandas ×1
python-2.7 ×1
python-3.x ×1
rest ×1
spring-boot ×1
swagger ×1