简体   繁体   English

在源代码中打开/关闭测试

[英]turn on/off test in the source code

I am reproducing a spreadsheet in python. 我正在用python复制电子表格。 The spreadsheet contains the data and the processing logic on every Monday not on the rest weekdays. 电子表格包含每个星期一而不是其余工作日的数据和处理逻辑。

I want to run the python code on everyday, if it is Monday, I want to compare the python result with the spreadsheet result. 我想每天运行python代码,如果是星期一,我想将python结果与电子表格结果进行比较。 I have 20+ tests spread across the python code doing the comparisons.The tests include: 1) comparing data that I got from production database is the same as in the excel 2) comparing the python produces the same results as excel(the logic is the same) if the inputs are the same. 我在python代码之间进行了20多个测试以进行比较。这些测试包括:1)比较从生产数据库获得的数据与excel中的数据相同2)比较python产生的结果与excel相同(逻辑是相同)(如果输入相同)。

How can I turn on the test for Monday, without inserting 20+ "if Monday: run test_n" to the python code? 如何在不插入20+“ if if Monday:run test_n”到python代码的情况下打开星期一的测试?

I don't think I can separate the test and the source code, since later tests takes inputs from previous processing steps. 我认为我无法将测试和源代码分开,因为以后的测试会从先前的处理步骤中获取输入。

It looks like you have a limited number of choices. 看来您的选择数量有限。

You could refactor your code to pull the tests together to activate them with fewer if tests. 您可以重构代码以将测试组合在一起,以通过更少的if测试来激活它们。 You say that may not be possible, but it seems to me that you should try to do that first. 您说不可能,但是在我看来您应该首先尝试这样做。 You recognize there is a smell in your code, so you should try some of the refactoring techniques to succeed in separating the test and the source code. 您认识到代码中有异味,因此您应该尝试一些重构技术以成功分离测试和源代码。 Check some of the techniques--there are many books and web sites that discuss some of them. 检查其中的一些技巧-有许多讨论其中一些技巧的书籍和网站。

You could leave your code as is. 您可以按原样保留您的代码。 This will build up technical debt but that may be necessary. 这将增加技术债务,但这可能是必要的。 Use the over 20 if statements and comment them well to they can be found and modified later if needed. 使用20多个if语句,并对它们进行注释,以使它们可以被找到并在需要时进行修改。 At least do the date check only once in your code, set a Boolean variable, and test that variable rather than redoing the date check. 至少在代码中只进行一次日期检查,设置一个布尔变量,然后测试该变量,而不要重做日期检查。

Without more detail I do not see how we could offer any other options. 没有更多细节,我看不到我们如何提供其他选择。

If these are tests in the "make sure it works" sense, they should not be in the production code. 如果这些测试是“确保它能正常工作”的意思,则它们不应出现在生产代码中。 They should be wholly separate in a test suite. 在测试套件中,它们应该完全分开。

Testing code is a very broad topic, but here's a few resources to get you started. 测试代码是一个非常广泛的主题,但是这里有一些资源可以帮助您入门。

I don't think I can separate the test and the source code, since later tests takes inputs from previous processing steps. 我认为我无法将测试和源代码分开,因为以后的测试会从先前的处理步骤中获取输入。

You absolutely can, every system does, but it may require redesigning your system. 您绝对可以,每个系统都可以,但是可能需要重新设计系统。 This is a common chicken-and-egg problem for legacy code: how do you change it safely if you can't test it? 对于遗留代码,这是一个常见的“鸡与蛋”问题:如果无法测试,如何安全地对其进行更改? And there are various techniques for dealing with that. 有各种各样的技术可以解决这个问题。 Refactoring , the process of redesigning code without changing how it works, will feature prominently. 重构是重新设计代码而不改变其工作方式的过程。 But without details I can't say much more. 但是没有细节,我不能说更多。

1) comparing data that I got from production database is the same as in the excel 1)比较我从生产数据库中获得的数据与excel中的数据相同

2) comparing the python produces the same results as excel(the logic is the same) if the inputs are the same. 2)如果输入相同,则比较python会产生与excel相同的结果(逻辑相同)。

Rather than testing inside your code, you should be testing its outputs. 您应该在测试代码的输出,而不是在代码内部进行测试。

Both of these should be a matter of converting the output of the various processes into a common format which can then be compared. 两者都应与将各种过程的输出转换成可以比较的通用格式有关。 This could be dumping them as JSON, turning them all into Python data structures, CSVs... whatever is easiest for your data. 这可能是将它们以JSON格式转储,然后将它们全部转换为Python数据结构,CSV等……最适合您的数据。 Then compare them to ensure they're the same. 然后比较它们以确保它们相同。

Again, without more detail about your situation I can't offer much more. 同样,如果没有您情况的更多详细信息,我将无法提供更多信息。

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

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