cabal: how to stop the build on test failure? - unit-testing

i created tests in HUnit (Tests.hs). i connected them to main: main = runTestTT tests. when i do runhaskell Tests i see
### Failure in: 0
T(1)
expected: 145
but got: 45
Cases: 10 Tried: 10 Errors: 0 Failures: 1
Counts {cases = 10, tried = 10, errors = 0, failures = 1}
which is expected. in cabal file i did
test-suite xxx
type: exitcode-stdio-1.0
main-is: Tests.hs
build-depends: base ==4.5.*, HUnit ==1.2.5.2, containers == 0.5.5.1
and when i do cabal test same test logs are written to a file - so i'm sure tests are executed and failing (as expected) but in console i see:
1 of 1 test suites (1 of 1 test cases) passed.
and the exit code is 0.
so my question is: why cabal claims tests passed and how to make it report errors correctly?

I just had to figure this out myself, this is what I finally got to work....
module Main where
import Data.Monoid
import Test.Framework
import Test.Framework.Providers.HUnit
import Test.HUnit
firstTest::Assertion --This one passes
firstTest = do
assertEqual "reward state root doesn't match" (1::Int) 1
secondTest::IO () --This one fails (note, Assertion is just "IO()", so you can use either)
secondTest = do
assertEqual "empty db didn't match" (1::Int) 2
main::IO ()
main =
defaultMainWithOpts
[
testCase "ShortcutNodeData Insert" firstTest,
testCase "FullNodeData Insert" secondTest
] mempty
In my .cabal file
Test-Suite test-program
type: exitcode-stdio-1.0
main-is: Main.hs
hs-source-dirs: test
build-depends: base
, test-framework
, test-framework-hunit
, HUnit
, containers
then run with cabal test
It is tricky, because cabal looks at the exitcode (see the type above), but the HUnit outputs its own messages.... So if you don't return the correct value, you can see output like "test failed" followed by "test passed". Obviously, the solution is to use the builtin defaultMainWithOpts, which does everything correctly.

Related

scope="module" does not seem to be working in pytest

I am working with pytest fixtures. My test module is as follows :
import pytest
#pytest.yield_fixture
#pytest.fixture(scope="module")
def jlt() :
print("setup executed")
yield None
print("tearing up")
def test_one(jlt) :
id = 123
assert id == 123
def test_two(jlt) :
id = 456
assert id == 456
I am executing this as follows :
py.test -v --capture=no test_jlt.py
The output is :
platform linux2 -- Python 2.7.12, pytest-2.8.7, py-1.4.31, pluggy-0.3.1 -- /usr/bin/python
cachedir: ../../.cache
rootdir: /home/vandana/unix Home/pythonLearn, inifile: pytest.ini
collected 2 items
test_jlt.py::test_one setup executed
PASSEDtearing up
test_jlt.py::test_two setup executed
PASSEDtearing up
The scope="module" does not seem to be working. The fixture is getting executed for each function and not just once for the entire module.
I do not know what should be done
#pytest.yield_fixture replaces #pytest.fixture, so you should use #pytest.yield_fixture(scope="module") instead.
Note that with pytest 3.x you can simply use #pytest.fixture and use yield inside the fixture, which simplifies things a bit.

AttributeError: TestSwitch instance has no attribute 'assertTrue'

