When debug in GoLand, I found init function run before some unit test, but not run before some unit test. I have not set any configuration related to unit test and init function. For example:
function A:
package1/function a()
function B:
package2/function b()
function C:
package3/int()
init runs before a(), but not befor b(). It's so weired. I have not found any law.
Unit tests should test a single package. Packages that are not referred to (not imported) from the tested package may or may not be initialized, you should not assume anything about packages not imported.
You only have guarantees that init() functions of all imported packages will be called first (recursively). The rules are listed in Spec: Package initialization.
So if you test packagea which does not import packageb, there is no guarantee that init() functions of packageb will be run. If you need that, you have to import packageb.
See related question: Is it really bad to use init in go?
Related
I have a test named builtin_functions and design_variables how can I run only these two tests and nothing else.
I have naively tried cargo test builtin_functions,design_variables and cargo test builtin_functions design_variables but neither work.
What can I do?
Unfortunately, there are only two ways of getting the behavior you specified, neither of them great. You could type
cargo test builtin_functions && cargo test design_variables
,effectively calling the function twice
or you could add an apparently arbitrary prefix that only these two tests would have before you run them, such as
fn foo_builtin_functions {
/*...*/
}
fn foo_design_variables() {
/*...*/
}
and them simply call
cargo test foo
Which would run all tests that contain foo in their name calling both
both tests synchronously, as is the default behavior, but obviously requires more forethought.
I define some subfunctions in my Octave's .m file, and would like to use the built-in self-test in Octave %test to test these subfunctions like unit test.
My .m file looks like this:
function A = fn1()
A = 1
end
function B = fn2()
B = 2
end
%!test
%!assert (fn2(), 2)
But I was told:
!!!!! test failed
'fn2' undefined near line 2 column 9
I would like to know if it is possible to define subfunctions and test it with Octave's built-in test feature. Thanks.
UPDATE:
I came from Java/Python world and am new to Octave. When I start working on some problem using Octave, I try to look for some built-in testing feature in Octave to test some "private" functions in Octave. The "private" function equivalent I found in Octave seems to be "subfunctions", and the built-in test feature in Octave is the %test self-test, that is why I am looking for a way to do self-test for subfunctions in Octave.
You can't write tests to subfunctions. The tests work by running
the code in the test block. The test fails if the code in the
test block errors. Typically the test unit would call the
function being tested somewhere in the code test.
However, subfunctions only exist in the scope of their parent
functions. They will not be available in any other scope and so
cannot be called. The test unit will fail because the function is not
defined.
This is actually a quite common question. The typical answer is that
a subfunction exists only to be called somewhere by the parent. So
instead, you should write a test unit that will cover the call to that
subfunction. If it becomes too difficult, then your parent function
may be doing too much stuff and you should consider having multiple
functions.
Assume I have a Haskell module named Foo, defined in src/Foo.hs. Assume also that Foo exports a type Bar.
Now I want to write unit tests for Bar (for the whole Foo module, actually), so I throw a couple of QuickCheck properties into test/FooTest.hs; but hey, now I need to define an Arbitrary instance for Bar.
And there's the rub: in -Wall -Werror mode, ghc requires instance declarations to appear in one of two places: in the same file where the type is defined, or where the class is defined. But I don't want to clutter my Foo module with a build dependency on QuickCheck, and I obviously cannot add an instance of Bar to QuickCheck.
So how do I make my datatype an instance of Arbitrary, for the purpose of unit testing only, without introducing a dependency on QuickCheck for users of my module and without tossing-Wall -Werror out of the window?
Within the test suite, create a newtype which wraps Bar and define the Arbitrary instance for the newtype.
Try ghc -Wall -Werror -Wno-orphans for the test module.
Not exactly perfect since it will disable the warning for other orphan instances, but I believe it's the closest we can get at the moment.
Having a "suppress this warning in the next line" pragma would also be nice.
You can conditionally define a TESTING CPP macro when you're compiling the test suite. This lets you avoid orphans but only incur the dependency when you use it. You can see a similar use of such a macro in the containers package, but that package currently uses orphan instances for the specific purpose of adding Arbitrary instances to the test suite. I may change that soon.
I need to test code that fetches and processes some data from standard C library. However, this data may not be installed at any particular system. So at the start of each test I detect whether the data is installed and skip the test.
It's easy to wrap the test in an if, but since the code that tests whether the test is possible may itself fail, I need to at least know that the tests were skipped. I know I can't simply use println! and I don't want to have to remember to pass --nocapture every time I test (and I want to see the warnings in the Travis log; Travis virtuals don't have all the data).
A lesser-known fact is that println! uses the thread-local "standard out", which can be set to other output types. During tests, this is set to a capturing type. However, you can still get direct access to the true standard out and use that:
use std::io::{self,Write};
#[test]
fn a() {
println!("Hi!"); // Will be captured / hidden by default
write!(&mut io::stdout(), "Hello!").unwrap(); // Will be shown
}
Has output like:
running 1 test
Hello!test a ... ok
Do Visual Studio unit tests in a batch run only do static initialization once?
This seems to be so. I have for example a static object with cached information in a class. I use this class in two unit tests. If I run them separately they run okay. If I run them together, I have to explicitly clear the cache again.
So it seems when I run 'Test1' and 'Test2', the static information from 'Test1' is preserved when I start to run 'Test2'.
I am presently rewriting my test, but is there anything I can to about that, in an option? By the way the static information is not part of my test itself of course, but part of the module I have to run the test on.
Static is .. static.
Variables (and constructors) of such are not automatically "reset" unless a new Application Domain (or .NET process) is used for each test, which is not the case.
The solution is to not use static (or really any) state in unit tests not Setup or contained entirely within the test itself.