持续的第三方api在Rails上监视和测试

时间:2022-11-24 22:59:12

We would like to setup automated jobs (via Jenkins) to alert if the third party API is down or they deployed an incompatible APIs.

我们希望设置自动作业(通过Jenkins),以提醒第三方API是否已关闭,或者他们部署了不兼容的API。

I am talking about to test against the real HTTP APIs and not a mock, but as we already have mock written using rspec, I am not sure if we should duplicate the effort by writing two independent testes.

我说的是针对真正的HTTP api进行测试,而不是模拟测试,但是由于我们已经使用rspec编写了模拟测试,所以我不确定是否应该通过编写两个独立的测试来重复这一过程。

Anyone have this experience in this before? (I am not limited to Ruby/Rspec if other tools can help)

有人有过这样的经历吗?(如果有其他工具可以帮助的话,我并不局限于Ruby/Rspec)

4 个解决方案

#1


3  

Mocks are used to test YOUR OWN code w/o touching real API. And you want to test real API.

模拟用于测试您自己的代码w/o接触到真实的API。你想要测试真实的API。

So I think you have to write a set of tests in RSpec for example for unobtrusive test of 3rd party API.
By "unobtrusive" I mean track than you don't issue accidental "DELETE" API requests for example, or use all your daily requests API limit by a single test suite run.

因此,我认为您必须在RSpec中编写一组测试,例如对第三方API进行不引人注目的测试。我说的“不引人注目”是指跟踪,而不是像您那样发出意外的“删除”API请求,或者使用您的所有日常请求API限制来运行一个测试套件。

Don't know if specified API test tools exist.
As for me, I used RSpec to test my own remote APIs/servers with success.

不知道是否存在指定的API测试工具。至于我,我使用RSpec来测试自己的远程api /服务器。

#2


5  

Have you had a look at VCR? Using it, you can "record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests". I've used it with RSpec when testing expected responses from external APIs, and think it's great. I'd encourage you to check out the * questions tagged with if it's something you think may work for you.

你看过录像机吗?使用它,您可以“记录测试套件的HTTP交互,并在将来的测试运行中为快速、确定、准确的测试重播它们”。在测试来自外部api的预期响应时,我将它与RSpec一起使用,并认为它很棒。我鼓励你去看看那些标有vcr的*问题,如果你觉得它对你有用的话。

Not sure of its Jenkins integration, but when I was using VCR, I automated some regular tasks where I needed to hit the APIs with Whenever ("Cron jobs in Ruby"). Not really continuous, but somewhat automated.

不确定它的Jenkins集成,但是当我使用VCR时,我自动完成了一些常规任务,当我需要使用api时(“Ruby中的Cron jobs”)。不是连续的,但有点自动化。

#3


4  

When I was in this situation a few months ago I did the following:

几个月前,当我处于这种情况时,我做了以下事情:

  1. Mock the API and write tests against the mocked data (you already have that)
  2. 模拟API并针对模拟数据编写测试(您已经有了)
  3. Write one more test that gets data from the real API and asserts that it is (still) in the same form and contains the same kind of data that we expect
  4. 再编写一个测试,从真实的API获取数据,并断言它(仍然)以相同的形式包含我们期望的相同类型的数据

I did it like this since it was impossible for me to guess/know what content will be provided by the live API.

我这样做是因为我不可能猜到/知道live API将提供什么内容。

#4


2  

