unittest output in IPython - ipython

I have a script for testing a module using unittest. When I run the script using the python
console I get the output:
test_equal (__main__.TestOutcome) ... ok
test_win_amount (__main__.TestOutcome) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
But, on running the same script using IPython console, I don't get any output.
I am using the following to run my script,
suite = unittest.TestLoader().loadTestsFromTestCase(TestOutcome)
unittest.TextTestRunner(verbosity=2).run(suite)
Any ideas if this might be due to IPython settings?

Calling TextTestRunner with the stream parameter will make it work in IPython. This is how I run the tests:
suite = unittest.TestLoader().loadTestsFromTestCase(MyTest)
unittest.TextTestRunner(verbosity=1,stream=sys.stderr).run(suite)

Related

How to source environment file in python?

I am trying to automate an application restarts with Python which is currently implemented with shell scripts. Before running the restart commands, we need to source a .sh environment file which is done in the shell script with
. ./opt///**.sh. I am using paramiko and exec_command to run the restart command which throws the error that libraries are missing. So I have tried several ways to source this environment file in the python program but have not been successful.
Things I have tried:
stdin1,stdout1,stderr1 = ssh_client1.exec_command(". ./opt///**.sh")
stdin1,stdout1,stderr1 = ssh_client1.exec_command("source /opt///**.sh")
subprocess.call("/opt///**.sh", shell=True)
os.system(". /opt///**.sh")
I picked the below from stack overflow if I remember correctly
enter code herecommand = shlex.split("bash -c 'source /opt///**.sh'")
enter code hereproc = subprocess.Popen(command, stdout = subprocess.PIPE)
enter code herefor line in proc.stdout:
enter code here(key, _, value) = line.partition("=")
enter code hereos.environ[key] = value
enter code hereproc.communicate()
I still get the same error missing libraries which I am supposed to get when I do not source the environment file when I run the application restart commands using exec_command("/opt///start")
Any help is really appreciated.
Thanks.
#Martin Prikryl: Execute multiple commands in Paramiko so that commands are affected by their predecessors
I love you man!! I am writing a py script to remote ssh with paramiko that sources a local environment file before running application restart commands. I was breaking my head over this for over 2 weeks, until I read this post. It works like a charm. Thank you!!
Solution:
Each exec_command multiple times is executed in its own "shell". So the previous commands have no effect on an environment of the following commands.
Use && to combine the commands. In this way, the previous command, which in my case is the environment file, has an effect on the following command.
stdin3,stdout3,stderr3 = ssh_client1.exec_command(". /opt///env.sh && STOP")

How do I show the exact command being tested when a pytest script runs?

How is it possible that anyone uses pytest without having it output the exact command that it runs.
I have a set of 5 test scripts with a total of 41 different test combinations. The script functions all basically follow this same template where at some stage, the funciton does:
subprocess.Popen(cmdline_builder(opt, name, options))
When there's a failure, the output is nearly useless, it doesn't show the exact command that was run.
How does anybody use this ? How would you expect to debug a failed test without knowing what to run ?

Printing to Console in scalding script

I am trying to display some content on the console in a scalding script. When I run the same logic in the scalding shell I get the desired output and when I run the script I get an error:
scripttest.scala:4: error: value dump is not a member of com.twitter.scalding.typed.TypedPipe[String]
The script is
import com.twitter.scalding._
class scripttest(args:Args) extends Job(args){
val hello = TypedPipe.from(TextLine("tutorial/data/hello.txt"))
hello.dump
}
When I ran the same logic in console, it ran successfully.
The output in console:
Hello world
Goodbye world
Please explain why this occurs and how to print to console in a scalding script.
After looking closely at the documentation, you will see in section "2.6 REPL Reference", subsection "2.6.1 Enrichments available on TypedPipe/Grouped/CoGrouped objects" :
.dump: Print the contents to stdout (uses .toIterator)
hence dump is available only in the REPL.
I don't see a "scalding way" to write on the console, nor do I think it would make sense: you are running a pipeline, so the only "guaranteed milestone" is the end of the pipeline, when you can just write your results into a file, as is done on all the tutorial scripts.
If it's just a matter of printing "hello my job started", remember it's just a Scala file and use println (for more advanced logging, Logback is your friend).
To run the script locally, after having cloned the repository:
> ./sbt assembly
> ./scripts/scald.rb --local MyScript.scala
The first line will run all tests and build "scald.rb", the script used in the second line to run your scalding script.

Output selenium test result as html after running perl script

I am currently looking for a way to output the test result nicely after running selenium perl script.
The htmlSuite command from running selenium server outputs a nice html format result page, but I don't know how to do that in perl script.
Problem is, I have it setup so that Selenium is being run 24/7 on a virtual machine workstation(Windows 7), where anyone one can run tests on. Therefore I can't use htmlSuite to run the test because the server will close after the test is finished.
Is there a command argument or perl script method to make selenium server output results on html or other nice format other than printing it on the command line?
Or is there a better way to do this?
If your script is output TAP (that's what Test::More would put out), then you can use the Test::Harness family of modules to parse that TAP and use it to generate an HTML report.
How nice is nice? Under Hudson/Jenkins this gives graphs and a tabular report of tests run:
prove --timer --formatter=TAP::Formatter::JUnit large_test.t >junit.xml

DBD::Oracle fails to connect with OCIEnvInit when called when accessed through webserver only

I have a simple perl script that uses DBD::Oracle to run a query and print the results. It works fine from the command line, but I also have a PHP script that runs it and reads the output. When the PHP script is accessed through apache it fails to connect, with the error "OCIEnvInit".
I've tried creating a shell script that re-sets all the environment variables available in the shell but that didn't help, and I also tried setting the debugging output for DBI but got nothing. What could cause this error when the script does work?
Are you sure that ORACLE_HOME and other relevant environment variables (e.g., LD_LIBRARY_PATH) that are set in your shell when you run the script from the command line are also set to the same values in the apache/PHP process?