Developer Zone

MATLAB的高级软件开发

朋克,你感到幸运吗?

我是数学的一部分consulting team总部位于英国专门从事软件架构师ure and testing (Andy's发表了我的文章前)。作为我最近进行的一些培训的一部分,我正在考虑这张图已经在内部浮动了很多年:

Hilarious, I know. The point being made is an obvious one, which got me thinking – if it’s so obvious that writing tests is a good idea, why don’t more people do it?

You need a test, so why don't you write one?

I came up with some possible reasons as to why people don’t write tests:

  1. 他们不知道如何。
  2. They think it's difficult.
  3. They think that they don’t have the time.
  4. 他们尚未被咬伤 - 也许他们的代码还不够大/关键/关键性/关键因素,以至于发生了大规模的中断。

For the last of those points, you’ve got to ask yourself one question: Do I feel lucky?

Thinking of some of the customer projects I’ve worked on, people rely on the apps developed. If they stop working, they’re not going be putting lives at risk, but 10-100s of people might not be able to do their work. This costs time and money, and there might be reputational damage for the team that develops the app. I don’t feel lucky!

Test automation takes the luck out of software delivery

I think it’s widely accepted in the software industry that:

  • Software is getting more complex.
  • Our reliance on it is increasing.
  • Development teams are getting bigger.
  • 更频繁地需要更新/交付。

因此,我们需要测试以:

  • 验证这种复杂的行为。
  • Ensure robustness.
  • Check code from the whole team works together when integrated.

通过自动化快速有效地运行这些测试,我们可以自信地实现这些频繁的交付。

Testing helps individual developers too

以下是测试帮助我完成实际项目的五种方法:

  1. Testing has allowed me tomake changes to large projects with confidence。我不知道该代码的作用,即使经过很长时间的工作。当然,我可以给出执行摘要,或者我可以告诉您我处理过的特定代码的低级详细信息,但不是之间的所有内容。这就是客户工程师的领域知识。通过运行测试,我可以确定一切仍然有效。
  2. Tests have helped me to学习新的代码库当我被送入一个项目时。他们向我展示了如何使用代码以及依赖关系。
  3. Tests封装知识即使是一个很小的项目,我也获得了我作为开发过程的一部分做出的设计决策。我一再对复杂性的迅速累积感到惊讶 - 在编码的几个小时内,我失去了对每件代码的要做的事情以及所处理的所有边缘案例的跟踪。
  4. 测试使我能够focus on requirements rather than implementation细节。当实施不明显时,这特别有用。我可以遵循确定需要做的事情的过程,编写(失败的)测试以捕获该信息,执行一些实施,然后迭代 - ”红色,绿色,重构”.
  5. 它有助于drive software architecture通过创建小型,集中的,testablecomponents. If the code is difficult to test, it’s almost certainly because I’ve designed it wrong.

测试现在是标准练习

在主流软件开发中,写作测试已成为标准实践已有10多年了。“朋克”已经消失了 - 专业严谨和工艺是必须的。写作测试与编写代码息息相关。

已经说过(我忘记了谁 - 也许Uncle Bob,也许来自Cpp Con) that if you were to ask people in the late 90s or early 2000s whether they use version control, you would have got a patchy response – it wasn’t common place. Today, if you're not using version control, you'd be mocked! How could you possible develop anything without version control? Testing is going through the same transition. Don’t be the butt of the joke!

Whether your team or customer is demanding tests, as a software industry professional, you should be showing them the way!

立即开始写作测试

You should start writing tests whenever you start a project. Complexity builds up surprisingly quickly so start testing from the beginning. It’s much, much easier to write tests as you go rather than coming back later and trying to do it.

Failing that, start now! If you already have a project without tests, don’t be put off. You can adopt an incremental or “fix as you go” approach to build up your tests. Writing tests doesn’t need to be a monolithic undertaking. Over time your test suite and coverage will increase.

A common question related to this is how to manage the transition from prototyping algorithms and workflows to a more formal implementation with tests. The objection goes “what’s the point of writing all these tests when the code is in so much flux?”.

在这里,需要一个判断电话。在第一阶段,您会回答诸如“这甚至可能是可能的”,“我如何真正做到”和“结果值得进一步投资”之类的问题。在第二阶段,您开始依靠代码产生的答案,或者其他人也希望运行您的代码。无论哪种方式,您都开始构建工具。

一旦有第二阶段的暗示,就开始测试!

“But I don’t have time for any of this”

This common objection has been covered in many testing books and articles. To citeKent Beckas just one example:

“The more stress you feel, the less testing you will do. The less testing you do, the more errors you will make. The more errors you make, the more stress you feel. Rinse and repeat”.

-- Beck, K. (2003).Test-Driven Development: By Example。波士顿:艾迪生 - 韦斯利

想想那个在开发过程后期出现的错误,需要几天的时间来追踪。如果您进行了自动测试,该测试在出现后立即将其定位呢?想想一个糟糕的情况 - 发现错误时已经发布了代码。这需要多少时间来解决?我可以保证不会在星期一早上9点发生。在星期五下午5点尝试!

Now imagine an alternative world – you need to ship a new feature to meet a tight deadline. You integrate the code but are worried that it could break something else. But you remember you’ve got a test suite with full coverage that you trust. You run the suite and everything passes. You ship the code fully confident that everything works and go home on time.

所以最终,你有时间吗notto test?

So how do you get started with testing in MATLAB?

How totesting is an entire topic in itself. For now, I’m just going to point you to some resources to help you get started:

And finally...

是的,我知道 - 标题是错误的。实际报价是“ ...你必须问自己一个问题:'我感到幸运吗?'好吧,你,朋克吗?”




与Matlab®R2021A一起出版

|
  • print
  • 发送电子邮件

コメント

コメントを残すは,ここをクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。