There's 2 things I'd do to the existing test suite so it could be used live, the first uses the ability for describe and it blocks to take metadata (There's a good blog post on it here). The second uses the ability for shared_contexts to take a block.

我要对现有的测试套件做两件事,这样它就可以被实时使用,第一件事是使用description功能,然后它会阻塞元数据(这里有一个很好的博客文章)。第二种方法使用shared_contexts来获取一个块。

Firstly, mark the specs you'd like to run against the real API with metadata. For example, you want to know these can be run for real, e.g.

首先,标记您希望使用元数据运行的实际API的规格。例如:you want to know these can run For real。

describe "Hitting the API with a call", :can_be_real do
  # …
end

These specs can then be run from the commandline using the tag option.

然后,可以使用标记选项从命令行运行这些规范。

The second thing, is to replace the mocks with the real thing. It depends on how you've defined the mocks, whether a before or a let was used, and how much you mocked. As a silly example, see below:

第二件事,是用真实的东西代替模拟。这取决于您如何定义mock,是否使用了before或let,以及您对它们进行了多少嘲讽。作为一个愚蠢的例子,请看下面:

require 'rspec'

RSpec.configure do |c|
  c.treat_symbols_as_metadata_keys_with_true_values = true
end

shared_context "all my mocks and stubs" do
  let(:this) { false }
end

describe "a", :real do
  include_context "all my mocks and stubs" do
    let(:this) { true } if ENV["REAL_API_CALL"] == 'true'
    before do
      stub_const( "URI", Class.new ) unless ENV["REAL_API_CALL"] == 'true'
    end
  end
  it "should be real when it's real" do
    this.should == true
  end
  it "should escape things when it's real" do
    URI.should respond_to :escape
  end
end

When the file is run via bin/rspec example.rb the output is:

当文件通过bin/rspec示例运行时。rb的输出是:

a
  should be real when it's real (FAILED - 1)
  should escape things when it's real (FAILED - 2)

Failures:

  1) a should be real when it's real
     Failure/Error: this.should == true
       expected: true
            got: false (using ==)
     # ./example.rb:19:in `block (2 levels) in <top (required)>'

  2) a should escape things when it's real
     Failure/Error: URI.should respond_to :escape
       expected URI to respond to :escape
     # ./example.rb:22:in `block (2 levels) in <top (required)>'

Finished in 0.00349 seconds
2 examples, 2 failures

When run via env REAL_API_CALL=true bin/rspec example.rb:

当通过env REAL_API_CALL运行时=true bin/rspec example.rb:

a
  should be real when it's real
  should escape things when it's real

Finished in 0.00301 seconds
2 examples, 0 failures

So you see, you can change the context of the specs in several ways that would allow you the level of control you want from the command line (and hence, Jenkins). You would want to mark the specs with other metadata, such as whether it's safe to run for real, whether it may take a long time etc.

因此,您可以通过几种方式更改规范的上下文,以允许您从命令行(因此,Jenkins)获得所需的控制级别。您可能想要用其他元数据来标记规范,比如是否可以安全地运行,是否需要很长的时间等等。

#1


3  

Mocks are used to test YOUR OWN code w/o touching real API. And you want to test real API.

模拟用于测试您自己的代码w/o接触到真实的API。你想要测试真实的API。

So I think you have to write a set of tests in RSpec for example for unobtrusive test of 3rd party API.
By "unobtrusive" I mean track than you don't issue accidental "DELETE" API requests for example, or use all your daily requests API limit by a single test suite run.

因此,我认为您必须在RSpec中编写一组测试,例如对第三方API进行不引人注目的测试。我说的“不引人注目”是指跟踪,而不是像您那样发出意外的“删除”API请求,或者使用您的所有日常请求API限制来运行一个测试套件。

Don't know if specified API test tools exist.
As for me, I used RSpec to test my own remote APIs/servers with success.

不知道是否存在指定的API测试工具。至于我,我使用RSpec来测试自己的远程api /服务器。

#2


5  

Have you had a look at VCR? Using it, you can "record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests". I've used it with RSpec when testing expected responses from external APIs, and think it's great. I'd encourage you to check out the * questions tagged with if it's something you think may work for you.

你看过录像机吗?使用它,您可以“记录测试套件的HTTP交互,并在将来的测试运行中为快速、确定、准确的测试重播它们”。在测试来自外部api的预期响应时,我将它与RSpec一起使用,并认为它很棒。我鼓励你去看看那些标有vcr的*问题,如果你觉得它对你有用的话。

Not sure of its Jenkins integration, but when I was using VCR, I automated some regular tasks where I needed to hit the APIs with Whenever ("Cron jobs in Ruby"). Not really continuous, but somewhat automated.

