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?
How to judge a Ph.D. applicant that arrives "out of thin air"
The best place for swimming in Arctic Ocean
Can a table be formatted so that math mode is in some columns and text is in others by default?
Why does Canada require mandatory bilingualism in all government posts?
Do the books ever say oliphaunts aren’t elephants?
Why/when is AC-DC-AC conversion superior to direct AC-AC conversion?
How did the Axis intend to hold the Caucasus?
How to kill my goat in Goat Simulator
Sci-fi change: Too much or Not enough
Decreasing star count
Old French song lyrics with the word "baiser."
Could the rotation of a black hole cause other planets to rotate?
Why is it considered Acid Rain with pH <5.6
How do I stop my characters falling in love?
To find islands of 1 and 0 in matrix
Why force the nose of 737 Max down in the first place?
How do I explain an exponentially complex intuitively?
The Sword in the Stone
Seaborn style plot of pandas dataframe
What do you call a flexible diving platform?
Struggling with cyclical dependancies in unit tests
Japanese reading of an integer
Why can't my huge trees be chopped down?
Unethical behavior : should I report it?
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
4 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
4 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
4 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
4 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
4 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
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
4 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