Struggling with cyclical dependancies in unit testsWhat is the value of checking in failing unit tests?Unit test strategy for layered (or derived) method callsAre HSQLDB unit tests an anti pattern?Designing unit tests for a stateful systemWhat's the idea behind mocking data access in unit testsUnit testing implementation vs behaviourHow to reconcile “not mocking what you don't own” with “expectations” in unit tests?Unit testing trivial casesUnit test a generic floating point equality functionDoes it matter how I setup test data when creating unit tests?

Why did House of Representatives need to condemn Trumps Tweets?

When does Haskell complain about incorrect typing in functions?

How to judge a Ph.D. applicant that arrives "out of thin air"

Are the named pipe created by `mknod` and the FIFO created by `mkfifo` equivalent?

Isolated audio without a transformer

What does "see" in "the Holy See" mean?

Vertical tennis ball into fancy new enumerate

Melee or Ranged attacks by Monsters, no distinction in modifiers?

Converting 8V AC to 8V DC - bridge rectifier gets very hot while idling

Is my investment strategy a form of fundamental indexing?

Assuring luggage isn't lost with short layover

Use cases for M-0 & C-0?

Why is drive/partition number still used?

Seaborn style plot of pandas dataframe

What is the difference between position, displacement, and distance traveled?

Did the meaning of "significant" change in the 20th century?

How do I explain an exponentially complex intuitively?

Heisenberg uncertainty principle in daily life

Copying an existing HTML page and use it, is that against any copyright law?

Recommendations or Experiences on Archiving Mailing Data

Examples of simultaneous independent breakthroughs

How did the SysRq key get onto modern keyboards if it's rarely used?

Why isn't there a serious attempt at creating a third mass-appeal party in the US?

Defining a Function programmatically



Struggling with cyclical dependancies in unit tests


What is the value of checking in failing unit tests?Unit test strategy for layered (or derived) method callsAre HSQLDB unit tests an anti pattern?Designing unit tests for a stateful systemWhat's the idea behind mocking data access in unit testsUnit testing implementation vs behaviourHow to reconcile “not mocking what you don't own” with “expectations” in unit tests?Unit testing trivial casesUnit test a generic floating point equality functionDoes it matter how I setup test data when creating unit tests?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I'm trying to practice TDD, by using it to develop a simple like Bit Vector. I happen to be using Swift, but this is a language-agnostic question.



My BitVector is a struct that stores a single UInt64, and presents an API over it that lets you treat it like a collection. The details don't matter much, but it's pretty simple. The high 57 bits are storage bits, and the lower 6 bits are "count" bits, which tells you how many of the storage bits actually store a contained value.



So far, I have a handful of very simple capabilities:



  1. An initializer that constructs empty bit vectors

  2. A count property of type Int

  3. An isEmpty property of type Bool

  4. An equality operator (==). NB: this is a value-equality operator akin to Object.equals() in Java, not a reference equality operator like == in Java.

