简体   繁体   English

Cypress 中的固定装置和支持文件有什么区别? 哪个更好用?

[英]What is the difference between fixtures and support files in Cypress? Which one is better to use?

Both fixtures and support files store static data, right? fixtures和 support 文件都存储了 static 数据,对吧? What is the best recommended to use?最好推荐使用什么?

A use case for fixtures could be to put static data that is used during a test, such as a JSON body. fixtures的一个用例可能是放置测试期间使用的 static 数据,例如 JSON 主体。

Say you need to store some request body JSON that you're going to be using a lot.假设您需要存储一些您将经常使用的请求主体 JSON。 It won't change much and doesn't need to be changed by tests.它不会有太大变化,也不需要通过测试来改变。 requestBody.json could be stored in fixtures and then in the test spec the .json could be referenced and then used in a test. requestBody.json可以存储在fixtures ,然后在测试规范中.json然后在测试中使用。 Files in cypress/fixtures/ are not meant to change or be dynamic in the tests: cypress/fixtures/中的文件并不意味着在测试中改变或动态:

const requestBody = require("../fixtures/requestBody.json");

describe('Test', () => {
  it('should do something', () => {
    cy.request({
      method: 'GET',
      url: myUrl,
      body: requestBody
    })....

A use case for support could be to define a Custom Command in cypress/support/commands.js . support的一个用例可能是在cypress/support/commands.js中定义自定义命令。

Say you need to send a header with some data that you don't want to expose, such as an Authorization key.假设您需要发送一个 header,其中包含一些您不想公开的数据,例如授权密钥。 You also need to hardcode the GET method, but you want to leave the url and body to be dynamic.您还需要对GET方法进行硬编码,但您希望urlbody保持动态。 A Custom Command could be defined in cypress/support/commands.js :可以在cypress/support/commands.js中定义自定义命令:

Cypress.Commands.add('getRequestWithSecret', (url, body) => {
    cy.request({
        method: 'GET',
        headers: {
            Authorization: mySecretKey
        },
        url: url,
        body: body
    });
});

and then this Custom Command can be used in the code.然后可以在代码中使用此自定义命令。 You can see in the example below there is a mixture of static and dynamic content.您可以在下面的示例中看到混合了 static 和动态内容。 The fixture being imported, as well as the Custom Command.正在导入的夹具,以及自定义命令。 getRequestWithSecret is dynamic because it is accepting inputs. getRequestWithSecret是动态的,因为它正在接受输入。 cypress/support/index.js and commands.js get loaded before the test spec starts. cypress/support/index.jscommands.js在测试规范开始之前加载。 This is pretty useful because it means that you can add scripts that can run before tests start.这非常有用,因为这意味着您可以添加可以在测试开始之前运行的脚本。 So these don't need to be imported into your tests at runtime.所以这些不需要在运行时导入到您的测试中。

const requestBody = require("../fixtures/requestBody");

describe('Test', () => {
  it('should do something', () => {
    cy.getRequestWithSecret({
      url: myUrl,
      body: requestBody
    })....

Both have different purposes.两者都有不同的目的。 Fixtures are used as external pieces of static data that can be used by your tests whereas the support file is a great place to put reusable behavior such as custom commands or global overrides that you want to be applied and available to all of your spec files.夹具用作 static 数据的外部片段,可由您的测试使用,而支持文件是放置可重用行为的好地方,例如您希望应用并可用于所有规范文件的自定义命令或全局覆盖。 For more better understanding, I recommend you to read the cypress official docs https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Support-file为了更好地理解,我建议您阅读 cypress 官方文档https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Support-file

Fixture is static data that is needed for tests - usually we put images, JSON files, and some downloadable fixtures there. Fixture是测试所需的 static 数据——通常我们将图像、JSON 文件和一些可下载的 fixture 放在那里。

In support files we add/customize commands, and import Plugins and other dependencies needed for the test.在支持文件中,我们添加/自定义命令,并导入插件和测试所需的其他依赖项。 It can also be used to make some initial setup, like before/beforeAll functions, but this part is better made in configuration files or directly in the tests.它还可以用于进行一些初始设置,如 before/beforeAll 函数,但这部分最好在配置文件中或直接在测试中进行。

暂无
暂无

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

相关问题 React 函数式组件有什么区别<message id>和js function formatMessage() 哪个更好用?</message> - What is the difference between React functional component <Message id> and js function formatMessage() and which one is better to use? 赛普拉斯中的暂停和调试有什么区别 - What is the difference between pause and debug in Cypress 最小文件和常规文件有什么区别,我应该使用哪一个? - What is the difference between the min and the regular file and which one should I use? Cypress before() hook 和 it() 测试有什么区别 - What is the difference between Cypress before() hook and it() test 不同的 NodeJS HTTP 服务器端口有什么区别,我应该使用哪一个? - What's the difference between different NodeJS HTTP server ports and which one should I use? 这两者之间有什么区别,我应该使用哪一个? - What's the difference between these two and which should I use? dist /文件夹中的JS文件与root文件夹中的JS文件有什么区别? - What is the difference between JS files in dist/ folder and the one in root? 创建对象的两种方法,不确定是什么区别或使用哪种方法 - 2 ways of creating an object, not sure what the difference is or which one to use Cypress:`cy.contains` 和 `cy.contains` 有什么区别? findByText` - Cypress: what is the difference between `cy.contains` and `cy. findByText` Cypress 测试中的 import 和 cy.fixture 有什么区别? - What is the difference between import and cy.fixture in Cypress tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM