简体   繁体   中英

turn on/off test in the source code

I am reproducing a spreadsheet in 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. 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.

How can I turn on the test for Monday, without inserting 20+ "if Monday: run test_n" to the python code?

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. 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. 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

2) comparing the python produces the same results as excel(the logic is the same) if the inputs are the same.

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. Then compare them to ensure they're the same.

Again, without more detail about your situation I can't offer much more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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