不确定它的Jenkins集成,但是当我使用VCR时,我自动完成了一些常规任务,当我需要使用api时(“Ruby中的Cron jobs”)。不是连续的,但有点自动化。

#3


4  

When I was in this situation a few months ago I did the following:

几个月前,当我处于这种情况时,我做了以下事情:

  1. Mock the API and write tests against the mocked data (you already have that)
  2. 模拟API并针对模拟数据编写测试(您已经有了)
  3. Write one more test that gets data from the real API and asserts that it is (still) in the same form and contains the same kind of data that we expect
  4. 再编写一个测试,从真实的API获取数据,并断言它(仍然)以相同的形式包含我们期望的相同类型的数据

I did it like this since it was impossible for me to guess/know what content will be provided by the live API.

我这样做是因为我不可能猜到/知道live API将提供什么内容。

#4


2  

There's 2 things I'd do to the existing test suite so it could be used live, the first uses the ability for describe and it blocks to take metadata (There's a good blog post on it here). The second uses the ability for shared_contexts to take a block.

我要对现有的测试套件做两件事,这样它就可以被实时使用,第一件事是使用description功能,然后它会阻塞元数据(这里有一个很好的博客文章)。第二种方法使用shared_contexts来获取一个块。

Firstly, mark the specs you'd like to run against the real API with metadata. For example, you want to know these can be run for real, e.g.

首先,标记您希望使用元数据运行的实际API的规格。例如:you want to know these can run For real。

describe "Hitting the API with a call", :can_be_real do
  # …
end

These specs can then be run from the commandline using the tag option.

然后,可以使用标记选项从命令行运行这些规范。

The second thing, is to replace the mocks with the real thing. It depends on how you've defined the mocks, whether a before or a let was used, and how much you mocked. As a silly example, see below:

第二件事,是用真实的东西代替模拟。这取决于您如何定义mock,是否使用了before或let,以及您对它们进行了多少嘲讽。作为一个愚蠢的例子,请看下面:

require 'rspec'

RSpec.configure do |c|
  c.treat_symbols_as_metadata_keys_with_true_values = true
end

shared_context "all my mocks and stubs" do
  let(:this) { false }
end

describe "a", :real do
  include_context "all my mocks and stubs" do
    let(:this) { true } if ENV["REAL_API_CALL"] == 'true'
    before do
      stub_const( "URI", Class.new ) unless ENV["REAL_API_CALL"] == 'true'
    end
  end
  it "should be real when it's real" do
    this.should == true
  end
  it "should escape things when it's real" do
    URI.should respond_to :escape
  end
end

When the file is run via bin/rspec example.rb the output is:

当文件通过bin/rspec示例运行时。rb的输出是:

a
  should be real when it's real (FAILED - 1)
  should escape things when it's real (FAILED - 2)

Failures:

  1) a should be real when it's real
     Failure/Error: this.should == true
       expected: true
            got: false (using ==)
     # ./example.rb:19:in `block (2 levels) in <top (required)>'

  2) a should escape things when it's real
     Failure/Error: URI.should respond_to :escape
       expected URI to respond to :escape
     # ./example.rb:22:in `block (2 levels) in <top (required)>'

Finished in 0.00349 seconds
2 examples, 2 failures

When run via env REAL_API_CALL=true bin/rspec example.rb:

当通过env REAL_API_CALL运行时=true bin/rspec example.rb:

a
  should be real when it's real
  should escape things when it's real

Finished in 0.00301 seconds
2 examples, 0 failures

So you see, you can change the context of the specs in several ways that would allow you the level of control you want from the command line (and hence, Jenkins). You would want to mark the specs with other metadata, such as whether it's safe to run for real, whether it may take a long time etc.

因此,您可以通过几种方式更改规范的上下文,以允许您从命令行(因此,Jenkins)获得所需的控制级别。您可能想要用其他元数据来标记规范,比如是否可以安全地运行,是否需要很长的时间等等。