简体   繁体   English

如何在 python 单元测试 aws-assume-role-lib 和 boto3 库中进行模拟?

[英]How to mock in python unit tests aws-assume-role-lib and boto3 libraries?

i have a function that has the following code in it我有一个 function,其中包含以下代码

ses = boto3.Session()
as_role = aws_assume_role_lib.assume_role(session, "some_role")

i'm trying to unit test it and i need to mock those two calls.我正在尝试对其进行单元测试,我需要模拟这两个调用。 What's the best way to do so?最好的方法是什么?

Without more information, there's no way to answer your question.没有更多信息,就无法回答您的问题。 Any direct answer would require a lot more information.任何直接的答案都需要更多的信息。 What is it that you want to test?你想测试什么? It certainly can't be the code you're showing us because if you mock out both of these calls, there's nothing left to test.它当然不可能是您向我们展示的代码,因为如果您模拟出这两个调用,就没有什么可测试的了。

Mocking involves injecting stand-ins for specific test cases such that for those test cases, the mocks return predetermined values without calling the real routines. Mocking 涉及为特定测试用例注入替身,这样对于这些测试用例,模拟返回预定值而不调用实际例程。 So what do you want those values to be?那么您希望这些值是什么? The mocks could also have side effects, or change their behavior based on external state, but you want to try to avoid either of those situations.模拟也可能有副作用,或者根据外部 state 改变它们的行为,但你想尽量避免这些情况。 Mocks are usually functional...returning a specific output for each set of specific explicit inputs, and neither modifying or being affected by implicit (external) state.模拟通常是功能性的......为每组特定的显式输入返回特定的 output,并且既不修改也不受到隐式(外部)state 的影响。

I assume that ses should be session so that the result of the first call is passed to the second call.我假设ses应该是session以便将第一次调用的结果传递给第二次调用。 In this case, neither of these calls takes varying data, so you can mock the result of the two calls by just assigning a static value to as_role ...whatever value your later code wants to see.在这种情况下,这些调用都不会使用不同的数据,因此您可以通过将 static 值分配给as_role来模拟这两个调用的结果......无论您以后的代码想要看到什么值。 Since there is only one set of inputs, there should only be one possible output.由于只有一组输入,因此应该只有一个可能的 output。

It would be more complicated if you need the mocks to change their behavior based on external state.如果您需要模拟基于外部 state 更改其行为,那将更加复杂。 How you might mock in that case is entirely dependent on the external states that are possible, where they come from, and how they affects the value of as_role .在这种情况下如何模拟完全取决于可能的外部状态,它们来自哪里,以及它们如何影响as_role的值。 Likewise, if you need your mocks to cause a change in external state, like modifying global variables, you'd need to specify that, and that might affect how you'd choose to mock.同样,如果您需要您的模拟来导致外部 state 发生变化,例如修改全局变量,您需要指定它,这可能会影响您选择模拟的方式。

In short, you should define a spec describing how you want your mocks to act under a restricted set of particular conditions.简而言之,您应该定义一个规范来描述您希望您的模拟如何在一组受限的特定条件下运行。 Having that spec will let you begin to decide how to implement them.拥有该规范将使您开始决定如何实施它们。

If you're just asking how to generally build mocks in Python, check out one or more of the existing Python mocking frameworks, like mock , flexmock , mox , Mocker , etc. A quick Google pointed me to this, which might be helpful: https://garybernhardt.github.io/python-mock-comparison/ .如果您只是问如何通常在flexmock Mocker mox mock https://garybernhardt.github.io/python-mock-comparison/ Asking for library recommendations is against SO policy as it involves mostly personal opinions, so if you're asking for that, you should look elsewhere.要求图书馆推荐是违反 SO 政策的,因为它主要涉及个人意见,所以如果您要求这样做,您应该寻找其他地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM