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;
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:
- An initializer that constructs empty bit vectors
- A
count
property of typeInt
- An
isEmpty
property of typeBool
- An equality operator (
==
). NB: this is a value-equality operator akin toObject.equals()
in Java, not a reference equality operator like==
in Java.
I'm running into a bunch of cyclical dependancies:
The unit test that tests my initializer need to verify that the newly constructed
BitVector
. It can do so in one of 3 ways:- Check
bv.count == 0
- Check
bv.isEmpty == true
- Check that
bv == knownEmptyBitVector
Method 1 relies on
count
, method 2 relies onisEmpty
(which itself relies oncount
, so there's no point using it), method 3 relies on==
. In any case, I can't test my initializer in isolation.- Check
The test for
count
needs to operate on something, which inevitably tests my initializer(s)The implementation of
isEmpty
relies oncount
The implementation of
==
relies oncount
.
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
add a comment |
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:
- An initializer that constructs empty bit vectors
- A
count
property of typeInt
- An
isEmpty
property of typeBool
- An equality operator (
==
). NB: this is a value-equality operator akin toObject.equals()
in Java, not a reference equality operator like==
in Java.
I'm running into a bunch of cyclical dependancies:
The unit test that tests my initializer need to verify that the newly constructed
BitVector
. It can do so in one of 3 ways:- Check
bv.count == 0
- Check
bv.isEmpty == true
- Check that
bv == knownEmptyBitVector
Method 1 relies on
count
, method 2 relies onisEmpty
(which itself relies oncount
, so there's no point using it), method 3 relies on==
. In any case, I can't test my initializer in isolation.- Check
The test for
count
needs to operate on something, which inevitably tests my initializer(s)The implementation of
isEmpty
relies oncount
The implementation of
==
relies oncount
.
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
add a comment |
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:
- An initializer that constructs empty bit vectors
- A
count
property of typeInt
- An
isEmpty
property of typeBool
- An equality operator (
==
). NB: this is a value-equality operator akin toObject.equals()
in Java, not a reference equality operator like==
in Java.
I'm running into a bunch of cyclical dependancies:
The unit test that tests my initializer need to verify that the newly constructed
BitVector
. It can do so in one of 3 ways:- Check
bv.count == 0
- Check
bv.isEmpty == true
- Check that
bv == knownEmptyBitVector
Method 1 relies on
count
, method 2 relies onisEmpty
(which itself relies oncount
, so there's no point using it), method 3 relies on==
. In any case, I can't test my initializer in isolation.- Check
The test for
count
needs to operate on something, which inevitably tests my initializer(s)The implementation of
isEmpty
relies oncount
The implementation of
==
relies oncount
.
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
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:
- An initializer that constructs empty bit vectors
- A
count
property of typeInt
- An
isEmpty
property of typeBool
- An equality operator (
==
). NB: this is a value-equality operator akin toObject.equals()
in Java, not a reference equality operator like==
in Java.
I'm running into a bunch of cyclical dependancies:
The unit test that tests my initializer need to verify that the newly constructed
BitVector
. It can do so in one of 3 ways:- Check
bv.count == 0
- Check
bv.isEmpty == true
- Check that
bv == knownEmptyBitVector
Method 1 relies on
count
, method 2 relies onisEmpty
(which itself relies oncount
, so there's no point using it), method 3 relies on==
. In any case, I can't test my initializer in isolation.- Check
The test for
count
needs to operate on something, which inevitably tests my initializer(s)The implementation of
isEmpty
relies oncount
The implementation of
==
relies oncount
.
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
unit-testing tdd swift-language
edited 7 hours ago
Alexander
asked 9 hours ago
AlexanderAlexander
1,0657 silver badges12 bronze badges
1,0657 silver badges12 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 writtenfalse
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.
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 incount
. My tests forcount
would break, and so would the tests for my initializers, and forisEmpty
, 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 writtenfalse
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.
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 incount
. My tests forcount
would break, and so would the tests for my initializers, and forisEmpty
, 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
add a comment |
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 writtenfalse
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.
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 incount
. My tests forcount
would break, and so would the tests for my initializers, and forisEmpty
, 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
add a comment |
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 writtenfalse
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.
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 writtenfalse
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.
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 incount
. My tests forcount
would break, and so would the tests for my initializers, and forisEmpty
, 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
add a comment |
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 incount
. My tests forcount
would break, and so would the tests for my initializers, and forisEmpty
, 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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