I have a following pyunit test case code where I am collecting the result of the function (True or False) and using it to drive my assertion. However, I am getting the "no attribute" error for assertTrue. What is missing here?
I am using python 2.7.8 and pyunit version of PyUnit-1.4.1-py2.7.
The same code when run from the Eclipse (pydev plugin) from my Mac, it works fine. Only when I take this to my Linux box, it does throw below error. So to me it looks like some package incompatibility problem.
import json
import unittest
class TestSwitch(unittest.TestCase):
def testFunction(self):
self.assertTrue(True, "test case failed")
Below is the test suite class.
import unittest
from mysample import TestSwitch
# Create an instance of each test case.
testCase = TestSwitch('testFunction')
# Add test cases to the test suite.
testSuite = unittest.TestSuite()
testSuite.addTest(testCase)
# Execute the test suite.
testRunner = unittest.TextTestRunner(verbosity=2)
testRunner.run(testSuite)
It throws below error.
bash-3.2$ python mysuite.py
testFunction (mysample.TestSwitch) ... ERROR
======================================================================
ERROR: testFunction (mysample.TestSwitch)
----------------------------------------------------------------------
Traceback (most recent call last):
File "workspace/pyunit/mysample.py", line 7, in testFunction
self.assertTrue(True, "test case failed")
AttributeError: TestSwitch instance has no attribute 'assertTrue'
----------------------------------------------------------------------
Ran 1 tests in 0.000s
FAILED (errors=1)
bash-3.2$
For now I've figured a workaround for this problem by using 'assertEqual' comparing with a boolean value and it works. I am not sure why 'assertTrue' and for that matter 'assertFalse' is having problem. I did not change any package version or anything.
The workaround code is as below.
17 def testFunction(self):
18 res = True
19 self.assertEqual(res, True, 'test case failed')

Why are my tests not getting run in my TestCase subclass?

I am learning test driven development...
I wrote a test that should fail but it's not...
(env)glitch:ipals nathann$ ./manage.py test npage/
Creating test database for alias 'default'...
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
in npage/ I have tests.py:
from django.test import TestCase
from npage.models import Tip
import datetime
# Example
class TipTester(TestCase):
def setUp(self):
print dir(self)
Tip.objects.create(pk=1,
text='Testing',
es_text='Probando')
def tips_in_spanish(self):
my_tip = Tip.objects.get(pk=1)
my_tip.set_language('es')
self.assertEqual(my_tip.text, 'this does not just say \'Probando\'')
What am I doing wrong? I've read this but I still can't figure out what is going wrong here.
Your test functions need to start with test:
def test_tips_in_spanish(self):
Docs here
"When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in any file whose name begins with test, automatically build a test suite out of those test cases, and run that suite."

Package visibility in Go Unit Tests

Given the following code file (named server.go) in Go:
package glimpse
func SplitHeader() string {
return "hi there"
}
and the accompanying test file (server_test.go):
package glimpse
import (
"testing"
)
func TestSplitHeader(t *testing.T) {
answer := SplitHeader()
if answer == "" {
t.Error("No return value")
}
}
Why is it the following command:
go test server_test.go
returns
# command-line-arguments
./server_test.go:9: undefined: SplitHeader
I'm certainly missing something catastrophically obvious.
Use only
$ go test
from within the package directory to perform testing. If you name specific files as an argument to go test, then only those file will be considered for the build of the test binary. That explains the 'undefined' error.
As an alternative, use "import path" as an argument to go test instead, for example
$ go test foo.com/glimpse

Why does a failing test with mox fail other tests as well?

