Unit testing with externally configurable parameters.

조회 수: 2 (최근 30일)
John
John 2021년 1월 13일
댓글: John 2021년 1월 13일
I am exploring Matlab's unit testing framework and I'd like to use it with a project that involves communicating with an external device connected through a serial port.
I currently have a wrapper class that encapsulates the serial interface (and the accompanying protocol implementation). This is a handle class, and contains a serial object as one of its properties. Additionally, the constructor will open the serial connection, and the destructor will close and delete it.
So in general usage I'd do something like:
my_obj = my_class('com1'); %Set up object connected to com1
my_obj.method1;
my_obj.method2;
delete(my_obj); %Destructor will close the serial object and then delete it.
Now my question. I'd like to write some unit tests (using the Matlab Testing framework). How can I set these tests up, but specify the com-port external to the test functions/scripts? Is there a way to instantiate my_obj, and then give a set of tests access to the specific my_obj?
Additional thoughts:
  • I do not want to hard-code the com-port in any of the test definitions.
  • Class-based unit tests don't seem to have conventional constructors (or a way of explicitly instantiating my_obj)
  • Function-based tests suggest that you can share information through the TestCase.TestData, but that only seems to be possible within the sub-tests that make up a test file.
  • I suppose I could get around this by loading a text/mat configuration file or checking an environment variable in the file fixtures, but that seems inelegant.
Thanks for your help.

채택된 답변

Steven Lord
Steven Lord 2021년 1월 13일
Write a parameterized test whose parameter is the port name. Inject a value for that parameter into the test when you create the test suite. See this documentation page for more information.
  댓글 수: 1
John
John 2021년 1월 13일
Okay. That looks like it will accomplish what I'm looking for. Thanks for the suggestion!
I had come across the documentation for conventional parameterized tests, but they seemed to be geared towards iterating through various combinations of specific property values.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Testing Frameworks에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by