Introduction to Functional Web Testing With Twill & Selenium

Part 1 :: Writing Twill Tests :: Nose just finds your tests

Synopsis

While we haven't done any testing yet, we can assure you that you won't just be writing one test. Nope. You'll be writing lots of tests. Fortunately, nose has a nice little test discovery feature that comes in handy.

Nose just finds your tests

In our previous example, we have two test files, 'unittest_toy.py' and 'test_toy.py.' Nose has this great test discovery feature that basically says, prefix your test files and functions with 'test' and nose will find and execute them. So let's add another test to the mix called 'test_more_stuff.py':


              def test_this():
                  assert True

              def test_that():
                  assert 2 > 1

              def test_the_other_thing():
                  assert "f" in "foo"

            

Now let's run 'test_more_stuff.py' with nose:

              [terryp@tpmacbook] code :: nosetests test_more_stuff.py 
              ...
              ----------------------------------------------------------------------
              Ran 3 tests in 0.001s

              OK
            

Great, we're still going! Okay, how about now we actually try out this discovery business. Let's just let nose find our tests!

              [terryp@tpmacbook] code :: nosetests 
              ....
              ----------------------------------------------------------------------
              Ran 4 tests in 0.005s

              OK
            

It found four tests this time, but which four? Add a little verbosity into the mix by passing in the '-v' option:

              [terryp@tpmacbook] code :: nosetests -v
              test_more_stuff.test_this ... ok
              test_more_stuff.test_that ... ok
              test_more_stuff.test_the_other_thing ... ok
              test_toy.test_toy ... ok

              ----------------------------------------------------------------------
              Ran 4 tests in 0.005s

              OK
            

See how it found test_more_stuff.py and test_toy.py? Pretty cool.

As mentioned in the previous section, nose has a few caveats you should keep in mind when trying to write compatible tests:

  • If it looks like a test, it's a test. Names of directories, modules, classes and functions are compared against the testMatch regular expression, and those that match are considered tests. Any class that is a unittest.TestCase subclass is also collected, so long as it is inside of a module that looks like a test.
  • Directories that don't look like tests and aren't packages are not inspected.
  • Packages are always inspected, but they are only collected if they look like tests. This means that you can include your tests inside of your packages (somepackage/tests) and nose will collect the tests without running package code inappropriately.
  • When a project appears to have library and test code organized into separate directories, library directories are examined first.
  • When nose imports a module, it adds that module's directory to sys.path; when the module is inside of a package, like package.module, it will be loaded as package.module and the directory of package will be added to sys.path.
  • If an object defines a __test__ attribute that does not evaluate to True, that object will not be collected, nor will any objects it contains.

 

next

Fluid 960 Grid System, created by Stephen Bau, based on the 960 Grid System by Nathan Smith. Released under the GPL/ MIT Licenses.