My issue is quite simple: I'm having a bunch of unit tests using pymox. When I add a new test that fails, most of the time a whole lot of others fail as well. How can I prevent that from happening?
For example, I have a simple script for which I have two unit tests:
def test_main_returnsUnknown_ifCalculator_returnsMinus1(self):
m=mox.Mox()
m.StubOutWithMock(check_es_insert,"getArgs")
check_es_insert.getArgs(\
'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
.AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
################
#some other mocking here, not relevant, I think
################
m.ReplayAll()
#now let's test
check_es_docs.main()
#verify and cleanup
m.UnsetStubs()
m.VerifyAll()
m.ResetAll()
def test_main_doesWhatPrintAndExitSays_inNormalConditions(self):
m=mox.Mox()
m.StubOutWithMock(check_es_insert,"getArgs")
check_es_insert.getArgs(\
'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
.AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
################
#some other mocking here, not relevant, I think
################
m.ReplayAll()
#now let's test
check_es_docs.main()
#verify and clean up
m.UnsetStubs()
m.VerifyAll()
m.ResetAll()
Normally, both tests pass, but if I sneak in a typo on my second tests, I get this output when running the tests:
$ ./check_es_docs.test.py
FE
======================================================================
ERROR: test_main_returnsUnknown_ifCalculator_returnsMinus1 (__main__.Main)
If it can't get the current value from ES, print an error message and exit 3
----------------------------------------------------------------------
Traceback (most recent call last):
File "./check_es_docs.test.py", line 13, in test_main_returnsUnknown_ifCalculator_returnsMinus1
m.StubOutWithMock(check_es_insert,"getArgs")
File "/usr/local/lib/python2.7/dist-packages/mox-0.5.3-py2.7.egg/mox.py", line 312, in StubOutWithMock
raise TypeError('Cannot mock a MockAnything! Did you remember to '
TypeError: Cannot mock a MockAnything! Did you remember to call UnsetStubs in your previous test?
======================================================================
FAIL: test_main_doesWhatPrintAndExitSays_inNormalConditions (__main__.Main)
If getCurrent returns a positive value, main() should print the text and exit with the code Calculator.printandexit() says
----------------------------------------------------------------------
Traceback (most recent call last):
File "./check_es_docs.test.py", line 69, in test_main_doesWhatPrintAndExitSays_inNormalConditions
check_es_docs.main()
File "/home/radu/check_es_docs.py", line 25, in main
check_es_insert.printer("Total number of documents in Elasticsearch is %d | 'es_docs'=%d;%d;%d;;" % (result,result,cmdline['warning'],cmdline['critical']))
File "/usr/local/lib/python2.7/dist-packages/mox-0.5.3-py2.7.egg/mox.py", line 765, in __call__
return mock_method(*params, **named_params)
File "/usr/local/lib/python2.7/dist-packages/mox-0.5.3-py2.7.egg/mox.py", line 1002, in __call__
expected_method = self._VerifyMethodCall()
File "/usr/local/lib/python2.7/dist-packages/mox-0.5.3-py2.7.egg/mox.py", line 1060, in _VerifyMethodCall
raise UnexpectedMethodCallError(self, expected)
UnexpectedMethodCallError: Unexpected method call. unexpected:- expected:+
- printer.__call__("Total number of documents in Elasticsearch is 3 | 'es_docs'=3;5;7;;") -> None
? -
+ printer.__call__("Total nuber of documents in Elasticsearch is 3 | 'es_docs'=3;5;7;;") -> None
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (failures=1, errors=1)
The first test should have passed with no error, since it wasn't changed a bit. check_es_insert.getArgs() shouldn't be a MockAnything instance, and I didn't forget to call UnsetStubs. I've searched quite a lot and I didn't find other people with the same problem. So I guess I'm missing something pretty obvious...
Additional info:
check_es_docs is the script I'm testing
check_es_insert is another script from which I'm importing a lot of stuff
I've tried putting UnsetStubs() after VerifyAll() with the same results
I've tried initializing the mox.Mox() object from the SetUp method, and also putting the cleanup stuff in TearDown, with the same results
I would recommend putting all of your tests into test classes that extend TestCase and then add in an UnsetStubs in the tearDown method:
from unittest import TestCase
import mox
class MyTestCasee(TestCase):
def __init__(self, testCaseName):
self.m = mox.Mox()
TestCase.__init__(self, testCaseName)
def tearDown(self):
self.m.UnsetStubs()
def test_main_returnsUnknown_ifCalculator_returnsMinus1(self):
self.m.StubOutWithMock(check_es_insert,"getArgs")
check_es_insert.getArgs(\
'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
.AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
################
#some other mocking here, not relevant, I think
################
self.m.ReplayAll()
#now let's test
check_es_docs.main()
#verify and cleanup
self.m.VerifyAll()
def test_main_doesWhatPrintAndExitSays_inNormalConditions(self):
self.m.StubOutWithMock(check_es_insert,"getArgs")
check_es_insert.getArgs(\
'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
.AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
################
#some other mocking here, not relevant, I think
################
self.m.ReplayAll()
#now let's test
check_es_docs.main()
#verify and clean up
self.m.VerifyAll()
self.m.ResetAll()
You can also use mox.MoxTestBase, which sets up self.mox and calls VerifyAll() on tearDown.
class ClassTestTest(mox.MoxTestBase):
def test():
m = self.mox.CreateMockAnything()
m.something()
self.mox.ReplayAll()
m.something() # If this line is removed the test will fail