I'm running into a bunch of cyclical dependancies:




  1. The unit test that tests my initializer need to verify that the newly constructed BitVector. It can do so in one of 3 ways:



    1. Check bv.count == 0

    2. Check bv.isEmpty == true

    3. Check that bv == knownEmptyBitVector

    Method 1 relies on count, method 2 relies on isEmpty (which itself relies on count, so there's no point using it), method 3 relies on ==. In any case, I can't test my initializer in isolation.



  2. The test for count needs to operate on something, which inevitably tests my initializer(s)


  3. The implementation of isEmpty relies on count


  4. The implementation of == relies on count.


I was able to partly solve this problem by introducing a private API that constructs a BitVector from an existing bit pattern (as a UInt64). This allowed me to initialize values without testing any other initializers, so that I could "boot strap" my way up.



For my unit tests to truly be unit tests, I find myself doing a bunch of hacks, which complicate my prod and test code substantially.



How exactly do you get around these sorts of issues?










share|improve this question






























    2















    I'm trying to practice TDD, by using it to develop a simple like Bit Vector. I happen to be using Swift, but this is a language-agnostic question.



    My BitVector is a struct that stores a single UInt64, and presents an API over it that lets you treat it like a collection. The details don't matter much, but it's pretty simple. The high 57 bits are storage bits, and the lower 6 bits are "count" bits, which tells you how many of the storage bits actually store a contained value.



    So far, I have a handful of very simple capabilities:



    1. An initializer that constructs empty bit vectors

    2. A count property of type Int

    3. An isEmpty property of type Bool

    4. An equality operator (==). NB: this is a value-equality operator akin to Object.equals() in Java, not a reference equality operator like == in Java.

    I'm running into a bunch of cyclical dependancies:




    1. The unit test that tests my initializer need to verify that the newly constructed BitVector. It can do so in one of 3 ways:



      1. Check bv.count == 0

      2. Check bv.isEmpty == true

      3. Check that bv == knownEmptyBitVector

      Method 1 relies on count, method 2 relies on isEmpty (which itself relies on count, so there's no point using it), method 3 relies on ==. In any case, I can't test my initializer in isolation.



    2. The test for count needs to operate on something, which inevitably tests my initializer(s)


    3. The implementation of isEmpty relies on count


    4. The implementation of == relies on count.


    I was able to partly solve this problem by introducing a private API that constructs a BitVector from an existing bit pattern (as a UInt64). This allowed me to initialize values without testing any other initializers, so that I could "boot strap" my way up.



    For my unit tests to truly be unit tests, I find myself doing a bunch of hacks, which complicate my prod and test code substantially.



    How exactly do you get around these sorts of issues?










    share|improve this question


























      2












      2








      2








      I'm trying to practice TDD, by using it to develop a simple like Bit Vector. I happen to be using Swift, but this is a language-agnostic question.



      My BitVector is a struct that stores a single UInt64, and presents an API over it that lets you treat it like a collection. The details don't matter much, but it's pretty simple. The high 57 bits are storage bits, and the lower 6 bits are "count" bits, which tells you how many of the storage bits actually store a contained value.



      So far, I have a handful of very simple capabilities:



      1. An initializer that constructs empty bit vectors

      2. A count property of type Int

      3. An isEmpty property of type Bool

      4. An equality operator (==). NB: this is a value-equality operator akin to Object.equals() in Java, not a reference equality operator like == in Java.

      I'm running into a bunch of cyclical dependancies:




      1. The unit test that tests my initializer need to verify that the newly constructed BitVector. It can do so in one of 3 ways:



        1. Check bv.count == 0

        2. Check bv.isEmpty == true

        3. Check that bv == knownEmptyBitVector

        Method 1 relies on count, method 2 relies on isEmpty (which itself relies on count, so there's no point using it), method 3 relies on ==. In any case, I can't test my initializer in isolation.



      2. The test for count needs to operate on something, which inevitably tests my initializer(s)


      3. The implementation of isEmpty relies on count


      4. The implementation of == relies on count.


      I was able to partly solve this problem by introducing a private API that constructs a BitVector from an existing bit pattern (as a UInt64). This allowed me to initialize values without testing any other initializers, so that I could "boot strap" my way up.



      For my unit tests to truly be unit tests, I find myself doing a bunch of hacks, which complicate my prod and test code substantially.



      How exactly do you get around these sorts of issues?










      share|improve this question
















      I'm trying to practice TDD, by using it to develop a simple like Bit Vector. I happen to be using Swift, but this is a language-agnostic question.



      My BitVector is a struct that stores a single UInt64, and presents an API over it that lets you treat it like a collection. The details don't matter much, but it's pretty simple. The high 57 bits are storage bits, and the lower 6 bits are "count" bits, which tells you how many of the storage bits actually store a contained value.



      So far, I have a handful of very simple capabilities:



      1. An initializer that constructs empty bit vectors

      2. A count property of type Int

      3. An isEmpty property of type Bool

      4. An equality operator (==). NB: this is a value-equality operator akin to Object.equals() in Java, not a reference equality operator like == in Java.

      I'm running into a bunch of cyclical dependancies:




      1. The unit test that tests my initializer need to verify that the newly constructed BitVector. It can do so in one of 3 ways:



        1. Check bv.count == 0

        2. Check bv.isEmpty == true

        3. Check that bv == knownEmptyBitVector

        Method 1 relies on count, method 2 relies on isEmpty (which itself relies on count, so there's no point using it), method 3 relies on ==. In any case, I can't test my initializer in isolation.



      2. The test for count needs to operate on something, which inevitably tests my initializer(s)


      3. The implementation of isEmpty relies on count


      4. The implementation of == relies on count.


      I was able to partly solve this problem by introducing a private API that constructs a BitVector from an existing bit pattern (as a UInt64). This allowed me to initialize values without testing any other initializers, so that I could "boot strap" my way up.



      For my unit tests to truly be unit tests, I find myself doing a bunch of hacks, which complicate my prod and test code substantially.



      How exactly do you get around these sorts of issues?







      unit-testing tdd swift-language






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 7 hours ago







      Alexander

















      asked 9 hours ago









      AlexanderAlexander

      1,0657 silver badges12 bronze badges




      1,0657 silver badges12 bronze badges




















          1 Answer
          1






          active

          oldest

          votes


















          7














          You're worrying about implementation details too much.



          It doesn't matter that in your current implementation, isEmpty relies on count (or whatever other relationships you might have): all you should be caring about is the public interface. For example, you can have three tests:



          • That a newly initialized object has count == 0.

          • That a newly initialized object has isEmpty == true (you've written false in your question, but that seems wrong to me)

          • That a newly initialized object equals the known empty object.

          These are all valid tests, and become especially important if you ever decide to refactor the internals of your class so that isEmpty has a different implementation that doesn't rely on count - so long as your tests all still pass, you know you haven't regressed anything.



          Similar stuff applies to your other points - remember to test the public interface, not your internal implementation. You may find TDD useful here, as you'd then be writing the tests you need for isEmpty before you'd written any implementation for it at all.






          share|improve this answer























          • I suspected so much, but then it doesn't seem I'm writing units tests. From what I've read, I got that the impression that if you break only piece of code, only unit tests directly relating to that code should fail. But alas, that's not the case. Suppose I change the layout of the bit fields in my struct, moving which bits represent the count, but forgot to update the logic in count. My tests for count would break, and so would the tests for my initializers, and for isEmpty, etc. Is that okay?

            – Alexander
            7 hours ago











          • @Alexander You sound like a man in need of a clear definition of unit testing. The best one I know comes from Michael Feathers

            – candied_orange
            4 hours ago











          • @candied_orange Could you elaborate on what you suspect I have wrong? I know the value of mocks to isolate tests from network APIs/DBs/file-systems, etc., and everything else in that article was in-line with what I was already thinking

            – Alexander
            3 hours ago













          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "131"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f395306%2fstruggling-with-cyclical-dependancies-in-unit-tests%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          You're worrying about implementation details too much.



          It doesn't matter that in your current implementation, isEmpty relies on count (or whatever other relationships you might have): all you should be caring about is the public interface. For example, you can have three tests:



          • That a newly initialized object has count == 0.

          • That a newly initialized object has isEmpty == true (you've written false in your question, but that seems wrong to me)

          • That a newly initialized object equals the known empty object.

          These are all valid tests, and become especially important if you ever decide to refactor the internals of your class so that isEmpty has a different implementation that doesn't rely on count - so long as your tests all still pass, you know you haven't regressed anything.



          Similar stuff applies to your other points - remember to test the public interface, not your internal implementation. You may find TDD useful here, as you'd then be writing the tests you need for isEmpty before you'd written any implementation for it at all.






          share|improve this answer























          • I suspected so much, but then it doesn't seem I'm writing units tests. From what I've read, I got that the impression that if you break only piece of code, only unit tests directly relating to that code should fail. But alas, that's not the case. Suppose I change the layout of the bit fields in my struct, moving which bits represent the count, but forgot to update the logic in count. My tests for count would break, and so would the tests for my initializers, and for isEmpty, etc. Is that okay?

            – Alexander
            7 hours ago











          • @Alexander You sound like a man in need of a clear definition of unit testing. The best one I know comes from Michael Feathers

            – candied_orange
            4 hours ago











          • @candied_orange Could you elaborate on what you suspect I have wrong? I know the value of mocks to isolate tests from network APIs/DBs/file-systems, etc., and everything else in that article was in-line with what I was already thinking

            – Alexander
            3 hours ago















          7














          You're worrying about implementation details too much.



          It doesn't matter that in your current implementation, isEmpty relies on count (or whatever other relationships you might have): all you should be caring about is the public interface. For example, you can have three tests:



          • That a newly initialized object has count == 0.

          • That a newly initialized object has isEmpty == true (you've written false in your question, but that seems wrong to me)

          • That a newly initialized object equals the known empty object.

          These are all valid tests, and become especially important if you ever decide to refactor the internals of your class so that isEmpty has a different implementation that doesn't rely on count - so long as your tests all still pass, you know you haven't regressed anything.



          Similar stuff applies to your other points - remember to test the public interface, not your internal implementation. You may find TDD useful here, as you'd then be writing the tests you need for isEmpty before you'd written any implementation for it at all.






          share|improve this answer























          • I suspected so much, but then it doesn't seem I'm writing units tests. From what I've read, I got that the impression that if you break only piece of code, only unit tests directly relating to that code should fail. But alas, that's not the case. Suppose I change the layout of the bit fields in my struct, moving which bits represent the count, but forgot to update the logic in count. My tests for count would break, and so would the tests for my initializers, and for isEmpty, etc. Is that okay?

            – Alexander
            7 hours ago











          • @Alexander You sound like a man in need of a clear definition of unit testing. The best one I know comes from Michael Feathers

            – candied_orange
            4 hours ago











          • @candied_orange Could you elaborate on what you suspect I have wrong? I know the value of mocks to isolate tests from network APIs/DBs/file-systems, etc., and everything else in that article was in-line with what I was already thinking

            – Alexander
            3 hours ago













          7












          7








          7







          You're worrying about implementation details too much.



          It doesn't matter that in your current implementation, isEmpty relies on count (or whatever other relationships you might have): all you should be caring about is the public interface. For example, you can have three tests:



          • That a newly initialized object has count == 0.

          • That a newly initialized object has isEmpty == true (you've written false in your question, but that seems wrong to me)

          • That a newly initialized object equals the known empty object.

          These are all valid tests, and become especially important if you ever decide to refactor the internals of your class so that isEmpty has a different implementation that doesn't rely on count - so long as your tests all still pass, you know you haven't regressed anything.



          Similar stuff applies to your other points - remember to test the public interface, not your internal implementation. You may find TDD useful here, as you'd then be writing the tests you need for isEmpty before you'd written any implementation for it at all.






          share|improve this answer













          You're worrying about implementation details too much.



          It doesn't matter that in your current implementation, isEmpty relies on count (or whatever other relationships you might have): all you should be caring about is the public interface. For example, you can have three tests:



          • That a newly initialized object has count == 0.

          • That a newly initialized object has isEmpty == true (you've written false in your question, but that seems wrong to me)

          • That a newly initialized object equals the known empty object.

          These are all valid tests, and become especially important if you ever decide to refactor the internals of your class so that isEmpty has a different implementation that doesn't rely on count - so long as your tests all still pass, you know you haven't regressed anything.



          Similar stuff applies to your other points - remember to test the public interface, not your internal implementation. You may find TDD useful here, as you'd then be writing the tests you need for isEmpty before you'd written any implementation for it at all.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 7 hours ago









          Philip KendallPhilip Kendall

          6,9523 gold badges21 silver badges29 bronze badges




          6,9523 gold badges21 silver badges29 bronze badges












          • I suspected so much, but then it doesn't seem I'm writing units tests. From what I've read, I got that the impression that if you break only piece of code, only unit tests directly relating to that code should fail. But alas, that's not the case. Suppose I change the layout of the bit fields in my struct, moving which bits represent the count, but forgot to update the logic in count. My tests for count would break, and so would the tests for my initializers, and for isEmpty, etc. Is that okay?

            – Alexander
            7 hours ago











          • @Alexander You sound like a man in need of a clear definition of unit testing. The best one I know comes from Michael Feathers

            – candied_orange
            4 hours ago











          • @candied_orange Could you elaborate on what you suspect I have wrong? I know the value of mocks to isolate tests from network APIs/DBs/file-systems, etc., and everything else in that article was in-line with what I was already thinking

            – Alexander
            3 hours ago

















          • I suspected so much, but then it doesn't seem I'm writing units tests. From what I've read, I got that the impression that if you break only piece of code, only unit tests directly relating to that code should fail. But alas, that's not the case. Suppose I change the layout of the bit fields in my struct, moving which bits represent the count, but forgot to update the logic in count. My tests for count would break, and so would the tests for my initializers, and for isEmpty, etc. Is that okay?

            – Alexander
            7 hours ago











          • @Alexander You sound like a man in need of a clear definition of unit testing. The best one I know comes from Michael Feathers

            – candied_orange
            4 hours ago











          • @candied_orange Could you elaborate on what you suspect I have wrong? I know the value of mocks to isolate tests from network APIs/DBs/file-systems, etc., and everything else in that article was in-line with what I was already thinking

            – Alexander
            3 hours ago
















          I suspected so much, but then it doesn't seem I'm writing units tests. From what I've read, I got that the impression that if you break only piece of code, only unit tests directly relating to that code should fail. But alas, that's not the case. Suppose I change the layout of the bit fields in my struct, moving which bits represent the count, but forgot to update the logic in count. My tests for count would break, and so would the tests for my initializers, and for isEmpty, etc. Is that okay?

          – Alexander
          7 hours ago





          I suspected so much, but then it doesn't seem I'm writing units tests. From what I've read, I got that the impression that if you break only piece of code, only unit tests directly relating to that code should fail. But alas, that's not the case. Suppose I change the layout of the bit fields in my struct, moving which bits represent the count, but forgot to update the logic in count. My tests for count would break, and so would the tests for my initializers, and for isEmpty, etc. Is that okay?

          – Alexander
          7 hours ago













          @Alexander You sound like a man in need of a clear definition of unit testing. The best one I know comes from Michael Feathers

          – candied_orange
          4 hours ago





          @Alexander You sound like a man in need of a clear definition of unit testing. The best one I know comes from Michael Feathers

          – candied_orange
          4 hours ago













          @candied_orange Could you elaborate on what you suspect I have wrong? I know the value of mocks to isolate tests from network APIs/DBs/file-systems, etc., and everything else in that article was in-line with what I was already thinking

          – Alexander
          3 hours ago





          @candied_orange Could you elaborate on what you suspect I have wrong? I know the value of mocks to isolate tests from network APIs/DBs/file-systems, etc., and everything else in that article was in-line with what I was already thinking

          – Alexander
          3 hours ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Software Engineering Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f395306%2fstruggling-with-cyclical-dependancies-in-unit-tests%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

          Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її