Unable to provide CLI arguments to `cargo test` - testing

I used to be able to run specific, named tests from the command-line interface like this: cargo test <test_name>. But now this gives me the error
running 1 test
error: Found argument '<test_name>' which wasn't expected, or isn't valid in this context
Other arguments to cargo test also don't work.
The line that causes the error is this line in the test setup:
let cli_default_args = Arc::new(cli_args::Args::from_args());
Where the cli_args::Args struct is a struct that holds the value of the command line arguments and the from_args function comes from the StructOpt package derivation. cli_args::Args is decorated with #[derive(StructOpt)].

The problem was that arguments intended for cargo test were interpreted as arguments for the application.
Replacing the problematic line in the test setup
let cli_default_args = Arc::new(cli_args::Args::from_args());
with
let cli_default_args = Arc::new(cli_args::Args::from_iter::<Vec<String>>(vec![]));
fixes the problem. The above code means that your test setup runs as if the program didn't get any CLI arguments, everything is running with its default values.

Related

Karate: Scenario fails if contains __arg and run in 'stand-alone' mode

I have faced a problem when I try to run a Scenario containing built-in __arg variable as 'stand-alone' (not 'called'), then my test fails with an error (I do not #ignore the called one as in order to use it in both 'called' and 'stand-alone' modes):
evaluation (js) failed: __arg, javax.script.ScriptException: ReferenceError: "__arg" is not defined in <eval> at line number 1
stack trace: jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
Following two simple features should be enough to reproduce.
called-standalone.feature:
Feature: Called + Stand-alone Scenario
Scenario: Should not fail on __arg when run as stand-alone
* def a = __arg
* print a
caller.feature:
Feature: Caller
Scenario: call without args
When def res = call read('called-standalone.feature')
Then match res.a == null
Scenario: call with args
When def res = call read('called-standalone.feature') {some: 42}
Then match res.a == {some: 42}
Putting these two features into the skeleton project and run mvn test will show an error.
I'm expecting this should work as the docs say that "‘called’ Karate scripts ... can behave like ‘normal’ Karate tests in ‘stand-alone’ mode".
‘called’ Karate scripts don’t need to use any special keywords to ‘return’ data and can behave like ‘normal’ Karate tests in ‘stand-alone’ mode if needed
All Karate variables have to be "defined" at run time. This is a rule which can not be relaxed.
So you should re-design your scripts. The good thing is you can use karate.get() to set a "default value".
* def a = karate.get('__arg', null)
That should answer your question.

Running a main-like in a non-main package

We have a package with a fair number of complex tests. As part of the test suite, they run on builds etc.
func TestFunc(t *testing.T) {
//lots of setup stuff and defining success conditions
result := SystemModel.Run()
}
Now, for one of these tests, I want to introduce some kind of frontend which will make it possible for me to debug a few things. It's not really a test, but a debug tool. For this, I want to just run the same test but with a Builder pattern:
func TestFuncWithFrontend(t *testing.T) {
//lots of setup stuff and defining success conditions
result := SystemModel.Run().WithHTTPFrontend(":9999")
}
The test then would only start if I send a signal via HTTP from the frontend. Basically WithHTTPFrontend() just waits with a channel on a HTTP call from the frontend.
This of course would make the automated tests fail, because no such signal will be sent and execution will hang.
I can't just rename the package to main because the package has 15 files and they are used elsewhere in the system.
Likewise I haven't found a way to run a test only on demand while excluding it from the test suite, so that TestFuncWithFrontend would only run from the commandline - I don't care if with go run or go test or whatever.
I've also thought of ExampleTestFunc() but there's so much output produced by the test it's useless, and without defining Output: ..., the Example won't run.
Unfortunately, there's also a lot of initialization code at (private, i.e. lower case) package level that the test needs. So I can't just create a sub-package main, as a lot of that stuff wouldn't be accessible.
It seems I have three choices:
Export all this initialization variables and code with upper case, so that I could be using it from a sub-main package
Duplicate the whole code.
Move the test into a sub-package main and then have a func main() for the test with Frontend and a _test.go for the normal test, which would have to import a few things from the parent package.
I'd rather like to avoid the second option...And the first is better, but isn't great either IMHO. I think I'll go for the third, but...
am I missing some other option?
You can pass a custom command line argument to go test and start the debug port based on that. Something like this:
package hello_test
import (
"flag"
"log"
"testing"
)
var debugTest bool
func init() {
flag.BoolVar(&debugTest, "debug-test", false, "Setup debugging for tests")
}
func TestHelloWorld(t *testing.T) {
if debugTest {
log.Println("Starting debug port for test...")
// Start the server here
}
log.Println("Done")
}
Then if you want to run just that specific test, go test -debug-test -run '^TestHelloWorld$' ./.
Alternatively it's also possible to set a custom environment variable that you check in the test function to change behaviour.
I finally found an acceptable option. This answer
Skip some tests with go test
brought me to the right track.
Essentially using build tags which would not be present in normal builds but which I can provide when executing manually.

lua code fails on require statement

Problem Description
I have some code that I've inherited and I'm trying to debug. It's a web application that is written in lua for the back end.
The system fails right now with an error that says the following:
398: attempt to call global 'require' (a nil value)
Code
Line 398 is the require statement you see below..
getstatus = function()
require("processinfo")
local value,errtxt=processinfo.package_version(packagename)
etc...
What I've tried so far:
I've tried to test to make sure that the package / module it's looking for exists. It does. It's in my lua lib folder
test-dev:/usr/share# find / -name processinfo*
/usr/share/lua/5.1/processinfo.lua
I've also launched the lua command line and tried to include the module, like so:
test-dev:/usr/share# lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
require("processinfo")
And it seems to be happy, no error messages.
Finally, I've tried to move the require statement from line 398 to the top of the file with the rest of the include statements that I have ... like so:
module(..., package.seeall)
posix = require("posix")
utils = require("utils")
processinfo = require("processinfo")
When I do that, the error message about the require statement goes away but instead i get an error about the string function I'm calling. The error message is :
139: attempt to index global 'string' (a nil value)
Line 139 is the opening of a while loop that looks like this:
while string.find(result, "%$[%w_]+") do
-- do something
end
Additional Information
Lua version is 5.1
this code is working on another server...

Undefined global variable when using QuestaSim

I have a variable defined in foo_const.v which is defined like this in foo_const.v:
localparam NUM_BITS = 32;
Then I have another file foo_const_slice.v which does this:
localparam SLICE_ADDR_BITS = NUM_BITS;
This compiles fine with the vcs command:
vcs -sverilog foo_const.v foo_const_slice.v
But when I try to use QuestaSim:
vlog -work work -sv foo_const.v foo_const_slice.v
I get the following error message:
** Error: foo_const_slice.v(46): (vlog-2730) Undefined variable: 'NUM_BITS'.
The problem is that by default, each file that vlog compiles goes into a separate compilation unit, just like C/C++ and many other languages. By default, vcs concatenates all files together into a single compilation unit.
Although there is a way to change the default (you can look it up in the user manual), the proper way to code this is to put your parameters in a package, and import the package where needed.
Dave

Setup, runTests and teardown fails at Batman.TestCase

I am trying to get batman testing up and running. Qunit and tests runs fine, but when i use the example:
class SimpleTest extends Batman.TestCase
#test 'A simple test', ->
#assert true
test = new SimpleTest
test.runTests()
I get the following messages when i browse to localhost:3000/qunit:
Setup failed on A simple test: undefined is not a function
Died on test #2 at Test.Batman.TestCase.TestCase.Test.Test.run (localhost:3000/assets/extras/testing/test_case.js?body=1:20:22)
at SimpleTest.Batman.TestCase.TestCase.runTests (localhost:3000/assets/extras/testing/test_case.js?body=1:51:28)
at localhost:3000/assets/simple_test.js?body=1:24:8
at localhost:3000/assets/simple_test.js?body=1:26:4: undefined is not a function
Teardown failed on A simple test: undefined is not a function
In the test_helper.coffee, I manually included the project, sinon and the four test case source files from the github source code found here, including test_case.coffee.
What am I doing wrong?
Depending on how those .coffee source files are loaded, it's possible that they don't have their dependencies loaded first.
You could try this:
Download the 0.16 release from http://batmanjs.org/download.html
Use precompiled batman.testing.js from the release
Make sure QUnit loads batman.js first, then batman.testing.js. (Batman.Object must be defined before you load Batman.TestCase.)
Does that help?