{"id":3897,"date":"2026-11-21T21:18:20","date_gmt":"2026-11-21T13:18:20","guid":{"rendered":"http:\/\/www.intacthair.com\/blog\/?p=3897"},"modified":"2026-11-21T21:18:20","modified_gmt":"2026-11-21T13:18:20","slug":"how-to-test-a-controller-in-unit-tests-4fe9-cb938c","status":"publish","type":"post","link":"http:\/\/www.intacthair.com\/blog\/2026\/11\/21\/how-to-test-a-controller-in-unit-tests-4fe9-cb938c\/","title":{"rendered":"How to test a controller in unit tests?"},"content":{"rendered":"<p>If you\u2019ve ever sold industrial automation gear, you know a dud controller can turn a client\u2019s production line from \u201crunning smooth\u201d to \u201cwhy did we even invest in this?\u201d in 24 hours. That\u2019s why when I\u2019m prepping to ship a controller, I don\u2019t wait for a client\u2019s first bad log to catch a bug\u2014I unit test every part of it, long before it hits the factory floor. If you\u2019re a dev, an automation engineer, or someone who just cares that your control hardware actually works, this is the no-BS guide to unit testing controllers that I\u2019ve refined over 10 years of selling these things. No jargon, no stuffy textbooks, just real-world stuff that\u2019s saved my team hundreds of hours of callback tickets. <a href=\"https:\/\/www.anjodrive.com\/controller\/\">Controller<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.anjodrive.com\/uploads\/45277\/small\/3-phase-ac-servo-motor484e4.jpg\"><\/p>\n<p>First, let\u2019s get one thing straight: unit testing for a controller isn\u2019t like testing a website\u2019s checkout flow. Controllers live in the real world\u2014they talk to sensors, trigger motors, and make split-second decisions that actually move physical parts. So I don\u2019t waste time testing abstract \u201cunits\u201d in a vacuum; I test every discrete function that matters, one at a time, like I\u2019m picking apart a puzzle to make sure none of the pieces are wonky. That\u2019s the key difference between unit testing for software and unit testing for the hardware\/software combo that\u2019s a controller.<\/p>\n<p>Let\u2019s break this down step by step, starting with the easiest (and most important) stuff first. First up: test the core logic without hooking anything to the controller. My team calls this the \u201cheadless test\u201d because we run the controller\u2019s firmware on our laptops first, no physical wires attached. For example, if my controller has a safety logic function that stops a conveyor if a light curtain triggers, I don\u2019t wait to plug in the light curtain to test that. I write mock inputs: I feed the controller a \u201cvirtual\u201d signal that mimics what the light curtain would send if something crosses it, and then I check if the output is what it\u2019s supposed to be\u2014no conveyor run, no error messages.<\/p>\n<p>Wait, why mock inputs? Because real-world sensors can be finicky\u2014they drift, they send random noise, they take time to connect. If I test only with the physical light curtain, I might miss a bug where the safety logic only works when the light curtain sends a clean, perfect signal. Mock inputs let me throw garbage at the controller on purpose: like a half-signal, a too-short pulse, or even a signal that\u2019s way out of range (like 10 volts when it\u2019s supposed to be 0-5). If the controller crashes or sends the wrong output when I feed it garbage, that\u2019s a bug I fix before it ever gets to a client. Last year we had a client who swore their controller was bad because it cut off a motor mid-run for no reason. Turns out it was a stray electrical signal from a nearby welding robot, and the controller\u2019s logic didn\u2019t have a filter to ignore that noise. If we\u2019d tested with mocks, we would\u2019ve caught that filter gap before shipping.<\/p>\n<p>Next step: test the I\/O channels individually. Once we\u2019re happy with the core logic, we hook up a simple test rig to check every input and output port on the controller\u2014no big sensors or motors, just cheap test gear. For input ports, we use a power supply to send known voltages or currents (or digital signals, like 0 or 1) and make sure the controller reads them correctly. For output ports, we connect a multimeter to check if the controller sends the exact voltage\/current it\u2019s supposed to when we trigger a function. This might sound tedious, but it\u2019s how we caught a weird bug a while back: our batch of controllers had one input port that flipped high when it was supposed to be low, and it only showed up when we tested multiple ports at once. If I\u2019d skipped this step, a client\u2019s motor wouldn\u2019t turn off when it was supposed to, and I\u2019d be driving 3 hours to fix it instead of shipping a fixed unit.<\/p>\n<p>Don\u2019t forget edge cases, either. Controllers don\u2019t just handle normal, everyday inputs\u2014they handle the weird stuff that happens when no one\u2019s looking. Like, what if you lose power mid-sequence? What if a sensor sends a signal that\u2019s at the absolute limit of what the controller can read (like 5.1 volts when the spec says max is 5)? What if you run the controller 10,000 times in a row to make sure it doesn\u2019t slow down or glitch? We call these \u201cstress tests\u201d and \u201ccorner case tests,\u201d and they\u2019ve saved us from a lot of panic. Once, we had a client\u2019s controller reset itself every time their line ran two shifts straight. Turns out the firmware had a memory leak that only showed up after 8 hours of non-stop operation\u2014something we caught by running our test rig for 12 hours straight, no breaks.<\/p>\n<p>Now, here\u2019s something most new controller suppliers skip: test the communication protocols. Most controllers don\u2019t work alone\u2014they talk to HMI screens, other controllers, or cloud servers. If your controller can\u2019t send data to where it needs to go, it\u2019s useless, no matter how good its core logic is. So we test every protocol we support: Modbus, Ethernet\/IP, whatever. For example, if a controller\u2019s supposed to send a production count to an HMI, we run a test that sends 1,000 counts back to back and make sure none get dropped. We also test what happens if the connection drops: does the controller hold onto its data until the connection comes back? Does it send an error alert? If not, that\u2019s a bug. A few years back, a small bakery ordered 20 of our controllers to run their cake decorating lines. They told us later that the communication test caught a missing error flag that would\u2019ve made them lose track of 100 cakes a week\u2014stuff that would\u2019ve cost them thousands.<\/p>\n<p>Wait, let\u2019s talk about common mistakes I see other suppliers make. A lot of people treat unit testing like a checkbox: write a test, hit run, move on. No. You need to make these tests automated, so every time we update the firmware, we run them all in 10 minutes, and if any test fails, we don\u2019t ship the code. That\u2019s not just for us\u2014it\u2019s for the client, too. If a new firmware update breaks something that used to work, automated tests catch it before it gets to a customer. Another mistake: testing only the happy path. No one runs a production line perfectly 100% of the time. If you only test when all sensors work and all signals are clean, you\u2019re setting yourself up for callbacks. I can\u2019t stress that enough: unit testing isn\u2019t about proving your controller works when everything is ideal\u2014it\u2019s about proving it works when things go wrong.<\/p>\n<p>Let me give you a concrete example of how this all comes together, from a recent client. A car parts manufacturer reached out last quarter because their old controller kept failing safety tests every month. They switched to ours, and here\u2019s how our unit testing would\u2019ve fixed their old problem. Their safety function requires two sensors to confirm a door is closed before the press runs. Their old controller only checked one sensor, so if that sensor glitched, the press would run with the door open. Our unit tests would\u2019ve: 1) Mocked both sensors to send \u201cdoor open\u201d and \u201cdoor closed\u201d signals, confirmed the press only runs when both are closed. 2) Fed a fake glitch to one sensor, confirmed the controller doesn\u2019t run the press and sends an alert. 3) Tested the I\/O port for that safety signal to make sure it didn\u2019t flip. 4) Tested the communication to their HMI to make sure the error showed up on the screen. That\u2019s the difference between a controller that works and a controller that fails.<\/p>\n<p>Now, for the people reading this who are buying controllers\u2014not selling them. A quick tip: don\u2019t just ask suppliers \u201cdo you unit test your controllers?\u201d Ask them what exactly they test. Do they test edge cases? Do they automate the tests? Do they test communication? If a supplier can\u2019t give you specific examples of the bugs they caught in unit tests, that\u2019s a red flag. We\u2019ve had clients tell us they once got a controller that worked in the lab but died after 2 weeks on their line\u2014stuff that never would\u2019ve happened if proper unit testing was done.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.anjodrive.com\/uploads\/45277\/small\/worm-and-wheel-gearbox92434.jpg\"><\/p>\n<p>At the end of the day, unit testing isn\u2019t a fancy luxury. It\u2019s the foundation of a controller that actually does what it\u2019s supposed to, when it\u2019s supposed to, even when things go sideways. I\u2019ve built my business on this\u2014no cutting corners on testing, no shipping \u201cgood enough\u201d units. If you\u2019re looking for a controller supplier that takes testing seriously, or you have questions about what goes into making a controller reliable, feel free to reach out. We\u2019re always happy to walk through our process, or help you pick the right controller for your application.<\/p>\n<p><a href=\"https:\/\/www.anjodrive.com\/motor\/stepper-motor\/\">Stepper Motor<\/a> References<\/p>\n<ol>\n<li>Industrial Automation Handbook, 5th Edition, McGraw-Hill<\/li>\n<li>Unit Testing for Embedded Systems, Second Edition, Addison-Wesley Professional<\/li>\n<li>Automation Standards for Safety Controllers, International Society of Automation (ISA)<\/li>\n<\/ol>\n<hr>\n<p><a href=\"https:\/\/www.anjodrive.com\/\">Hangzhou ANG Drive Co., Ltd.<\/a><br \/>As one of the most professional controller manufacturers and suppliers in China, we&#8217;re featured by quality products and good service. Please rest assured to wholesale custom made controller from our factory. Good service and quality products are available.<br \/>Address: No.185, Jincheng Rd., Hangzhou 311202, China<br \/>E-mail: export2@angdrive.com<br \/>WebSite: <a href=\"https:\/\/www.anjodrive.com\/\">https:\/\/www.anjodrive.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019ve ever sold industrial automation gear, you know a dud controller can turn a client\u2019s &hellip; <a title=\"How to test a controller in unit tests?\" class=\"hm-read-more\" href=\"http:\/\/www.intacthair.com\/blog\/2026\/11\/21\/how-to-test-a-controller-in-unit-tests-4fe9-cb938c\/\"><span class=\"screen-reader-text\">How to test a controller in unit tests?<\/span>Read more<\/a><\/p>\n","protected":false},"author":989,"featured_media":3897,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3860],"class_list":["post-3897","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-controller-4c75-cc4881"],"_links":{"self":[{"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/posts\/3897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/users\/989"}],"replies":[{"embeddable":true,"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/comments?post=3897"}],"version-history":[{"count":0,"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/posts\/3897\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/posts\/3897"}],"wp:attachment":[{"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/media?parent=3897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/categories?post=3897"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.intacthair.com\/blog\/wp-json\/wp\/v2\/tags?post=3897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}