简体   繁体   English

我应该使用什么测试方法?

[英]What testing approach should I use?

I am testing a very complex quoting tool where I need to test discount margin price and discount price.我正在测试一个非常复杂的报价工具,我需要测试折扣保证金价格和折扣价格。 Lets say price in catalog is 100$ And a partner is getting 25% discount, then partner price is 75$假设目录中的价格为 100 美元,而合作伙伴获得 25% 的折扣,则合作伙伴价格为 75 美元

Should I test like this -> cy.get(partnerPriceField)/should('have.value', 75)我应该这样测试 -> cy.get(partnerPriceField)/should('have.value', 75)

OR -> cy.get(partner PriceField)/should('have.value', catalogPrice * (1-discount/100))或 -> cy.get(partner PriceField)/should('have.value', catalogPrice * (1-discount/100))

cy.get(partnerPriceField).should('have.value', 75) is preferred for e2e tests. cy.get(partnerPriceField).should('have.value', 75)是 e2e 测试的首选。

The job of testing the calculation falls to unit tests , which are close to the source code so test can be changed if/when the src calculation changes.测试计算的工作属于单元测试,它们接近源代码,因此如果/当 src 计算发生变化时,可以更改测试。

The job of e2e test is to make sure that given some input data the page shows the output data (black box). e2e 测试的工作是确保给定一些输入数据,页面显示输出数据(黑框)。

Preferably input and output are supplied in a fixture table that's easily adjusted when the calculation is revised.输入和输出最好在一个夹具表中提供,当计算被修改时,该表很容易调整。

You can pass a regex with .contains() and followed by an assertion to check the value is visible.您可以使用.contains()传递一个正则表达式,然后是一个断言以检查该值是否可见。

const fullPrice = 100
const discount = .25
const discountPrice = fullPrice * discount
const regexMatcher = new RegExp(`$${discountPrice}`) // equals /\$75/

cy.contains('.selector-containing-price-text', regexMatcher)
  .should('be.visible')

I would suggest you go with the second option because in that case if in future the catalog price or discount value changes, you don't have to change the logic because it's generic.我建议您使用第二个选项,因为在这种情况下,如果将来catalog pricediscount value发生变化,您不必更改逻辑,因为它是通用的。 Having said that, for the second option to work you have to first change the catalogPrice and discount to numbers then after calculation, you have to convert the final answer to a string and then do the final assertion, something like this:话虽如此,要使第二个选项起作用,您必须首先将 catalogPrice 和折扣更改为数字,然后在计算之后,您必须将最终答案转换为字符串,然后执行最终断言,如下所示:

cy.get('partner PriceField').should(
  'have.value',
  (+catalogPrice * (1 - +discount / 100)).toString()
)

This is how your calculation looks like:这是您的计算方式:

JS计算

Also in case, if your catalogPrice is coming as 100$ and discount is coming as 25% , just add .replace(/\D/g,'') to remove % and $.此外,如果您的 catalogPrice 为100$并且折扣为25% ,只需添加.replace(/\D/g,'')以删除 % 和 $。 Then in this case your assertion will look like this:那么在这种情况下,您的断言将如下所示:

cy.get('partner PriceField').should(
  'have.value',
  (
    +catalogPrice.replace(/\D/g, '') *
    (1 - +discount.replace(/\D/g, '') / 100)
  ).toString()
)

Your calculation would look like this:您的计算将如下所示:

计算 2

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

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